libresilient/__tests__/service-worker.test.js

163 wiersze
5.3 KiB
JavaScript

const makeServiceWorkerEnv = require('service-worker-mock');
global.fetch = require('node-fetch');
jest.mock('node-fetch')
global.fetch.mockImplementation((url, init) => {
return Promise.resolve(
new Response(
new Blob(
[JSON.stringify({ test: "success" })],
{type: "application/json"}
),
{
status: 200,
statusText: "OK",
headers: {
'ETag': 'TestingETagHeader'
},
url: url
})
);
});
describe("service-worker", () => {
beforeEach(() => {
Object.assign(global, makeServiceWorkerEnv());
global.self = new ServiceWorkerGlobalScope()
jest.resetModules();
self.LibResilientPlugins = new Array()
})
test("basic set-up: LibResilientPlugins", async () => {
self.LibResilientPlugins = false
self.LibResilientConfig = {
plugins: {
},
loggedComponents: [
'service-worker'
]
}
require("../service-worker.js");
expect(self.LibResilientPlugins).toBeInstanceOf(Array)
})
test("basic set-up: LibResilientConfig", async () => {
self.LibResilientConfig = null
try {
require("../service-worker.js");
} catch(e) {}
expect(typeof self.LibResilientConfig).toEqual('object')
expect(self.LibResilientConfig.defaultPluginTimeout).toBe(10000)
expect(typeof self.LibResilientConfig.plugins).toEqual('object')
expect(self.LibResilientConfig.loggedComponents).toBeInstanceOf(Array)
})
test("fetching content should work", async () => {
self.LibResilientConfig = {
plugins: {
'fetch': {}
},
loggedComponents: [
'service-worker', 'fetch'
]
}
require("../plugins/fetch.js");
require("../service-worker.js");
await self.trigger('install')
await self.trigger('activate')
var response = await self.trigger('fetch', new Request('/test.json'))
expect(fetch).toHaveBeenCalled();
expect(await response.json()).toEqual({ test: "success" })
expect(response.headers.has('X-LibResilient-Method')).toEqual(true)
expect(response.headers.get('X-LibResilient-Method')).toEqual('fetch')
expect(response.headers.has('X-LibResilient-Etag')).toEqual(true)
expect(response.headers.get('X-LibResilient-ETag')).toEqual('TestingETagHeader')
});
test("stashing content after a successful fetch should work", async () => {
self.LibResilientConfig = {
plugins: {
'fetch': {},
'cache': {}
},
loggedComponents: [
'service-worker', 'fetch', 'cache'
]
}
require("../plugins/fetch.js");
require("../plugins/cache.js");
require("../service-worker.js");
await self.trigger('install')
await self.trigger('activate')
var response = await self.trigger('fetch', new Request('/test.json'))
expect(await response.json()).toEqual({ test: "success" })
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" })
});
test("stashing 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" })
});
});