Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a CacheOnly integration test #1128

Merged
merged 1 commit into from
Dec 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()));