Skip to content

Commit

Permalink
Update tests to run in a SW env
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Apr 1, 2019
1 parent eb73b03 commit 90756b4
Show file tree
Hide file tree
Showing 15 changed files with 903 additions and 1,074 deletions.
4 changes: 4 additions & 0 deletions gulp-tasks/test-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ const runIntegrationTestSuite = async (testPath, nodeEnv, seleniumBrowser,
testPath.includes('workbox-expiration') ||
testPath.includes('workbox-google-analytics') ||
testPath.includes('workbox-navigation-preload') ||
testPath.includes('workbox-precaching') ||
testPath.includes('workbox-range-requests') ||
testPath.includes('workbox-routing') ||
testPath.includes('workbox-strategies') ||
testPath.includes('workbox-window')) {
testMatcher = 'test-*.js';
} else {
Expand Down
18 changes: 9 additions & 9 deletions infra/testing/helpers/compareResponses.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
https://opensource.org/licenses/MIT.
*/

import {expect} from 'chai';

function compareResponses(first, second, shouldBeSame) {
const firstClone = first.clone();
const secondClone = second.clone();
const compareResponses = async (first, second, shouldBeSame) => {
const firstBody = await first.clone().text();
const secondBody = await second.clone().text();

return Promise.all([firstClone.text(), secondClone.text()])
.then(([firstBody, secondBody]) => {
return expect(firstBody === secondBody).to.eql(shouldBeSame);
});
}
if (shouldBeSame) {
expect(firstBody).to.equal(secondBody);
} else {
expect(firstBody).to.not.equal(secondBody);
}
};

export {
compareResponses,
Expand Down
11 changes: 7 additions & 4 deletions infra/testing/helpers/extendable-event-utils.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2018 Google LLC
Copyright 2019 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
Expand All @@ -10,13 +10,16 @@
const extendLifetimePromises = new WeakMap();
const eventResponses = new WeakMap();

export const eventDoneWaiting = async (event) => {
export const eventDoneWaiting = async (event, {catchErrors = true} = {}) => {
const promises = extendLifetimePromises.get(event);
let promise;

while (promise = promises.shift()) {
// Ignore errors.
await promise.catch((e) => e);
// Ignore errors by default;
if (catchErrors) {
promise = promise.catch((e) => e);
}
await promise;
}
};

Expand Down
18 changes: 18 additions & 0 deletions infra/testing/helpers/generateOpaqueResponse.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright 2019 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/


// Cache a resonse value and clone it instead of re-fetching every time.
let response;

export const generateOpaqueResponse = async () => {
if (!response) {
response = await fetch('https://google.com', {mode: 'no-cors'});
}
return response.clone();
};
14 changes: 14 additions & 0 deletions infra/testing/helpers/generateUniqueResponse.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
Copyright 2019 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/


let uid = 0;

export const generateUniqueResponse = (responseInit = {}) => {
return new Response(`${++uid}`, responseInit);
};
15 changes: 15 additions & 0 deletions infra/testing/helpers/sleep.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Copyright 2019 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

/**
* Asynchronously waits for the passed number of milliseconds.
*
* @param {number} ms
* @return {Promise}
*/
export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
16 changes: 16 additions & 0 deletions test/workbox-strategies/integration/test-sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Copyright 2019 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

const {runUnitTests} = require('../../../infra/testing/webdriver/runUnitTests');


describe(`[workbox-strategies]`, function() {
it(`passes all SW unit tests`, async function() {
await runUnitTests('/test/workbox-strategies/sw/');
});
});
22 changes: 9 additions & 13 deletions test/workbox-strategies/sw/plugins/test-cacheOkAndOpaquePlugin.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,26 @@
https://opensource.org/licenses/MIT.
*/

import {expect} from 'chai';
import {cacheOkAndOpaquePlugin} from '../../../packages/workbox-strategies/plugins/cacheOkAndOpaquePlugin.mjs';
import {cacheOkAndOpaquePlugin} from 'workbox-strategies/plugins/cacheOkAndOpaquePlugin.mjs';
import {generateOpaqueResponse} from '../../../../infra/testing/helpers/generateOpaqueResponse.mjs';
import {generateUniqueResponse} from '../../../../infra/testing/helpers/generateUniqueResponse.mjs';

describe(`[workbox-strategies] cacheOkAndOpaquePlugin`, function() {

describe(`cacheOkAndOpaquePlugin`, function() {
for (const status of [206, 404]) {
it(`should return null when status is ${status}`, function() {
const response = new Response('Hello', {
status,
});
const response = generateUniqueResponse({status});
expect(cacheOkAndOpaquePlugin.cacheWillUpdate({response})).to.equal(null);
});
}

it(`should return Response if status is opaque`, function() {
const response = new Response('Hello', {
status: 0,
});
it(`should return Response if status is opaque`, async function() {
const response = await generateOpaqueResponse();
expect(cacheOkAndOpaquePlugin.cacheWillUpdate({response})).to.equal(response);
});

it(`should return Response if status is 200`, function() {
const response = new Response('Hello', {
status: 200,
});
const response = generateUniqueResponse();
expect(cacheOkAndOpaquePlugin.cacheWillUpdate({response})).to.equal(response);
});
});

0 comments on commit 90756b4

Please sign in to comment.