cache plugin tests: first actual functional test passes (ref. #8)

merge-requests/1/head
Michał 'rysiek' Woźniak 2021-08-30 14:00:37 +00:00
rodzic 4730361bad
commit fb98acc396
4 zmienionych plików z 56 dodań i 1 usunięć

Wyświetl plik

@ -1,4 +1,29 @@
/**
* @jest-environment jsdom
*/
const makeServiceWorkerEnv = require('service-worker-mock');
const cache = require('cache-polyfill')
global.fetch = require('node-fetch');
jest.mock('node-fetch')
global.fetch.mockImplementation((url, init) => {
const response = new Response(
new Blob(
[JSON.stringify({ test: "success" })],
{type: "application/json"}
),
{
status: 200,
statusText: "OK",
headers: {
'ETag': 'TestingETagHeader'
},
url: url
});
return Promise.resolve(response);
});
describe("plugin: cache", () => {
beforeEach(() => {
@ -10,9 +35,32 @@ describe("plugin: cache", () => {
'cache':{}
}
}
self.log = function(component, ...items) {
console.debug(component + ' :: ', ...items)
}
})
test("it should register in LibResilientPlugins", () => {
require("../../plugins/cache.js");
expect(self.LibResilientPlugins[0].name).toEqual('cache');
});
test("it should stash successfully", () => {
require("../../plugins/cache.js");
expect.assertions(7);
return self.LibResilientPlugins[0].stash('https://resilient.is/test.json').then((result)=>{
expect(result).toBe(undefined)
return self.LibResilientPlugins[0].fetch('https://resilient.is/test.json')
}).then(fetchResult => {
expect(fetchResult.status).toEqual(200)
expect(fetchResult.statusText).toEqual('OK')
expect(fetchResult.url).toEqual('https://resilient.is/test.json')
expect(fetchResult.headers.has('Etag')).toEqual(true)
expect(fetchResult.headers.get('ETag')).toEqual('TestingETagHeader')
return fetchResult.json().then(json => {
expect(json).toEqual({ test: "success" })
})
})
});
});

6
package-lock.json wygenerowano
Wyświetl plik

@ -1095,6 +1095,12 @@
"integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==",
"dev": true
},
"cache-polyfill": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/cache-polyfill/-/cache-polyfill-1.0.1.tgz",
"integrity": "sha512-bIMkvMYuXvOOUMoxUChREYXT6OZi4kvPcRQrmEyaPkLR21sLwDfhdhxxSifZmoiPGQHNEdbeuVNtO5oXbSP31Q==",
"dev": true
},
"callsites": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz",

Wyświetl plik

@ -1,5 +1,6 @@
{
"devDependencies": {
"cache-polyfill": "^1.0.1",
"eslint": "^7.32.0",
"jest": "^27.0.6",
"node-fetch": "^2.6.1",

Wyświetl plik

@ -57,7 +57,7 @@
.then((cache) => {
if (typeof resource === 'string') {
// assume URL
self.log(config.name, "caching an URL")
self.log(config.name, "caching an URL: " + resource)
return cache.add(resource)
} else if (Array.isArray(resource)) {
// assume array of URLs