From 8603237db03e4825a9b0deebe4bc79d381dc53b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=27rysiek=27=20Wo=C5=BAniak?= Date: Sat, 18 Sep 2021 23:35:22 +0000 Subject: [PATCH] service-worker: working on making tests run with the new plugin loader (ref. #15) --- __tests__/service-worker.test.js | 130 +++++++++++++++++-------------- service-worker.js | 6 +- 2 files changed, 74 insertions(+), 62 deletions(-) diff --git a/__tests__/service-worker.test.js b/__tests__/service-worker.test.js index fe789aa..0b2da12 100644 --- a/__tests__/service-worker.test.js +++ b/__tests__/service-worker.test.js @@ -27,13 +27,19 @@ describe("service-worker", () => { global.self = new ServiceWorkerGlobalScope() jest.resetModules(); self.LibResilientPlugins = new Array() + self.importScripts = jest.fn((url)=>{ + console.debug(`importScripts('../${url}')`) + try { + require('../' + url); + } catch(e) {} + }) + global.LibResilientPluginConstructors = new Map() }) test("basic set-up: LibResilientPlugins", async () => { self.LibResilientPlugins = false self.LibResilientConfig = { - plugins: { - }, + plugins: [], loggedComponents: [ 'service-worker' ] @@ -45,8 +51,7 @@ describe("service-worker", () => { test("Promise.any() polyfill should work", async () => { self.LibResilientPlugins = false self.LibResilientConfig = { - plugins: { - }, + plugins: [], loggedComponents: [ 'service-worker' ] @@ -87,14 +92,13 @@ describe("service-worker", () => { test("fetching content should work", async () => { self.LibResilientConfig = { - plugins: { - 'fetch': {} - }, + plugins: [{ + name: 'fetch' + }], loggedComponents: [ 'service-worker', 'fetch' ] } - require("../plugins/fetch.js"); require("../service-worker.js"); await self.trigger('install') @@ -111,10 +115,11 @@ describe("service-worker", () => { test("failed fetch by first configured plugin should not affect a successful fetch by a second one", async () => { self.LibResilientConfig = { - plugins: { - 'reject-all': {}, - 'resolve-all': {} - }, + plugins: [{ + name: 'reject-all' + },{ + name: 'resolve-all' + }], loggedComponents: [ 'service-worker' ] @@ -162,9 +167,9 @@ describe("service-worker", () => { jest.useFakeTimers() self.LibResilientConfig = { defaultPluginTimeout: 1000, - plugins: { - 'resolve-with-timeout': {} - }, + plugins: [{ + name: 'resolve-with-timeout' + }], loggedComponents: [ 'service-worker', ] @@ -214,9 +219,9 @@ describe("service-worker", () => { ); }); self.LibResilientConfig = { - plugins: { - 'reject-all': {} - }, + plugins: [{ + name: 'reject-all' + }], loggedComponents: [ 'service-worker' ] @@ -252,9 +257,9 @@ describe("service-worker", () => { ); }); self.LibResilientConfig = { - plugins: { - 'reject-all': {} - }, + plugins: [{ + name: 'reject-all' + }], loggedComponents: [ 'service-worker' ] @@ -273,16 +278,15 @@ describe("service-worker", () => { test("stashing content after a successful fetch should work", async () => { self.LibResilientConfig = { - plugins: { - 'fetch': {}, - 'cache': {} - }, + plugins: [{ + name: 'fetch' + },{ + name: 'cache' + }], loggedComponents: [ 'service-worker', 'fetch', 'cache' ] } - require("../plugins/fetch.js"); - require("../plugins/cache.js"); require("../service-worker.js"); await self.trigger('install') @@ -306,10 +310,11 @@ describe("service-worker", () => { test("stashing should be skipped if content was retrieved from a stashing plugin", async () => { self.LibResilientConfig = { - plugins: { - 'stashing-test': {}, - 'reject-all': {} - }, + plugins: [{ + name: 'stashing-test' + },{ + name: 'reject-all' + }], loggedComponents: [ 'service-worker' ] @@ -360,10 +365,11 @@ describe("service-worker", () => { test("content should be stashed if it was retrieved from a job after retrieval from a stashing plugin, and it differs from the stashed version", async () => { self.LibResilientConfig = { - plugins: { - 'stashing-test': {}, - 'resolve-all': {} - }, + plugins: [{ + name: 'stashing-test' + },{ + name: 'resolve-all' + }], loggedComponents: [ 'service-worker' ] @@ -446,10 +452,11 @@ describe("service-worker", () => { test("content should be stashed if it was retrieved from a job after retrieval from a stashing plugin, even it does not differ from the stashed version", async () => { self.LibResilientConfig = { - plugins: { - 'stashing-test': {}, - 'resolve-all': {} - }, + plugins: [{ + name: 'stashing-test' + },{ + name: 'resolve-all' + }], loggedComponents: [ 'service-worker' ] @@ -498,14 +505,13 @@ describe("service-worker", () => { test("stashing content explicitly should work", async () => { self.LibResilientConfig = { - plugins: { - 'cache': {} - }, + plugins: [{ + name: 'cache' + }], loggedComponents: [ 'service-worker', 'cache' ] } - require("../plugins/cache.js"); require("../service-worker.js"); await self.trigger('install') @@ -547,14 +553,13 @@ describe("service-worker", () => { test("unstashing content explicitly should work", async () => { self.LibResilientConfig = { - plugins: { - 'cache': {} - }, + plugins: [{ + name: 'cache' + }], loggedComponents: [ 'service-worker', 'cache' ] } - require("../plugins/cache.js"); require("../service-worker.js"); await self.trigger('install') @@ -608,9 +613,9 @@ describe("service-worker", () => { test("publishing content explicitly should work (stub)", async () => { self.LibResilientConfig = { - plugins: { - 'publish-test': {} - }, + plugins: [{ + name: 'publish-test' + }], loggedComponents: [ 'service-worker' ] @@ -649,10 +654,15 @@ describe("service-worker", () => { test("using plugins with dependencies should work", async () => { self.LibResilientConfig = { - plugins: { - 'dependent-test': { - } - }, + plugins: [{ + // TODO: actually implement the test? + name: 'dependent-test', + uses: [{ + name: 'dependency1-test' + },{ + name: 'dependency2-test' + }] + }], loggedComponents: [ 'service-worker' ] @@ -684,9 +694,9 @@ describe("service-worker", () => { test("should error out if all plugins fail", async () => { self.LibResilientConfig = { - plugins: { - 'reject-all': {} - }, + plugins: [{ + name: 'reject-all' + }], loggedComponents: [ 'service-worker' ] @@ -708,9 +718,9 @@ describe("service-worker", () => { test("should send clientId back if event.resultingClientId is set", async () => { self.LibResilientConfig = { - plugins: { - 'resolve-all': {} - }, + plugins: [{ + name: 'resolve-all' + }], loggedComponents: [ 'service-worker' ] diff --git a/service-worker.js b/service-worker.js index ff066ce..e4365de 100644 --- a/service-worker.js +++ b/service-worker.js @@ -113,7 +113,9 @@ try { self.log('service-worker', 'config loaded.') // create the LibResilientPluginConstructors map - var LibResilientPluginConstructors = new Map() + // the global... hack is here so that we can run tests; not the most elegant + // TODO: find a better way + var LibResilientPluginConstructors = global.LibResilientPluginConstructors || new Map() // this is the stash for plugins that need dependencies instantiated first var dependentPlugins = new Array() @@ -157,8 +159,8 @@ try { do { // instantiate the plugin - self.log('service-worker', `${pluginConfig.name}: instantiated`) let plugin = LibResilientPluginConstructors.get(pluginConfig.name)(self, pluginConfig) + self.log('service-worker', `${pluginConfig.name}: instantiated`) // do we have a stashed plugin that requires dependencies? if (dependentPlugins.length === 0) {