-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutation-ready.test.ts
More file actions
96 lines (77 loc) · 3.04 KB
/
mutation-ready.test.ts
File metadata and controls
96 lines (77 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { mutationReady } from './mutation-ready';
describe('mutationReady', () => {
beforeEach(() => {
document.body.innerHTML = '';
});
it('should call callback when element is already in DOM', () => {
const callback = jest.fn();
const div = document.createElement('div');
div.className = 'test-element';
document.body.appendChild(div);
mutationReady('.test-element', callback);
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith(div);
});
it('should not call callback twice for the same element', () => {
const callback = jest.fn();
const div = document.createElement('div');
div.className = 'test-element';
document.body.appendChild(div);
mutationReady('.test-element', callback);
mutationReady('.test-element', callback);
expect(callback).toHaveBeenCalledTimes(2);
});
it('should call callback for each matching element already in DOM', () => {
const callback = jest.fn();
const div1 = document.createElement('div');
const div2 = document.createElement('div');
div1.className = 'test-element';
div2.className = 'test-element';
document.body.appendChild(div1);
document.body.appendChild(div2);
mutationReady('.test-element', callback);
expect(callback).toHaveBeenCalledTimes(2);
expect(callback).toHaveBeenCalledWith(div1);
expect(callback).toHaveBeenCalledWith(div2);
});
it('should call callback when a new element is added to the DOM', async () => {
const callback = jest.fn();
mutationReady('.dynamic-element', callback);
expect(callback).not.toHaveBeenCalled();
const div = document.createElement('div');
div.className = 'dynamic-element';
document.body.appendChild(div);
await Promise.resolve();
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith(div);
});
it('should call callback for a new element but not re-fire for the same element node', async () => {
const callback = jest.fn();
const div = document.createElement('div');
div.className = 'payment-method';
mutationReady('.payment-method', callback);
document.body.appendChild(div);
await Promise.resolve();
expect(callback).toHaveBeenCalledTimes(1);
document.body.removeChild(div);
document.body.appendChild(div);
await Promise.resolve();
expect(callback).toHaveBeenCalledTimes(1);
});
it('should call callback again when a fresh element with the same selector is added', async () => {
const callback = jest.fn();
const div1 = document.createElement('div');
div1.className = 'payment-method';
mutationReady('.payment-method', callback);
document.body.appendChild(div1);
await Promise.resolve();
expect(callback).toHaveBeenCalledTimes(1);
document.body.removeChild(div1);
const div2 = document.createElement('div');
div2.className = 'payment-method';
document.body.appendChild(div2);
await Promise.resolve();
expect(callback).toHaveBeenCalledTimes(2);
expect(callback).toHaveBeenCalledWith(div2);
});
});