Skip to content

Commit

Permalink
feat(tests): improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
addyosmani committed Nov 25, 2018
1 parent d0b8911 commit 3ee52bd
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 5 deletions.
24 changes: 24 additions & 0 deletions demo/test-basic-usage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Prefetch: Basic Usage</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>
</head>
<body>
<a href="1.html">Link 1</a>
<a href="2.html">Link 2</a>
<a href="3.html">Link 3</a>
<section id="stuff">
<a href="main.css">CSS</a>
</section>
<a href="4.html" style="position:absolute;margin-top:900px;">Link 4</a>
<script src="../dist/quicklink.umd.js"></script>
<script>
quicklink();
</script>
</body>
</html>
27 changes: 27 additions & 0 deletions demo/test-custom-dom-source.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Prefetch: Custom DOM source</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>
</head>
<body>
<a href="1.html">Link 1</a>
<a href="2.html">Link 2</a>
<a href="3.html">Link 3</a>
<section id="stuff">
<a href="main.css">CSS</a>
</section>
<a href="4.html" style="position:absolute;margin-top:900px;">Link 4</a>
<script src="../dist/quicklink.umd.js"></script>
<script>
let elem = document.getElementById('stuff');
quicklink({
el: elem
});
</script>
</body>
</html>
24 changes: 24 additions & 0 deletions demo/test-es-modules.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Prefetch: ES Modules</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>
</head>
<body>
<a href="1.html">Link 1</a>
<a href="2.html">Link 2</a>
<a href="3.html">Link 3</a>
<section id="stuff">
<a href="main.css">CSS</a>
</section>
<a href="4.html" style="position:absolute;margin-top:900px;">Link 4</a>
<script type="module">
import quicklink from "../dist/quicklink.mjs";
quicklink();
</script>
</body>
</html>
26 changes: 26 additions & 0 deletions demo/test-static-url-list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Prefetch: Static URL list</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>
</head>
<body>
<a href="1.html">Link 1</a>
<a href="2.html">Link 2</a>
<a href="3.html">Link 3</a>
<section id="stuff">
<a href="main.css">CSS</a>
</section>
<a href="4.html" style="position:absolute;margin-top:900px;">Link 4</a>
<script src="../dist/quicklink.umd.js"></script>
<script>
quicklink({
urls: ['2.html','4.html']
});
</script>
</body>
</html>
42 changes: 37 additions & 5 deletions test/sample.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,59 @@ describe('quicklink tests', function () {

before(async function () {
page = await browser.newPage();
await page.goto('http://127.0.0.1:8080/demo/index.html');
// await page.goto('http://127.0.0.1:8080/demo/index.html');
});

after(async function () {
await page.close();
});

it('should have the correct page title', async function () {
expect(await page.title()).to.eql('Prefetch experiments');
it('should prefetch in-viewport links correctly (UMD)', async function () {
const responseURLs = [];
page.on('response', resp => {
responseURLs.push(resp.url());
});
await page.goto('http://127.0.0.1:8080/demo/test-basic-usage.html');
await page.waitFor(1000);
expect(responseURLs).to.be.an('array');
expect(responseURLs).to.include('http://127.0.0.1:8080/demo/2.html');
expect(responseURLs).to.include('http://127.0.0.1:8080/demo/2.html');
expect(responseURLs).to.include('http://127.0.0.1:8080/demo/3.html');
});

it('should prefetch pages correctly', async function () {
it('should prefetch in-viewport links correctly (ES Modules)', async function () {
const responseURLs = [];
page.on('response', resp => {
responseURLs.push(resp.url());
});
await page.goto('http://127.0.0.1:8080/demo/index.html');
await page.goto('http://127.0.0.1:8080/demo/test-es-modules.html');
await page.waitFor(1000);
expect(responseURLs).to.be.an('array');
expect(responseURLs).to.include('http://127.0.0.1:8080/demo/2.html');
expect(responseURLs).to.include('http://127.0.0.1:8080/demo/2.html');
expect(responseURLs).to.include('http://127.0.0.1:8080/demo/3.html');
});

it('should prefetch a static list of URLs correctly', async function () {
const responseURLs = [];
page.on('response', resp => {
responseURLs.push(resp.url());
});
await page.goto('http://127.0.0.1:8080/demo/test-static-url-list.html');
await page.waitFor(1000);
expect(responseURLs).to.be.an('array');
expect(responseURLs).to.include('http://127.0.0.1:8080/demo/2.html');
expect(responseURLs).to.include('http://127.0.0.1:8080/demo/4.html');
});

it('should prefetch in-viewport links from a custom DOM source', async function () {
const responseURLs = [];
page.on('response', resp => {
responseURLs.push(resp.url());
});
await page.goto('http://127.0.0.1:8080/demo/test-custom-dom-source.html');
await page.waitFor(1000);
expect(responseURLs).to.be.an('array');
expect(responseURLs).to.include('http://127.0.0.1:8080/demo/main.css');
});
});

0 comments on commit 3ee52bd

Please # to comment.