Skip to content

Commit e872744

Browse files
committed
test
1 parent 2ed1f1b commit e872744

File tree

1 file changed

+59
-21
lines changed

1 file changed

+59
-21
lines changed

test/quicklink.spec.js

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ mainSuite('should prefetch in-viewport links correctly (UMD)', async context =>
3535
responseURLs.push(resp.url());
3636
});
3737
await context.page.goto(`${server}/test-basic-usage.html`);
38-
await sleep();
38+
await context.page.waitForResponse(resp => resp.url() === `${server}/3.html`);
3939
assert.instance(responseURLs, Array);
4040
assert.ok(responseURLs.includes(`${server}/1.html`));
4141
assert.ok(responseURLs.includes(`${server}/2.html`));
@@ -70,7 +70,7 @@ mainSuite('should prefetch in-viewport links that scroll into view correctly (UM
7070
await context.page.evaluate(_ => {
7171
window.scrollBy(0, window.innerHeight);
7272
});
73-
await sleep();
73+
await context.page.waitForResponse(resp => resp.url() === `${server}/4.html`);
7474
assert.instance(responseURLs, Array);
7575
assert.ok(responseURLs.includes(`${server}/1.html`));
7676
assert.ok(responseURLs.includes(`${server}/2.html`));
@@ -84,7 +84,7 @@ mainSuite('should prefetch in-viewport links from a custom DOM source', async co
8484
responseURLs.push(resp.url());
8585
});
8686
await context.page.goto(`${server}/test-custom-dom-source.html`);
87-
await sleep();
87+
await context.page.waitForResponse(resp => resp.url() === `${server}/main.css`);
8888
assert.instance(responseURLs, Array);
8989
assert.ok(responseURLs.includes(`${server}/main.css`));
9090
});
@@ -95,7 +95,8 @@ mainSuite('should prefetch in-viewport links from NodeList', async context => {
9595
responseURLs.push(resp.url());
9696
});
9797
await context.page.goto(`${server}/test-node-list.html`);
98-
await sleep();
98+
await context.page.waitForResponse(resp => resp.url() === `${server}/2.html`);
99+
await context.page.waitForResponse(resp => resp.url() === `${server}/3.html`);
99100
assert.instance(responseURLs, Array);
100101
assert.ok(responseURLs.includes(`${server}/2.html`));
101102
assert.ok(responseURLs.includes(`${server}/3.html`));
@@ -106,23 +107,43 @@ mainSuite('should only prefetch links if allowed in origins list', async context
106107
context.page.on('response', resp => {
107108
responseURLs.push(resp.url());
108109
});
110+
await context.page.setRequestInterception(true);
111+
const requestHandler = req => {
112+
if (req.url().startsWith('https://example.com/') || req.url().startsWith('https://github.githubassets.com/')) {
113+
req.respond({status: 200, body: 'mocked'});
114+
} else {
115+
req.continue();
116+
}
117+
};
118+
context.page.on('request', requestHandler);
109119
await context.page.goto(`${server}/test-allow-origin.html`);
110-
await sleep(1000);
120+
await sleep(200);
111121
assert.instance(responseURLs, Array);
112122

113-
// => origins: ['github.githubassets.com']
123+
// => origins: ['github.githubassets.com', 'example.com']
114124
assert.not.ok(responseURLs.includes(`${server}/2.html`));
115125
assert.ok(responseURLs.includes('https://example.com/1.html'));
116126
assert.ok(responseURLs.includes('https://github.githubassets.com/images/spinners/octocat-spinner-32.gif'));
127+
context.page.off('request', requestHandler);
128+
await context.page.setRequestInterception(false);
117129
});
118130

119131
mainSuite('should prefetch all links when allowing all origins', async context => {
120132
const responseURLs = [];
121133
context.page.on('response', resp => {
122134
responseURLs.push(resp.url());
123135
});
136+
await context.page.setRequestInterception(true);
137+
const requestHandler = req => {
138+
if (req.url().startsWith('https://')) {
139+
req.respond({status: 200, body: 'mocked'});
140+
} else {
141+
req.continue();
142+
}
143+
};
144+
context.page.on('request', requestHandler);
124145
await context.page.goto(`${server}/test-allow-origin-all.html`);
125-
await sleep();
146+
await sleep(200);
126147
assert.instance(responseURLs, Array);
127148

128149
// => origins: true
@@ -131,6 +152,8 @@ mainSuite('should prefetch all links when allowing all origins', async context =
131152
assert.ok(responseURLs.includes('https://example.com/1.html'));
132153
assert.ok(responseURLs.includes(`${server}/2.html`));
133154
assert.ok(responseURLs.includes('https://github.githubassets.com/images/spinners/octocat-spinner-32.gif'));
155+
context.page.off('request', requestHandler);
156+
await context.page.setRequestInterception(false);
134157
});
135158

136159
mainSuite('should only prefetch links of same origin (default)', async context => {
@@ -139,7 +162,7 @@ mainSuite('should only prefetch links of same origin (default)', async context =
139162
responseURLs.push(resp.url());
140163
});
141164
await context.page.goto(`${server}/test-same-origin.html`);
142-
await sleep();
165+
await context.page.waitForResponse(resp => resp.url() === `${server}/2.html`);
143166
assert.instance(responseURLs, Array);
144167

145168
// => origins: [location.hostname] (default)
@@ -173,7 +196,7 @@ mainSuite('should only prefetch links after ignore patterns allowed it (multiple
173196
responseURLs.push(resp.url());
174197
});
175198
await context.page.goto(`${server}/test-ignore-multiple.html`);
176-
await sleep();
199+
await context.page.waitForResponse(resp => resp.url() === `${server}/2.html`);
177200
assert.instance(responseURLs, Array);
178201

179202
// => origins: true (all)
@@ -193,7 +216,7 @@ mainSuite('should accept a single URL to prefetch()', async context => {
193216
responseURLs.push(resp.url());
194217
});
195218
await context.page.goto(`${server}/test-prefetch-single.html`);
196-
await sleep();
219+
await context.page.waitForResponse(resp => resp.url() === `${server}/2.html`);
197220
assert.instance(responseURLs, Array);
198221
assert.ok(responseURLs.includes(`${server}/2.html`));
199222
});
@@ -204,7 +227,7 @@ mainSuite('should accept multiple URLs to prefetch()', async context => {
204227
responseURLs.push(resp.url());
205228
});
206229
await context.page.goto(`${server}/test-prefetch-multiple.html`);
207-
await sleep();
230+
await context.page.waitForResponse(resp => resp.url() === `${server}/4.html`);
208231

209232
// don't care about first 3 URLs (markup)
210233
const ours = responseURLs.slice(3);
@@ -221,7 +244,7 @@ mainSuite('should not prefetch() the same URL repeatedly', async context => {
221244
responseURLs.push(resp.url());
222245
});
223246
await context.page.goto(`${server}/test-prefetch-duplicate.html`);
224-
await sleep();
247+
await context.page.waitForResponse(resp => resp.url() === `${server}/2.html`);
225248

226249
// don't care about first 3 URLs (markup)
227250
const ours = responseURLs.slice(3);
@@ -237,7 +260,7 @@ mainSuite.skip('should not call the same URL repeatedly (shared)', async context
237260
responseURLs.push(resp.url());
238261
});
239262
await context.page.goto(`${server}/test-prefetch-duplicate-shared.html`);
240-
await sleep();
263+
await context.page.waitForResponse(resp => resp.url() === `${server}/2.html`);
241264

242265
// count occurrences of our link
243266
const target = responseURLs.filter(x => x === `${server}/2.html`);
@@ -250,7 +273,7 @@ mainSuite('should not exceed the `limit` total', async context => {
250273
responseURLs.push(resp.url());
251274
});
252275
await context.page.goto(`${server}/test-limit.html`);
253-
await sleep();
276+
await context.page.waitForResponse(resp => resp.url() === `${server}/1.html`);
254277

255278
// don't care about first 3 URLs (markup)
256279
const ours = responseURLs.slice(3);
@@ -266,7 +289,7 @@ mainSuite('should respect the `throttle` concurrency', async context => {
266289
// ~> so that we can ensure throttling occurs
267290
await context.page.setRequestInterception(true);
268291

269-
context.page.on('request', async req => {
292+
const throttleHandler = async req => {
270293
const url = req.url();
271294
if (/test\/fixtures\/\d+\.html$/i.test(url)) {
272295
await sleep(100);
@@ -275,7 +298,8 @@ mainSuite('should respect the `throttle` concurrency', async context => {
275298
}
276299

277300
req.continue();
278-
});
301+
};
302+
context.page.on('request', throttleHandler);
279303

280304
await context.page.goto(`${server}/test-throttle.html`);
281305

@@ -288,6 +312,8 @@ mainSuite('should respect the `throttle` concurrency', async context => {
288312
// Note: Parallel requests, w/ 50ms buffer
289313
await sleep(250);
290314
assert.is(URLs.length, 4);
315+
context.page.off('request', throttleHandler);
316+
await context.page.setRequestInterception(false);
291317
});
292318

293319
mainSuite('should prefetch using a custom function to build the URL', async context => {
@@ -296,15 +322,28 @@ mainSuite('should prefetch using a custom function to build the URL', async cont
296322
responseURLs.push(resp.url());
297323
});
298324

325+
await context.page.setRequestInterception(true);
326+
const hrefMockHandler = req => {
327+
const url = req.url();
328+
if (url.startsWith('https://example.com/?url=')) {
329+
return req.respond({status: 200, body: 'mocked'});
330+
}
331+
req.continue();
332+
};
333+
context.page.on('request', hrefMockHandler);
334+
299335
await context.page.goto(`${server}/test-custom-href-function.html`);
300-
await sleep();
336+
await sleep(200);
301337

302338
// don't care about first 3 URLs (markup)
303339
const ours = responseURLs.slice(3);
304340
assert.ok(ours.includes(`https://example.com/?url=${server}/1.html`));
305341
assert.ok(ours.includes(`https://example.com/?url=${server}/2.html`));
306342
assert.ok(ours.includes(`https://example.com/?url=${server}/3.html`));
307343
assert.ok(ours.includes(`https://example.com/?url=${server}/4.html`));
344+
345+
context.page.off('request', hrefMockHandler);
346+
await context.page.setRequestInterception(false);
308347
});
309348

310349
mainSuite('should delay prefetch for in-viewport links correctly (UMD)', async context => {
@@ -313,7 +352,7 @@ mainSuite('should delay prefetch for in-viewport links correctly (UMD)', async c
313352
responseURLs.push(resp.url());
314353
});
315354
await context.page.goto(`${server}/test-delay.html`);
316-
await sleep();
355+
await context.page.waitForResponse(resp => resp.url() === `${server}/3.html`);
317356
assert.instance(responseURLs, Array);
318357
assert.ok(responseURLs.includes(`${server}/1.html`));
319358
assert.ok(responseURLs.includes(`${server}/2.html`));
@@ -322,7 +361,6 @@ mainSuite('should delay prefetch for in-viewport links correctly (UMD)', async c
322361
await context.page.evaluate(_ => {
323362
window.scrollBy(0, window.innerHeight);
324363
});
325-
await sleep(100);
326364
await context.page.evaluate(_ => {
327365
window.scrollBy(0, -window.innerHeight);
328366
});
@@ -331,7 +369,7 @@ mainSuite('should delay prefetch for in-viewport links correctly (UMD)', async c
331369
await context.page.evaluate(_ => {
332370
window.scrollBy(0, window.innerHeight);
333371
});
334-
await sleep(200);
372+
await sleep(400);
335373
assert.ok(responseURLs.includes(`${server}/4.html`));
336374
});
337375

@@ -345,7 +383,7 @@ mainSuite('should consider threshold option before prefetching (UMD)', async con
345383
width: 1000,
346384
height: 800,
347385
});
348-
await sleep();
386+
await sleep(200);
349387
assert.instance(responseURLs, Array);
350388
assert.ok(responseURLs.includes(`${server}/1.html`));
351389
assert.ok(responseURLs.includes(`${server}/2.html`));

0 commit comments

Comments
 (0)