-
Notifications
You must be signed in to change notification settings - Fork 823
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a cache only integration test
- Loading branch information
Matt Gaunt
committed
Dec 14, 2017
1 parent
21c8338
commit 91cbb6a
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const expect = require('chai').expect; | ||
const seleniumAssistant = require('selenium-assistant'); | ||
|
||
describe(`[workbox-strategies] CacheOnly Requests`, function() { | ||
let webdriver; | ||
let testServerAddress = global.__workbox.server.getAddress(); | ||
|
||
beforeEach(async function() { | ||
if (webdriver) { | ||
await seleniumAssistant.killWebDriver(webdriver); | ||
webdriver = null; | ||
} | ||
|
||
global.__workbox.server.reset(); | ||
|
||
// Allow async functions 10s to complete | ||
webdriver = await global.__workbox.seleniumBrowser.getSeleniumDriver(); | ||
webdriver.manage().timeouts().setScriptTimeout(10 * 1000); | ||
}); | ||
|
||
after(async function() { | ||
if (webdriver) { | ||
await seleniumAssistant.killWebDriver(webdriver); | ||
} | ||
}); | ||
|
||
const activateSW = async (swFile) => { | ||
const error = await webdriver.executeAsyncScript((swFile, cb) => { | ||
navigator.serviceWorker.register(swFile) | ||
.then(() => { | ||
navigator.serviceWorker.addEventListener('controllerchange', () => { | ||
if (navigator.serviceWorker.controller.scriptURL.endsWith(swFile)) { | ||
cb(); | ||
} | ||
}); | ||
}) | ||
.catch((err) => { | ||
cb(err); | ||
}); | ||
}, swFile); | ||
if (error) { | ||
throw error; | ||
} | ||
}; | ||
|
||
it(`should respond with cached and non-cached entry`, async function() { | ||
const testingURl = `${testServerAddress}/test/workbox-strategies/static/cache-only/`; | ||
const SW_URL = `${testingURl}sw.js`; | ||
|
||
// Load the page and wait for the first service worker to register and activate. | ||
await webdriver.get(testingURl); | ||
|
||
// Register the first service worker. | ||
await activateSW(SW_URL); | ||
|
||
let response = await webdriver.executeAsyncScript((cb) => { | ||
fetch(new URL(`/CacheOnly/InCache/`, location).href) | ||
.then((response) => response.text()) | ||
.then((responseBody) => cb(responseBody)) | ||
.catch((err) => cb(err.message)); | ||
}); | ||
expect(response).to.equal('Cached'); | ||
|
||
// For a non-cached entry, the fetch should throw an error with | ||
// a message defined by the browser. | ||
response = await webdriver.executeAsyncScript((cb) => { | ||
fetch(new URL(`/CacheOnly/NotInCache/`, location).href) | ||
.then(() => cb(null)) | ||
.catch((err) => cb(err.message)); | ||
}); | ||
expect(response).to.exist; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<html> | ||
<head> | ||
</head> | ||
<body> | ||
<p>You need to manually register sw.js</p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
importScripts('/__WORKBOX/buildFile/workbox-sw'); | ||
|
||
/* globals workbox */ | ||
|
||
workbox.routing.registerRoute( | ||
new RegExp('/CacheOnly/.*/'), | ||
new workbox.strategies.CacheOnly(), | ||
); | ||
|
||
self.addEventListener('install', (event) => event.waitUntil( | ||
caches.open(workbox.core.cacheNames.runtime) | ||
.then((cache) => cache.put('/CacheOnly/InCache/', new Response('Cached'))) | ||
.then(() => self.skipWaiting())) | ||
); | ||
self.addEventListener('activate', (event) => event.waitUntil(self.clients.claim())); |