From a85b3cb5d46c79f9df94f4175db59f4c088115ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=27rysiek=27=20Wo=C5=BAniak?= Date: Sat, 4 Sep 2021 18:33:33 +0000 Subject: [PATCH] service-worker: two more tests - POST requests, unstashing (ref. #8) --- __tests__/service-worker.test.js | 101 +++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/__tests__/service-worker.test.js b/__tests__/service-worker.test.js index d4e57dd..d309762 100644 --- a/__tests__/service-worker.test.js +++ b/__tests__/service-worker.test.js @@ -109,6 +109,46 @@ describe("service-worker", () => { expect(response.headers.get('X-LibResilient-ETag')).toEqual('TestingETagHeader') }); + test("making a POST request should work and not go through the plugins", async () => { + global.fetch.mockImplementation((request, init) => { + return Promise.resolve( + new Response( + new Blob( + [JSON.stringify({ test: "success" })], + {type: "application/json"} + ), + { + status: 200, + statusText: "OK", + headers: { + 'ETag': 'TestingETagHeader' + }, + method: 'POST', + url: request.url + }) + ); + }); + self.LibResilientConfig = { + plugins: { + 'reject-all': {} + }, + loggedComponents: [ + 'service-worker' + ] + } + self.LibResilientPlugins.push({ + name: 'reject-all', + description: 'Reject all requests.', + version: '0.0.1', + fetch: (request, init)=>{ return Promise.reject(request); } + }) + require("../service-worker.js"); + var response = await self.trigger('fetch', new Request('/test.json', {method: "POST"})) + console.log(response) + expect(response.method).toEqual('POST') + expect(await response.json()).toEqual({ test: "success" }) + }) + test("stashing content after a successful fetch should work", async () => { self.LibResilientConfig = { plugins: { @@ -190,5 +230,66 @@ describe("service-worker", () => { return json })).toEqual({ test: "success" }) }); + + test("unstashing content explicitly should work", async () => { + self.LibResilientConfig = { + plugins: { + 'cache': {} + }, + loggedComponents: [ + 'service-worker', 'cache' + ] + } + require("../plugins/cache.js"); + require("../service-worker.js"); + + await self.trigger('install') + await self.trigger('activate') + + await self.trigger( + 'message', + { + data:{ + stash: [new Response( + new Blob( + [JSON.stringify({ test: "success" })], + {type: "application/json"} + ), + { + status: 200, + statusText: "OK", + headers: { + 'ETag': 'TestingETagHeader' + }, + url: self.location.origin + '/test.json' + })] + } + }) + + expect (await caches.open('v1').then((cache)=>{ + return cache.keys() + }).then((keys)=>{ + return keys[0].url + })).toEqual(self.location.origin + '/test.json') + expect (await caches.open('v1').then((cache)=>{ + return cache.match(self.location.origin + '/test.json') + }).then((response)=>{ + return response.json() + }).then((json)=>{ + return json + })).toEqual({ test: "success" }) + + // now unstash + await self.trigger( + 'message', + { + data:{ + unstash: [self.location.origin + '/test.json'] + } + }) + expect (await caches.open('v1').then((cache)=>{ + return cache.keys() + })).toEqual([]) + }); });