Skip to content

Commit

Permalink
Adding a cache only integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Gaunt committed Dec 14, 2017
1 parent 21c8338 commit 91cbb6a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
73 changes: 73 additions & 0 deletions test/workbox-strategies/integration/test-cacheOnly.js
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;
});
});
7 changes: 7 additions & 0 deletions test/workbox-strategies/static/cache-only/index.html
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>
15 changes: 15 additions & 0 deletions test/workbox-strategies/static/cache-only/sw.js
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()));

0 comments on commit 91cbb6a

Please sign in to comment.