From 900b7e4c707e44e431ba8fa148124161e4fe9146 Mon Sep 17 00:00:00 2001 From: Matt Gaunt Date: Thu, 26 Nov 2015 13:23:41 +0000 Subject: [PATCH 1/2] Adding tests for dynamicUrlToDependency --- test/data/templates/a.handlebars | 1 + test/data/templates/b.handlebars | 1 + test/test.js | 86 ++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 test/data/templates/a.handlebars create mode 100644 test/data/templates/b.handlebars diff --git a/test/data/templates/a.handlebars b/test/data/templates/a.handlebars new file mode 100644 index 0000000..1b760f3 --- /dev/null +++ b/test/data/templates/a.handlebars @@ -0,0 +1 @@ +

Template: A

diff --git a/test/data/templates/b.handlebars b/test/data/templates/b.handlebars new file mode 100644 index 0000000..0ddffb9 --- /dev/null +++ b/test/data/templates/b.handlebars @@ -0,0 +1 @@ +

Template: B

diff --git a/test/test.js b/test/test.js index da0f768..11c0690 100644 --- a/test/test.js +++ b/test/test.js @@ -388,3 +388,89 @@ describe('addDirectoryIndex', function() { done(); }); }); + +describe('dynamicUrlToDependencies', function() { + var SW_FILE = 'test/output/dynamicUrlToDependencies_sw.js'; + + it('should not throw an error when using dynamicUrlToDependencies', function(done) { + var config = { + logger: NOOP, + dynamicUrlToDependencies: { + '/fake': [ + 'test/data/templates/a.handlebars' + ] + } + }; + + write(SW_FILE, config, function(error) { + assert.ifError(error); + fs.stat(SW_FILE, function(error, stats) { + assert.ifError(error); + assert(stats.isFile(), 'file exists'); + assert(stats.size > 0, 'file contains data'); + done(); + }); + }); + }); + + it('should not throw an error for multiple files in a dynamicUrlToDependency', function(done) { + var config = { + logger: NOOP, + dynamicUrlToDependencies: { + '/fake': [ + 'test/data/templates/a.handlebars', + 'test/data/templates/b.handlebars' + ] + } + }; + + write(SW_FILE, config, function(error) { + assert.ifError(error); + fs.stat(SW_FILE, function(error, stats) { + assert.ifError(error); + assert(stats.isFile(), 'file exists'); + assert(stats.size > 0, 'file contains data'); + done(); + }); + }); + }); + + it('should throw an error for an invalid file in a dynamicUrlToDependency', function(done) { + var config = { + logger: NOOP, + dynamicUrlToDependencies: { + '/fake': [ + 'test/data/templates/non-existant.handlebars' + ] + } + }; + + write(SW_FILE, config, function(error) { + assert(error); + done(); + }); + }); + + it('should throw an error for one non existant file in an array in dynamicUrlToDependency', function(done) { + var config = { + logger: NOOP, + dynamicUrlToDependencies: { + '/fake': [ + 'test/data/templates/a.handlebars', + 'test/data/templates/b.handlebars', + 'test/data/templates/non-existant.handlebars' + ] + } + }; + + write(SW_FILE, config, function(error) { + assert(error); + done(); + }); + }); + + after(function() { + fs.unlinkSync(SW_FILE); + fs.rmdirSync(path.dirname(SW_FILE)); + }); +}); From ce00555d6dd071be62a7667ebb877f84307d1242 Mon Sep 17 00:00:00 2001 From: Matt Gaunt Date: Mon, 30 Nov 2015 14:26:47 +0000 Subject: [PATCH 2/2] Fixing up linting of javascript files and eslint errors --- .eslintrc | 11 +++++++++-- demo/gulpfile.js | 4 ++-- gulpfile.js | 5 +++-- lib/sw-precache.js | 4 ++-- package.json | 4 ++-- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/.eslintrc b/.eslintrc index bff4dbe..7aae167 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,10 +1,17 @@ { - "extends": "xo", + "extends": "./node_modules/eslint-config-xo/index.js", "rules": { "default-case": 0, "indent": [2, 2, {"SwitchCase": 1}], "no-console": 0, "no-inline-comments": 0, - "space-before-function-paren": [2, "never"] + "space-before-function-paren": [2, "never"], + "func-style": 0, + + "no-empty-class": 0, + "no-extra-strict": 0, + "no-reserved-keys": 0, + "no-space-before-semi": 0, + "no-wrap-func": 0 } } diff --git a/demo/gulpfile.js b/demo/gulpfile.js index eea49f5..8d1a0b1 100644 --- a/demo/gulpfile.js +++ b/demo/gulpfile.js @@ -28,8 +28,8 @@ function runExpress(port, rootDir) { var server = app.listen(port, function() { var host = server.address().address; - var port = server.address().port; - console.log('Server running at http://%s:%s', host, port); + var serverPort = server.address().port; + console.log('Server running at http://%s:%s', host, serverPort); }); } diff --git a/gulpfile.js b/gulpfile.js index b5e337f..c548fc0 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -14,7 +14,7 @@ gulp.task('generate-demo-service-worker', function(callback) { }); gulp.task('lint', ['generate-demo-service-worker'], function() { - return gulp.src(['{lib,test,demo}/**/*.js', '*.js']) + return gulp.src(['{lib,demo}/**/*.js', '*.js']) .pipe($.eslint()) .pipe($.eslint.format()) .pipe($.eslint.failOnError()); @@ -25,7 +25,8 @@ gulp.task('test', function() { .pipe($.mocha()) .on('error', function(error) { console.error(error); - process.exit(1); + // process.exit(1); + throw error; }); }); diff --git a/lib/sw-precache.js b/lib/sw-precache.js index 4cbfa59..547ab28 100644 --- a/lib/sw-precache.js +++ b/lib/sw-precache.js @@ -67,9 +67,9 @@ function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } -function generate(params, callback) { +function generate(inputParams, callback) { return new Promise(function(resolve, reject) { - params = defaults(params || {}, { + var params = defaults(inputParams || {}, { cacheId: '', directoryIndex: 'index.html', dynamicUrlToDependencies: {}, diff --git a/package.json b/package.json index 32218dc..b410737 100644 --- a/package.json +++ b/package.json @@ -21,8 +21,8 @@ }, "devDependencies": { "del": "^1.2.0", - "eslint": "^1.9.0", - "eslint-config-xo": "^0.6.0", + "eslint": "^1.10.2", + "eslint-config-xo": "^0.8.0", "express": "^4.12.4", "gh-pages": "^0.3.1", "grunt": "^0.4.5",