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

Check idb entries in integration test #1127

Merged
merged 1 commit into from
Dec 14, 2017
Merged
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
85 changes: 63 additions & 22 deletions test/workbox-precaching/integration/precache-and-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,42 @@ describe(`[workbox-precaching] Precache and Update`, function() {
}
};

const getCachedRequests = (cacheName) => {
return webdriver.executeAsyncScript((cacheName, cb) => {
caches.open(cacheName)
.then((cache) => {
return cache.keys();
})
.then((keys) => {
cb(
keys.map((request) => request.url).sort()
);
});
}, cacheName);
};

const getIDBData = () => {
return webdriver.executeAsyncScript((cb) => {
const request = indexedDB.open('workbox-precaching');
request.onerror = function(event) {
cb('Error opening indexedDB');
};
request.onsuccess = function(event) {
const db = event.target.result;

const transaction = db.transaction(['precached-details-models'], 'readwrite');
const objectStore = transaction.objectStore('precached-details-models');
const getAllRequest = objectStore.getAll();
getAllRequest.onerror = function(event) {
cb('Error opening getting all content');
};
getAllRequest.onsuccess = function(event) {
cb(event.target.result);
};
};
});
};

it(`should load a page with service worker `, async function() {
const testingURl = `${testServerAddress}/test/workbox-precaching/static/precache-and-update/`;
const SW_1_URL = `${testingURl}sw-1.js`;
Expand Down Expand Up @@ -71,22 +107,24 @@ describe(`[workbox-precaching] Precache and Update`, function() {
]);

// Check that the cached requests are what we expect for sw-1.js
let cachedRequests = await webdriver.executeAsyncScript((cacheName, cb) => {
caches.open(cacheName)
.then((cache) => {
return cache.keys();
})
.then((keys) => {
cb(
keys.map((request) => request.url).sort()
);
});
}, keys[0]);
let cachedRequests = await getCachedRequests(keys[0]);
expect(cachedRequests).to.deep.equal([
'http://localhost:3004/test/workbox-precaching/static/precache-and-update/index.html',
'http://localhost:3004/test/workbox-precaching/static/precache-and-update/styles/index.css',
]);

let savedIDBData = await getIDBData();
expect(savedIDBData).to.deep.equal([
{
revision: '1',
url: 'http://localhost:3004/test/workbox-precaching/static/precache-and-update/index.html',
},
{
revision: '1',
url: 'http://localhost:3004/test/workbox-precaching/static/precache-and-update/styles/index.css',
},
]);

// Make sure the requested URL's include cache busting search param if needed.
let requestsMade = global.__workbox.server.getRequests();
expect(requestsMade['/test/workbox-precaching/static/precache-and-update/']).to.equal(1);
Expand All @@ -102,6 +140,7 @@ describe(`[workbox-precaching] Precache and Update`, function() {
// Request the page and check that the precached assets weren't requested from the network
global.__workbox.server.reset();
await webdriver.get(testingURl);

requestsMade = global.__workbox.server.getRequests();
expect(requestsMade['/test/workbox-precaching/static/precache-and-update/']).to.equal(undefined);
expect(requestsMade['/test/workbox-precaching/static/precache-and-update/index.html']).to.equal(undefined);
Expand Down Expand Up @@ -133,22 +172,24 @@ describe(`[workbox-precaching] Precache and Update`, function() {

// Check that the cached entries were deleted / added as expected when
// updating from sw-1.js to sw-2.js
cachedRequests = await webdriver.executeAsyncScript((cacheName, cb) => {
caches.open(cacheName)
.then((cache) => {
return cache.keys();
})
.then((keys) => {
cb(
keys.map((request) => request.url).sort()
);
});
}, keys[0]);
cachedRequests = await getCachedRequests(keys[0]);
expect(cachedRequests).to.deep.equal([
'http://localhost:3004/test/workbox-precaching/static/precache-and-update/index.html',
'http://localhost:3004/test/workbox-precaching/static/precache-and-update/new-request.txt',
]);

savedIDBData = await getIDBData();
expect(savedIDBData).to.deep.equal([
{
revision: '2',
url: 'http://localhost:3004/test/workbox-precaching/static/precache-and-update/index.html',
},
{
revision: '2',
url: 'http://localhost:3004/test/workbox-precaching/static/precache-and-update/new-request.txt',
},
]);

// Refresh the page and test that the requests are as expected
global.__workbox.server.reset();
await webdriver.get(testingURl);
Expand Down