Skip to content

Commit b89faf0

Browse files
committed
test: add “ignores” suite
1 parent 53f5169 commit b89faf0

3 files changed

Lines changed: 92 additions & 0 deletions

File tree

test/quicklink.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,37 @@ describe('quicklink tests', function () {
127127
expect(responseURLs).to.not.include('https://example.com/1.html');
128128
expect(responseURLs).to.not.include('https://github.githubassets.com/images/spinners/octocat-spinner-32.gif');
129129
});
130+
131+
it('should only prefetch links after ignore patterns allowed it', async function () {
132+
const responseURLs = [];
133+
page.on('response', resp => {
134+
responseURLs.push(resp.url());
135+
});
136+
await page.goto(`${server}/test-ignore-basic.html`);
137+
138+
await page.waitFor(1000);
139+
expect(responseURLs).to.be.an('array');
140+
//=> origins: [location.hostname] (default)
141+
//=> ignores: /2.html/
142+
expect(responseURLs).to.not.include(`${server}/2.html`); // via ignores
143+
expect(responseURLs).to.not.include('https://example.com/1.html'); // via same origin
144+
expect(responseURLs).to.not.include('https://github.githubassets.com/images/spinners/octocat-spinner-32.gif'); // via same origin
145+
});
146+
147+
it('should only prefetch links after ignore patterns allowed it (multiple)', async function () {
148+
const responseURLs = [];
149+
page.on('response', resp => {
150+
responseURLs.push(resp.url());
151+
});
152+
await page.goto(`${server}/test-ignore-multiple.html`);
153+
154+
await page.waitFor(1000);
155+
expect(responseURLs).to.be.an('array');
156+
//=> origins: true (all)
157+
//=> ignores: [...]
158+
expect(responseURLs).to.include(`${server}/2.html`);
159+
expect(responseURLs).to.not.include('https://example.com/1.html'); // /example/
160+
expect(responseURLs).to.not.include('https://foobar.com/3.html'); // (uri) => uri.includes('foobar')
161+
expect(responseURLs).to.not.include('https://github.githubassets.com/images/spinners/octocat-spinner-32.gif'); // (uri, elem) => elem.textContent.includes('Spinner')
162+
});
130163
});

test/test-ignore-basic.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<title>Prefetch: Ignore Basic</title>
7+
<meta name="viewport" content="width=device-width, initial-scale=1">
8+
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
9+
<script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>
10+
</head>
11+
<body>
12+
<a href="https://example.com/1.html">Link 1</a>
13+
<a href="2.html">Link 2</a>
14+
<a href="https://foobar.com/3.html">Link 3</a>
15+
<a href="https://github.githubassets.com/images/spinners/octocat-spinner-32.gif">Spinner</a>
16+
<section id="stuff">
17+
<a href="main.css">CSS</a>
18+
</section>
19+
<a href="4.html" style="position:absolute;margin-top:900px;">Link 4</a>
20+
<script src="../dist/quicklink.umd.js"></script>
21+
<script>
22+
quicklink({
23+
ignores: /2.html/
24+
});
25+
</script>
26+
</body>
27+
</html>

test/test-ignore-multiple.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<title>Prefetch: Ignore Multiple</title>
7+
<meta name="viewport" content="width=device-width, initial-scale=1">
8+
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
9+
<script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>
10+
</head>
11+
<body>
12+
<a href="https://example.com/1.html">Link 1</a>
13+
<a href="2.html">Link 2</a>
14+
<a href="https://foobar.com/3.html">Link 3</a>
15+
<a href="https://github.githubassets.com/images/spinners/octocat-spinner-32.gif">Spinner</a>
16+
<section id="stuff">
17+
<a href="main.css">CSS</a>
18+
</section>
19+
<a href="4.html" style="position:absolute;margin-top:900px;">Link 4</a>
20+
<script src="../dist/quicklink.umd.js"></script>
21+
<script>
22+
quicklink({
23+
origins: true, // allow all initially
24+
ignores: [
25+
/example/,
26+
(uri) => uri.includes('foobar'),
27+
(uri, elem) => elem.textContent.includes('Spinner')
28+
]
29+
});
30+
</script>
31+
</body>
32+
</html>

0 commit comments

Comments
 (0)