Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precaching idb fix #1139

Merged
merged 6 commits into from
Dec 18, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
34 changes: 24 additions & 10 deletions packages/workbox-precaching/models/PrecachedDetailsModel.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,33 @@ class PrecachedDetailsModel {
constructor(cacheName) {
this._cacheName = cacheNames.getPrecacheName(cacheName);
this._db = new DBWrapper(`workbox-precaching`, 2, {
onupgradeneeded: (evt) => {
if (evt.oldVersion < 2) {
try {
evt.target.result.deleteObjectStore('workbox-precaching');
} catch (err) {
// NOOP
}
}
evt.target.result.createObjectStore(DB_STORE_NAME);
},
onupgradeneeded: this._handleUpgrade,
});
}

/**
* Should perform an upgrade of indexedDB.
*
* @param {Event} evt
*
* @private
*/
_handleUpgrade(evt) {
const db = evt.target.result;
if (evt.oldVersion < 2) {
// IndexedDB version 1 used both 'workbox-precaching' and
// 'precached-details-model' before upgrading to version 2.
// Delete them and create a new store with latest schema.
if (db.objectStoreNames.indexOf('workbox-precaching') !== -1) {
db.deleteObjectStore('workbox-precaching');
}
if (db.objectStoreNames.indexOf(DB_STORE_NAME) !== -1) {
db.deleteObjectStore(DB_STORE_NAME);
}
}
db.createObjectStore(DB_STORE_NAME);
}

/**
* Check if an entry is already cached. Returns false if
* the entry isn't cached or the revision has changed.
Expand Down
78 changes: 78 additions & 0 deletions test/workbox-precaching/node/models/test-PrecachedDetailsModel.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import {expect} from 'chai';
import sinon from 'sinon';
import {reset as iDBReset} from 'shelving-mock-indexeddb';
import {_private} from '../../../../packages/workbox-core/index.mjs';
import PrecachedDetailsModel from '../../../../packages/workbox-precaching/models/PrecachedDetailsModel.mjs';
import PrecacheEntry from '../../../../packages/workbox-precaching/models/PrecacheEntry.mjs';

describe('[workbox-precaching] PrecachedDetailsModel', function() {
const sandbox = sinon.sandbox.create();

beforeEach(function() {
sandbox.restore();
iDBReset();
});

after(function() {
sandbox.restore();
iDBReset();
});

Expand All @@ -23,6 +32,75 @@ describe('[workbox-precaching] PrecachedDetailsModel', function() {
// TODO Bad cache name input
});

describe('_handleUpgrade', function() {
it('should handle upgrade 0 (Doesnt exist)', () => {
const fakeDB = {
objectStoreNames: [],
deleteObjectStore: sandbox.spy(),
createObjectStore: sandbox.spy(),
};

const precacheDetailsModel = new PrecachedDetailsModel();
precacheDetailsModel._handleUpgrade({
target: {
result: fakeDB,
},
});

// Assert only create called
expect(fakeDB.createObjectStore.callCount).to.equal(1);
expect(fakeDB.createObjectStore.args[0][0]).to.equal(`precached-details-models`);

expect(fakeDB.deleteObjectStore.callCount).to.equal(0);
});

it('should handle upgrade 1 > 2', () => {
const fakeDB = {
objectStoreNames: ['workbox-precaching'],
deleteObjectStore: sandbox.spy(),
createObjectStore: sandbox.spy(),
};

const precacheDetailsModel = new PrecachedDetailsModel();
precacheDetailsModel._handleUpgrade({
oldVersion: 1,
target: {
result: fakeDB,
},
});

// Assert only create called
expect(fakeDB.createObjectStore.callCount).to.equal(1);
expect(fakeDB.createObjectStore.args[0][0]).to.equal(`precached-details-models`);

expect(fakeDB.deleteObjectStore.callCount).to.equal(1);
expect(fakeDB.deleteObjectStore.args[0][0]).to.equal(`workbox-precaching`);
});

it('should handle upgrade 1 > 2 with constructing precached-details-models', () => {
const fakeDB = {
objectStoreNames: ['workbox-precaching', 'precached-details-models'],
deleteObjectStore: sandbox.spy(),
createObjectStore: sandbox.spy(),
};

const precacheDetailsModel = new PrecachedDetailsModel();
precacheDetailsModel._handleUpgrade({
oldVersion: 1,
target: {
result: fakeDB,
},
});

// Assert only create called
expect(fakeDB.createObjectStore.callCount).to.equal(1);
expect(fakeDB.createObjectStore.args[0][0]).to.equal(`precached-details-models`);
expect(fakeDB.deleteObjectStore.callCount).to.equal(2);
expect(fakeDB.deleteObjectStore.args[0][0]).to.equal(`workbox-precaching`);
expect(fakeDB.deleteObjectStore.args[1][0]).to.equal(`precached-details-models`);
});
});

describe('_getAllEntries', function() {
it(`should return an empty array`, async function() {
const model = new PrecachedDetailsModel(`test-cache-name`);
Expand Down