Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #84 from PolymerElements/fix-flaky-test
Browse files Browse the repository at this point in the history
Don't run tests until the page is under SW control
  • Loading branch information
jeffposnick committed Dec 16, 2015
2 parents 4cfa76a + 3a52a11 commit fb5a32d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
17 changes: 17 additions & 0 deletions test/controlled-promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Provides an equivalent to navigator.serviceWorker.ready that waits for the page to be controlled,
// as opposed to waiting for the active service worker.
// See https://github.com/slightlyoff/ServiceWorker/issues/799
window._controlledPromise = new Promise(function(resolve) {
// Resolve with the registration, to match the .ready promise's behavior.
var resolveWithRegistration = function() {
navigator.serviceWorker.getRegistration().then(function(registration) {
resolve(registration);
});
};

if (navigator.serviceWorker.controller) {
resolveWithRegistration();
} else {
navigator.serviceWorker.addEventListener('controllerchange', resolveWithRegistration);
}
});
3 changes: 2 additions & 1 deletion test/platinum-sw-cache/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../../web-component-tester/browser.js"></script>
<script src="../controlled-promise.js"></script>

<link rel="import" href="../../platinum-sw-register.html">
<link rel="import" href="../../platinum-sw-cache.html">
Expand All @@ -32,7 +33,7 @@

suite('Precaching', function() {
test('precache results in Cache Storage API entries', function() {
return navigator.serviceWorker.ready.then(function() {
return window._controlledPromise.then(function() {
return window.fetch(swc.cacheConfigFile).then(function(response) {
return response.json();
}).then(function(config) {
Expand Down
13 changes: 7 additions & 6 deletions test/platinum-sw-fetch/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../../web-component-tester/browser.js"></script>
<script src="../controlled-promise.js"></script>

<link rel="import" href="../../platinum-sw-register.html">
<link rel="import" href="../../platinum-sw-import-script.html">
Expand All @@ -34,23 +35,23 @@
<script>
suite('Service Worker Fetch Handlers', function() {
test('the same-origin custom fetch handler is used when the path matches', function() {
return navigator.serviceWorker.ready.then(function() {
return window.fetch('customFetch').then(function(response) {
assert.equal(response.status, 203, 'Custom response status doesn\'t match');
});
return window._controlledPromise.then(function() {
return window.fetch('customFetch');
}).then(function(response) {
assert.equal(response.status, 203, 'Custom response status doesn\'t match');
});
});

test('the same-origin custom fetch handler isn\'t used when the path doesn\t match', function() {
return navigator.serviceWorker.ready.then(function() {
return window._controlledPromise.then(function() {
return window.fetch('dummyUrlThatShould404').then(function(response) {
assert.equal(response.status, 404, 'Expected response status doesn\'t match');
});
});
});

test('the cross-origin fetch handler is used when the path and origin matches', function() {
return navigator.serviceWorker.ready.then(function() {
return window._controlledPromise.then(function() {
return window.fetch('https://matching.domain/path/to/customFetch').then(function(response) {
assert.equal(response.status, 410, 'Custom response status doesn\'t match');
});
Expand Down
5 changes: 2 additions & 3 deletions test/platinum-sw-import-script/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../../web-component-tester/browser.js"></script>
<script src="../controlled-promise.js"></script>

<link rel="import" href="../../platinum-sw-register.html">
<link rel="import" href="../../platinum-sw-import-script.html">
Expand All @@ -27,10 +28,8 @@

<script>
suite('Importing Service Worker Scripts', function() {
// TODO: postMessage/message events are in flux in the Service Worker spec, so this may
// need to change in the future.
test('message handler is registered via imported script', function(done) {
return navigator.serviceWorker.ready.then(function(registration) {
return window._controlledPromise.then(function(registration) {
var message = 'hello';
var messageChannel = new MessageChannel();
messageChannel.port1.onmessage = function(event) {
Expand Down
3 changes: 2 additions & 1 deletion test/platinum-sw-register/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../../web-component-tester/browser.js"></script>
<script src="../controlled-promise.js"></script>

<link rel="import" href="../../platinum-sw-register.html">
</head>
Expand All @@ -27,7 +28,7 @@

suite('Registration & Installation', function() {
test('creates an active service worker', function() {
return navigator.serviceWorker.ready.then(function(registration) {
return window._controlledPromise.then(function(registration) {
assert.ok(registration.active);
});
});
Expand Down

0 comments on commit fb5a32d

Please sign in to comment.