Skip to content

Commit

Permalink
Call new on the corresponding Plugin class. (#1216)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffposnick authored Jan 23, 2018
1 parent 093649b commit b312073
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
25 changes: 19 additions & 6 deletions infra/testing/validator/service-worker-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,29 @@ const sinon = require('sinon');
const vm = require('vm');

function setupSpiesAndContext() {
const cacheableResponsePluginSpy = sinon.spy();
class CacheableResponsePlugin {
constructor(...args) {
cacheableResponsePluginSpy(...args);
}
}

const cacheExpirationPluginSpy = sinon.spy();
class CacheExpirationPlugin {
constructor(...args) {
cacheExpirationPluginSpy(...args);
}
}

const importScripts = sinon.spy();

const workbox = {
// To make testing easier, return the name of the plugin.
cacheableResponse: {
Plugin: sinon.stub().returns('workbox.cacheableResponse.Plugin'),
Plugin: CacheableResponsePlugin,
},
clientsClaim: sinon.spy(),
// To make testing easier, return the name of the plugin.
expiration: {
Plugin: sinon.stub().returns('workbox.expiration.Plugin'),
Plugin: CacheExpirationPlugin,
},
precaching: {
precacheAndRoute: sinon.spy(),
Expand Down Expand Up @@ -44,8 +57,8 @@ function setupSpiesAndContext() {

const methodsToSpies = {
importScripts,
cacheableResponsePlugin: workbox.cacheableResponse.Plugin,
cacheExpirationPlugin: workbox.expiration.Plugin,
cacheableResponsePlugin: cacheableResponsePluginSpy,
cacheExpirationPlugin: cacheExpirationPluginSpy,
cacheFirst: workbox.strategies.cacheFirst,
clientsClaim: workbox.clientsClaim,
networkFirst: workbox.strategies.networkFirst,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function getOptionsString(options = {}) {
throw new Error(`${errors['bad-runtime-caching-config']} ${pluginName}`);
}

plugins.push(`${pluginString}(${JSON.stringify(pluginConfig)})`);
plugins.push(`new ${pluginString}(${JSON.stringify(pluginConfig)})`);
}

if (networkTimeoutSeconds || cacheName || plugins.length > 0) {
Expand Down
7 changes: 3 additions & 4 deletions test/workbox-build/node/entry-points/generate-sw-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,7 @@ describe(`[workbox-build] entry-points/generate-sw-string.js (End to End)`, func
[STRING_HANDLER]: [[{
cacheName: runtimeCachingOptions.cacheName,
plugins: runtimeCachingOptions.plugins.concat([
'workbox.expiration.Plugin',
'workbox.cacheableResponse.Plugin',
{}, {},
]),
}]],
cacheableResponsePlugin: [[runtimeCachingOptions.cacheableResponse]],
Expand Down Expand Up @@ -436,10 +435,10 @@ describe(`[workbox-build] entry-points/generate-sw-string.js (End to End)`, func
await validateServiceWorkerRuntime({swCode, expectedMethodCalls: {
[STRING_HANDLER]: [[{
cacheName: firstRuntimeCachingOptions.cacheName,
plugins: ['workbox.expiration.Plugin'],
plugins: [{}],
}], [{
cacheName: secondRuntimeCachingOptions.cacheName,
plugins: ['workbox.cacheableResponse.Plugin'],
plugins: [{}],
}]],
cacheableResponsePlugin: [[secondRuntimeCachingOptions.cacheableResponse]],
cacheExpirationPlugin: [[firstRuntimeCachingOptions.expiration]],
Expand Down
4 changes: 2 additions & 2 deletions test/workbox-build/node/entry-points/generate-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,10 @@ describe(`[workbox-build] entry-points/generate-sw.js (End to End)`, function()
await validateServiceWorkerRuntime({swFile: swDest, expectedMethodCalls: {
[STRING_HANDLER]: [[{
cacheName: firstRuntimeCachingOptions.cacheName,
plugins: ['workbox.expiration.Plugin'],
plugins: [{}],
}], [{
cacheName: secondRuntimeCachingOptions.cacheName,
plugins: ['workbox.cacheableResponse.Plugin'],
plugins: [{}],
}]],
cacheableResponsePlugin: [[secondRuntimeCachingOptions.cacheableResponse]],
cacheExpirationPlugin: [[firstRuntimeCachingOptions.expiration]],
Expand Down

0 comments on commit b312073

Please sign in to comment.