-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest.js
313 lines (247 loc) · 8.38 KB
/
test.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import {setTimeout as delay} from 'node:timers/promises';
import test from 'ava';
import {JSDOM} from 'jsdom';
import {promiseStateSync} from 'p-state';
import elementReady, {observeReadyElements} from './index.js';
const {window} = new JSDOM();
global.window = window;
global.document = window.document;
global.MutationObserver = window.MutationObserver;
test('check if element ready', async t => {
const elementCheck = elementReady('#unicorn', {stopOnDomReady: false});
(async () => {
await delay(500);
const element = document.createElement('p');
element.id = 'unicorn';
document.body.append(element);
})();
const element = await elementCheck;
t.is(element.id, 'unicorn');
});
test('check elements against a predicate', async t => {
const elementCheck = elementReady('li', {
stopOnDomReady: false,
predicate: element => element.textContent && element.textContent.match(/wanted/i),
});
(async () => {
await delay(500);
const listElement = document.createElement('ul');
for (const text of ['some text', 'wanted text']) {
const li = document.createElement('li');
li.textContent = text;
listElement.append(li);
}
document.body.append(listElement);
})();
const element = await elementCheck;
t.is(element.textContent, 'wanted text');
});
test('check if element ready inside target', async t => {
const target = document.createElement('p');
const elementCheck = elementReady('#unicorn', {
target,
stopOnDomReady: false,
});
(async () => {
await delay(500);
const element = document.createElement('p');
element.id = 'unicorn';
target.append(element);
})();
const element = await elementCheck;
t.is(element.id, 'unicorn');
});
test('check if different elements ready inside different targets with same selector', async t => {
const target1 = document.createElement('p');
const elementCheck1 = elementReady('.unicorn', {
target: target1,
stopOnDomReady: false,
});
const target2 = document.createElement('span');
const elementCheck2 = elementReady('.unicorn', {
target: target2,
stopOnDomReady: false,
});
(async () => {
await delay(500);
const element1 = document.createElement('p');
element1.id = 'unicorn1';
element1.className = 'unicorn';
target1.append(element1);
const element2 = document.createElement('span');
element2.id = 'unicorn2';
element2.className = 'unicorn';
target2.append(element2);
})();
const element1 = await elementCheck1;
t.is(element1.id, 'unicorn1');
const element2 = await elementCheck2;
t.is(element2.id, 'unicorn2');
});
test('check if element ready after dom loaded', async t => {
const elementCheck = elementReady('#bio', {
stopOnDomReady: true,
});
// The element will be added eventually, but we're not around to wait for it
setTimeout(() => {
const element = document.createElement('p');
element.id = 'bio';
document.body.append(element);
}, 50_000);
const element = await elementCheck;
t.is(element, undefined);
});
test('check if element ready before dom loaded', async t => {
const element = document.createElement('p');
element.id = 'history';
document.body.append(element);
const elementCheck = elementReady('#history', {
stopOnDomReady: true,
});
t.is(await elementCheck, element);
});
test('stop checking if DOM was already ready', async t => {
const elementCheck = elementReady('#no-gonna-get-us', {
stopOnDomReady: true,
});
t.is(await elementCheck, undefined);
});
test('check if element ready after timeout', async t => {
const elementCheck = elementReady('#cheezburger', {
stopOnDomReady: false,
timeout: 1000,
});
// The element will be added eventually, but we're not around to wait for it
const timeoutId = setTimeout(() => {
const element = document.createElement('p');
element.id = 'cheezburger';
document.body.append(element);
}, 50_000);
const element = await elementCheck;
t.is(element, undefined);
clearTimeout(timeoutId);
});
test('check if element ready before timeout', async t => {
const element = document.createElement('p');
element.id = 'thunders';
document.body.append(element);
const elementCheck = elementReady('#thunders', {
stopOnDomReady: false,
timeout: 10,
});
t.is(await elementCheck, element);
});
test('check if wait can be stopped', async t => {
const elementCheck = elementReady('#dofle', {stopOnDomReady: false});
await delay(200);
elementCheck.stop();
await delay(500);
const element = document.createElement('p');
element.id = 'dofle';
document.body.append(element);
t.is(await elementCheck, undefined);
});
test('ensure different promises are returned on second call with the same selector when first was stopped', async t => {
const elementCheck1 = elementReady('.unicorn', {stopOnDomReady: false});
elementCheck1.stop();
const elementCheck2 = elementReady('.unicorn', {stopOnDomReady: false});
t.not(elementCheck1, elementCheck2);
t.is(await elementCheck1, undefined);
});
test('ensure different promises are returned on second call with the same selector when first was found', async t => {
const prependElement = () => {
const element = document.createElement('p');
element.className = 'unicorn';
document.body.prepend(element);
return element;
};
t.is(prependElement(), await elementReady('.unicorn'));
document.querySelector('.unicorn').remove();
t.is(prependElement(), await elementReady('.unicorn'));
document.querySelector('.unicorn').remove();
t.is(prependElement(), await elementReady('.unicorn'));
});
test('ensure that the whole element has loaded', async t => {
const {window} = new JSDOM('<nav class="loading-html-fixture">');
const {document} = window;
// Fake the pre-DOM-ready state
Object.defineProperty(document, 'readyState', {
get: () => 'loading',
});
const navigationElement = document.querySelector('nav');
const partialCheck = elementReady('nav', {
target: document,
waitForChildren: false,
});
const entireCheck = elementReady('nav', {
target: document,
waitForChildren: true,
});
t.is(await partialCheck, navigationElement, '<nav> appears in the loading document, so it should be found whether it’s loaded fully or not');
const expectation = 'elementReady can’t guarantee the element has loaded in full';
t.is(promiseStateSync(entireCheck), 'pending', expectation);
navigationElement.innerHTML = '<ul><li>Home</li><li>About</li></ul>';
t.is(promiseStateSync(entireCheck), 'pending', expectation);
navigationElement.insertAdjacentHTML('beforebegin', '<h1>Site title</h1>');
t.is(promiseStateSync(entireCheck), 'pending', expectation);
navigationElement.after('Some other part of the page, even a text node');
t.is(await entireCheck, await partialCheck, 'Something appears after <nav>, so it’s guaranteed that it loaded in full');
});
test('subscribe to newly added elements that match a selector', async t => {
(async () => {
await delay(500);
const element = document.createElement('p');
element.id = 'unicorn';
document.body.append(element);
const element2 = document.createElement('p');
element2.id = 'unicorn';
document.body.append(element2);
await delay(500);
const element3 = document.createElement('p');
element3.id = 'unicorn3';
document.body.append(element3);
})();
const readyElements = observeReadyElements('#unicorn, #unicorn3', {stopOnDomReady: false});
let readyElementsCount = 0;
for await (const element of readyElements) {
readyElementsCount++;
t.is(element.id, 'unicorn');
if (readyElementsCount === 2) {
break;
}
}
for await (const element of readyElements) {
readyElementsCount++;
t.is(element.id, 'unicorn3');
if (readyElementsCount === 3) {
break;
}
}
});
test('subscribe to newly added elements that match a predicate', async t => {
(async () => {
await delay(500);
const element = document.createElement('p');
element.textContent = 'unicorn';
document.body.append(element);
const element2 = document.createElement('p');
element2.textContent = 'horse';
document.body.append(element2);
await delay(500);
const element3 = document.createElement('p');
element3.textContent = 'penguin';
document.body.append(element3);
})();
const readyElements = observeReadyElements('p', {
stopOnDomReady: false,
predicate: element => element.textContent && element.textContent.match(/penguin|unicorn/),
});
let readyElementsCount = 0;
for await (const element of readyElements) {
readyElementsCount++;
t.regex(element.textContent, /unicorn|penguin/);
if (readyElementsCount === 2) {
break;
}
}
});