Skip to content

Commit

Permalink
Using cache name as idb name
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Gaunt committed Mar 5, 2018
1 parent f5792fd commit 70b4579
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
7 changes: 5 additions & 2 deletions packages/workbox-precaching/models/PrecachedDetailsModel.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ class PrecachedDetailsModel {
/**
* Construct a new model for a specific cache.
*
* @param {string} dbName
* @private
*/
constructor() {
this._db = new DBWrapper(`workbox-precaching`, 2, {
constructor(dbName) {
// This ensures the db name contains only letters, numbers, '-', '_' and '$'
const filteredDBName = dbName.replace(/[^\w-]/g, '_');
this._db = new DBWrapper(filteredDBName, 2, {
onupgradeneeded: this._handleUpgrade,
});
}
Expand Down
6 changes: 4 additions & 2 deletions test/workbox-precaching/integration/precache-and-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const activateAndControlSW = require('../../../infra/testing/activate-and-contro
const cleanSWEnv = require('../../../infra/testing/clean-sw');

describe(`[workbox-precaching] Precache and Update`, function() {
const DB_NAME = 'workbox-precache-http___localhost_3004_test_workbox-precaching_static_precache-and-update_';

let testServerAddress = global.__workbox.server.getAddress();
const testingUrl = `${testServerAddress}/test/workbox-precaching/static/precache-and-update/`;

Expand Down Expand Up @@ -62,7 +64,7 @@ describe(`[workbox-precaching] Precache and Update`, function() {
'http://localhost:3004/test/workbox-precaching/static/precache-and-update/styles/index.css',
]);

let savedIDBData = await getIdbData();
let savedIDBData = await getIdbData(DB_NAME);
expect(savedIDBData).to.deep.equal([
{
revision: '1',
Expand Down Expand Up @@ -126,7 +128,7 @@ describe(`[workbox-precaching] Precache and Update`, function() {
'http://localhost:3004/test/workbox-precaching/static/precache-and-update/new-request.txt',
]);

savedIDBData = await getIdbData();
savedIDBData = await getIdbData(DB_NAME);
expect(savedIDBData).to.deep.equal([
{
revision: '2',
Expand Down
22 changes: 11 additions & 11 deletions test/workbox-precaching/node/models/test-PrecachedDetailsModel.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() {

describe('constructor', function() {
it(`should construct with no input`, async function() {
new PrecachedDetailsModel();
new PrecachedDetailsModel(`test-idb-name`);
});
});

Expand All @@ -34,7 +34,7 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() {
createObjectStore: sandbox.spy(),
};

const precacheDetailsModel = new PrecachedDetailsModel();
const precacheDetailsModel = new PrecachedDetailsModel(`test-idb-name`);
precacheDetailsModel._handleUpgrade({
target: {
result: fakeDB,
Expand All @@ -58,7 +58,7 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() {
createObjectStore: sandbox.spy(),
};

const precacheDetailsModel = new PrecachedDetailsModel();
const precacheDetailsModel = new PrecachedDetailsModel(`test-idb-name`);
precacheDetailsModel._handleUpgrade({
oldVersion: 1,
target: {
Expand All @@ -84,7 +84,7 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() {
createObjectStore: sandbox.spy(),
};

const precacheDetailsModel = new PrecachedDetailsModel();
const precacheDetailsModel = new PrecachedDetailsModel(`test-idb-name`);
precacheDetailsModel._handleUpgrade({
oldVersion: 1,
target: {
Expand All @@ -103,13 +103,13 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() {

describe('_getAllEntries', function() {
it(`should return an empty array`, async function() {
const model = new PrecachedDetailsModel(`test-cache-name`);
const model = new PrecachedDetailsModel(`test-idb-name`);
const allEntries = await model._getAllEntries();
expect(allEntries).to.deep.equal([]);
});

it(`should return entry with ID`, async function() {
const model = new PrecachedDetailsModel(`test-cache-name`);
const model = new PrecachedDetailsModel(`test-idb-name`);
await model._addEntry(new PrecacheEntry(
{}, '/', '1234', true
));
Expand All @@ -130,7 +130,7 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() {
// TODO Test bad inputs

it(`should return false for non-existant entry`, async function() {
const model = new PrecachedDetailsModel();
const model = new PrecachedDetailsModel(`test-idb-name`);
const isCached = await model._isEntryCached(
'test-cache',
new PrecacheEntry(
Expand All @@ -143,7 +143,7 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() {
it(`should return false for entry with different revision`, async function() {
const cacheName = 'test-cache';

const model = new PrecachedDetailsModel();
const model = new PrecachedDetailsModel(`test-idb-name`);

await model._addEntry(
new PrecacheEntry(
Expand All @@ -163,7 +163,7 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() {
it(`should return false for entry with revision but not in cache`, async function() {
const cacheName = 'test-cache';

const model = new PrecachedDetailsModel();
const model = new PrecachedDetailsModel(`test-idb-name`);
const entry = new PrecacheEntry(
{}, '/', '1234', true
);
Expand All @@ -177,7 +177,7 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() {
it(`should return true if entry with revision and in cache`, async function() {
const cacheName = 'test-cache';

const model = new PrecachedDetailsModel();
const model = new PrecachedDetailsModel(`test-idb-name`);
const entry = new PrecacheEntry(
{}, '/', '1234', true
);
Expand All @@ -196,7 +196,7 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() {
// TODO add bad input tests

it(`should be able to delete an entry`, async function() {
const model = new PrecachedDetailsModel();
const model = new PrecachedDetailsModel(`test-idb-name`);

await model._addEntry(
new PrecacheEntry(
Expand Down
8 changes: 4 additions & 4 deletions test/workbox-precaching/utils/getPrecachedIDBData.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Get the data for precached details from IDB via the window.
module.exports = () => {
return global.__workbox.webdriver.executeAsyncScript((cb) => {
const request = indexedDB.open('workbox-precaching');
module.exports = (dbName) => {
return global.__workbox.webdriver.executeAsyncScript((dbName, cb) => {
const request = indexedDB.open(dbName);
request.onerror = function(event) {
cb('Error opening indexedDB');
};
Expand All @@ -18,5 +18,5 @@ module.exports = () => {
cb(event.target.result);
};
};
});
}, dbName);
};

0 comments on commit 70b4579

Please sign in to comment.