Skip to content

Commit

Permalink
Call bind() to get the right this in precaching's plugins (#1928)
Browse files Browse the repository at this point in the history
* Call bind() to get the right this

* Added a unit test, too.

* Use thisValues
  • Loading branch information
jeffposnick committed Feb 28, 2019
1 parent d76e770 commit b6b1506
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/workbox-precaching/PrecacheController.mjs
Expand Up @@ -177,7 +177,7 @@ class PrecacheController {
let cacheWillUpdateCallback;
for (const plugin of (plugins || [])) {
if ('cacheWillUpdate' in plugin) {
cacheWillUpdateCallback = plugin.cacheWillUpdate;
cacheWillUpdateCallback = plugin.cacheWillUpdate.bind(plugin);
}
}

Expand Down
22 changes: 22 additions & 0 deletions test/workbox-precaching/node/test-PrecacheController.mjs
Expand Up @@ -441,6 +441,28 @@ describe(`[workbox-precaching] PrecacheController`, function() {
expect(cacheWrapper.put.args[1][0].plugins).to.equal(testPlugins);
});

it(`it should use the proper 'this' when calling a cacheWillUpdate plugin`, async function() {
const precacheController = new PrecacheController();
const cacheList = [
'/index.1234.html',
];
precacheController.addToCacheList(cacheList);

class TestPlugin {
cacheWillUpdate({response}) {
return response;
}
}
const pluginInstance = new TestPlugin();
const cacheWillUpdateSpy = sandbox.spy(pluginInstance, 'cacheWillUpdate');

await precacheController.install({
plugins: [pluginInstance],
});

expect(cacheWillUpdateSpy.thisValues[0]).to.be.an.instanceof(TestPlugin);
});

it(`it should set credentials: 'same-origin' on the precaching requests`, async function() {
sandbox.spy(fetchWrapper, 'fetch');

Expand Down
11 changes: 9 additions & 2 deletions test/workbox-precaching/static/precache-and-update/sw-1.js
Expand Up @@ -6,10 +6,17 @@
https://opensource.org/licenses/MIT.
*/

importScripts('/__WORKBOX/buildFile/workbox-core');
importScripts('/__WORKBOX/buildFile/workbox-precaching');
importScripts('/__WORKBOX/buildFile/workbox-sw');
importScripts('/infra/testing/comlink/sw-interface.js');

workbox.setConfig({modulePathPrefix: '/__WORKBOX/buildFile/'});

workbox.precaching.addPlugins([
new workbox.cacheableResponse.Plugin({
statuses: [200],
}),
]);

workbox.precaching.precache([
{
url: 'styles/index.css',
Expand Down

0 comments on commit b6b1506

Please sign in to comment.