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

Adding tests for dynamicUrlToDependency #58

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -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
}
}
4 changes: 2 additions & 2 deletions demo/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down
5 changes: 3 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
});
});

Expand Down
4 changes: 2 additions & 2 deletions lib/sw-precache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions test/data/templates/a.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Template: A</h1>
1 change: 1 addition & 0 deletions test/data/templates/b.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Template: B</h1>
86 changes: 86 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});