Skip to content

Commit

Permalink
Merge pull request GoogleChromeLabs#5 from GoogleChrome/tests
Browse files Browse the repository at this point in the history
Added some basic tests
  • Loading branch information
wibblymat committed Jun 16, 2015
2 parents 8763dcc + 8dedb7c commit 90abd79
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"gulp-jshint": "^1.9.0",
"jshint-stylish": "^1.0.0",
"path-to-regexp": "^1.0.1",
"qunitjs": "^1.18.0",
"serviceworker-cache-polyfill": "coonsta/cache-polyfill",
"vinyl-source-stream": "^1.0.0"
},
Expand Down
13 changes: 13 additions & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html>
<head>
<title>Service Worker Toolbox Tests</title>
<link rel="stylesheet" href="../node_modules/qunitjs/qunit/qunit.css"/>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="../node_modules/qunitjs/qunit/qunit.js"></script>
<script src="tests.js"></script>
</body>
</html>
72 changes: 72 additions & 0 deletions tests/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

/* Setup */
importScripts('../sw-toolbox.js');

self.addEventListener('install', function(event) {
self.skipWaiting();
});

self.addEventListener('activate', function(event) {
event.waitUntil(self.clients.claim());
});

toolbox.options.debug = true;

/* Helpers */
var respondString = function(string) {
return function() {
return new Response(string);
};
};

var respondOK = respondString('OK');
var respondError = function(reason) {
return new Response(`Error: ${reason}`, {status: 500});
};

var rewrite = function(find, replace) {
return function(request, values, options) {
var req = new Request(request.url.replace(find, replace), request);
var route = toolbox.router.match(req);
if (!route) {
return toolbox.router.default;
}
return route(req, values, options);
}
};

/*
This section is actually just ensuring that the test infrastructure is fine
*/
toolbox.router.get('', rewrite(/\/$/, '/index.html'));
toolbox.router.get('index.html', toolbox.fastest);
toolbox.router.get('tests.js', toolbox.networkFirst);
toolbox.router.get('/(.*)/qunit/(.*)', toolbox.fastest);
toolbox.precache(['index.html', 'test.js', '../node_modules/qunitjs/qunit/qunit.css', '../node_modules/qunitjs/qunit/qunit.js']);

/* Routes needed for tests */
toolbox.router.default = respondString('Default');
toolbox.router.get(new URL('absolute/route', self.location).pathname, respondOK);
toolbox.router.get('relative/route', respondOK);
toolbox.router.get('matching/:string/patterns', function(request, values) {
return new Response(values.string);
});

toolbox.router.any('matches/any/method', respondOK);
toolbox.router.head('matches/only/head', respondOK);

toolbox.router.get('multiple/match/:foo.html', respondString('1'));
toolbox.router.get('multiple/match/:foo', respondString('2'));


toolbox.router.get('cache/:name', toolbox.cacheOnly);
toolbox.router.post('cache/:name', function(request) {
return Promise.all([request.text(), caches.open(toolbox.options.cacheName)]).then(function(params) {
var text = params[0], cache = params[1];
return cache.put(request.url, new Response(text));
}).then(respondOK, respondError);
});
toolbox.router.delete('cache/:name', function(request) {
return toolbox.uncache(request.url).then(respondOK, respondError);
});
92 changes: 92 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
'use strict';

navigator.serviceWorker.register('service-worker.js', { scope: './' });

var checkValue = function(url, value, assert, method) {
var done = assert.async();
method = method || 'get';
return fetch(url, {method: method}).then(function(response) {
return response.text();
}).then(function(text) {
assert.equal(text, value);
done();
}).catch(function(reason) {
assert.ok(false, reason);
});
};

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

QUnit.test('Default route', function(assert) {
checkValue('not/real/path', 'Default', assert);
});

QUnit.test('Absolute route', function(assert) {
checkValue(new URL('absolute/route', self.location).pathname, 'OK', assert);
});

QUnit.test('Relative route', function(assert) {
checkValue('relative/route', 'OK', assert);
});

QUnit.test('Pattern matching', function(assert) {
checkValue('matching/any/patterns', 'any', assert);
checkValue('matching/all/patterns', 'all', assert);
});

QUnit.test('Method-based matching', function(assert) {
checkValue('matches/any/method', 'OK', assert, 'get');
checkValue('matches/any/method', 'OK', assert, 'put');
checkValue('matches/any/method', 'OK', assert, 'post');
checkValue('matches/any/method', 'OK', assert, 'delete');
checkValue('matches/any/method', 'OK', assert, 'head');
checkValue('matches/any/method', 'OK', assert, 'x-custom');

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) {
// Matches both routes
checkValue('multiple/match/anything.html', '1', assert);

// Only matches second route
checkValue('multiple/match/anything', '2', assert);
});

// Testing the cache/uncache methods
QUnit.test('Caching', function(assert) {
// Construct a URL for a resource that should not already exist
var date = Date.now();
var url = 'cache/' + date;

var done = assert.async();

// Confirm that the URL cannot be fetched
var step1 = fetch(url);
// If the fetch succeeds then we have a problem
step1.then(function(response) {
assert.ok(false, 'Succeeded fetching file that shouldn\'t exist');
done();
});

// Otherwise, move on to the next check
step1.catch(function(reason) {
// Add to the cache
return fetch(url, {method: 'post', body: date + ''}).then(function(response) {
// Check that retrieving from the cache now succeeds
return checkValue(url, date, assert);
}).then(function() {
// Tidy up after ourselves
return fetch(url, {method: 'delete'});
}).catch(function(reason) {
// Catch-all error handler
assert.ok(false, 'Failed: ' + reason);
}).then(done);;
})
});
});

0 comments on commit 90abd79

Please sign in to comment.