Skip to content

Commit

Permalink
Merge pull request #1992 from GoogleChrome/migrate-routing-tests
Browse files Browse the repository at this point in the history
Migrate routing tests to run in a SW environment
  • Loading branch information
philipwalton committed Mar 28, 2019
2 parents cb2eac0 + 47bf2c0 commit 76ba317
Show file tree
Hide file tree
Showing 18 changed files with 246 additions and 317 deletions.
12 changes: 6 additions & 6 deletions infra/testing/helpers/extendable-event-utils.mjs
Expand Up @@ -20,29 +20,29 @@ export const eventDoneWaiting = async (event) => {
}
};

export const watchEvent = (event) => {
export const spyOnEvent = (event) => {
const promises = [];
extendLifetimePromises.set(event, promises);

event.waitUntil = (promise) => {
event.waitUntil = sinon.stub().callsFake((promise) => {
promises.push(promise);
};
});

if (event instanceof FetchEvent) {
event.respondWith = (responseOrPromise) => {
event.respondWith = sinon.stub().callsFake((responseOrPromise) => {
eventResponses.set(event, responseOrPromise);
promises.push(Promise.resolve(responseOrPromise));

// TODO(philipwalton): we cannot currently call the native
// `respondWith()` due to this bug in Firefix:
// https://bugzilla.mozilla.org/show_bug.cgi?id=1538756
// FetchEvent.prototype.respondWith.call(event, responseOrPromise);
};
});
}
};

export const dispatchAndWaitUntilDone = async (event) => {
watchEvent(event);
spyOnEvent(event);
self.dispatchEvent(event);
await eventDoneWaiting(event);
};
Expand Down
2 changes: 1 addition & 1 deletion packages/workbox-routing/Router.mjs
Expand Up @@ -105,7 +105,7 @@ class Router {
event.waitUntil(requestPromises);

// If a MessageChannel was used, reply to the message on success.
if (event.ports) {
if (event.ports && event.ports[0]) {
await requestPromises;
event.ports[0].postMessage(true);
}
Expand Down
16 changes: 16 additions & 0 deletions test/workbox-routing/integration/test-sw.js
@@ -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-routing]`, function() {
it(`passes all SW unit tests`, async function() {
await runUnitTests('/test/workbox-routing/sw/');
});
});
51 changes: 0 additions & 51 deletions test/workbox-routing/node/test-index.mjs

This file was deleted.

45 changes: 0 additions & 45 deletions test/workbox-routing/node/test-setCatchHandler.mjs

This file was deleted.

45 changes: 0 additions & 45 deletions test/workbox-routing/node/test-setDefaultHandler.mjs

This file was deleted.

Expand Up @@ -6,10 +6,8 @@
https://opensource.org/licenses/MIT.
*/

import {expect} from 'chai';
import expectError from '../../../infra/testing/expectError.js';
import {devOnly} from '../../../infra/testing/env-it';
import {NavigationRoute} from '../../../packages/workbox-routing/NavigationRoute.mjs';
import {NavigationRoute} from 'workbox-routing/NavigationRoute.mjs';


const handler = {
handle: () => {},
Expand All @@ -18,8 +16,10 @@ const functionHandler = () => {};

const invalidHandlerObject = {};

describe(`[workbox-routing] NavigationRoute`, function() {
devOnly.it(`should throw when called without a valid handler parameter in dev`, async function() {
describe(`NavigationRoute`, function() {
it(`should throw when called without a valid handler parameter in dev`, async function() {
if (process.env.NODE_ENV === 'production') this.skip();

await expectError(
() => new NavigationRoute(),
'incorrect-type',
Expand Down
Expand Up @@ -6,14 +6,10 @@
https://opensource.org/licenses/MIT.
*/

import sinon from 'sinon';
import {expect} from 'chai';
import {RegExpRoute} from 'workbox-routing/RegExpRoute.mjs';

import {RegExpRoute} from '../../../packages/workbox-routing/RegExpRoute.mjs';
import expectError from '../../../infra/testing/expectError.js';
import {devOnly} from '../../../infra/testing/env-it.js';

describe(`[workbox-routing] RegExpRoute`, function() {
describe(`RegExpRoute`, function() {
const SAME_ORIGIN_URL = new URL('https://example.com');
const CROSS_ORIGIN_URL = new URL('https://cross-origin-example.com');
const PATH = '/test/path';
Expand All @@ -22,14 +18,16 @@ describe(`[workbox-routing] RegExpRoute`, function() {
const sandbox = sinon.createSandbox();
beforeEach(function() {
sandbox.restore();
sandbox.stub(global, 'location').value(SAME_ORIGIN_URL);
sandbox.stub(self, 'location').value(SAME_ORIGIN_URL);
});
after(function() {
sandbox.restore();
});

for (const badRegExp of [undefined, null, 123, '123', {}]) {
devOnly.it(`should throw when called with a regExp parameter of ${JSON.stringify(badRegExp)} in dev`, async function() {
it(`should throw when called with a regExp parameter of ${JSON.stringify(badRegExp)} in dev`, async function() {
if (process.env.NODE_ENV === 'production') this.skip();

await expectError(
() => new RegExpRoute(),
'incorrect-class',
Expand Down
Expand Up @@ -6,9 +6,8 @@
https://opensource.org/licenses/MIT.
*/

import {expect} from 'chai';
import expectError from '../../../infra/testing/expectError.js';
import {Route} from '../../../packages/workbox-routing/Route.mjs';
import {Route} from 'workbox-routing/Route.mjs';


const match = () => {};
const handler = {
Expand All @@ -20,7 +19,7 @@ const method = 'POST';
const invalidHandlerObject = {};
const invalidMethod = 'INVALID';

describe(`workbox-routing: Route`, function() {
describe(`Route`, function() {
it(`should throw when called without any parameters in dev`, async function() {
if (process.env.NODE_ENV === 'production') return this.skip();

Expand Down

0 comments on commit 76ba317

Please sign in to comment.