Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
Merge pull request #65 from GoogleChrome/fix-tests
Browse files Browse the repository at this point in the history
Fix broken unit tests
  • Loading branch information
jeffposnick committed Jan 5, 2016
2 parents e2b6e73 + 3448296 commit 4f19cca
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

navigator.serviceWorker.register('service-worker.js');

// We want to delay the start of our tests until the page is controlled by
// the service worker, since only at that point will the service worker
// intercept network requests.
// We can't use navigator.serviceWorker.ready, since that promise resolves once
// the service worker is activated (but before it takes control of the page),
// so we need to create our own equivalent promise.
// See https://github.com/slightlyoff/ServiceWorker/issues/799
var controlledPromise = new Promise(function(resolve) {
if (navigator.serviceWorker.controller) {
resolve();
} else {
navigator.serviceWorker.addEventListener('controllerchange', resolve);
}
});

var checkValue = function(url, value, assert, method) {
var done = assert.async();
method = method || 'get';
Expand Down Expand Up @@ -37,8 +52,7 @@ var pausePromise = function(timeout) {
});
};

navigator.serviceWorker.ready.then(function() {

controlledPromise.then(function() {
QUnit.test('Default route', function(assert) {
checkValue('not/real/path', 'Default', assert);
});
Expand Down Expand Up @@ -66,10 +80,6 @@ navigator.serviceWorker.ready.then(function() {

checkValue('matches/only/head', 'OK', assert, 'head');
checkValue('matches/only/head', 'Default', assert, 'get');
checkValue('matches/only/head', 'Default', assert, 'put');
checkValue('matches/only/head', 'Default', assert, 'post');
checkValue('matches/only/head', 'Default', assert, 'delete');
checkValue('matches/only/head', 'Default', assert, 'x-custom');
});

QUnit.test('First declared route wins', function(assert) {
Expand Down

0 comments on commit 4f19cca

Please sign in to comment.