libresilient/__tests__/service-worker.test.js

296 wiersze
9.6 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("Promise.any() polyfill should work", async () => {
self.LibResilientPlugins = false
self.LibResilientConfig = {
plugins: {
},
loggedComponents: [
'service-worker'
]
}
expect.assertions(4)
// we want to make sure to actually test the polyfill
Promise.any = undefined
expect(typeof Promise.any).toEqual('undefined')
require("../service-worker.js");
expect(typeof Promise.any).toEqual('function')
expect(await Promise.any([
Promise.resolve('test resolve 1'),
Promise.reject('test reject 2')
])).toEqual('test resolve 1')
try {
await Promise.any([
Promise.reject('test reject 1'),
Promise.reject('test reject 2')
])
} catch (e) {
expect(e).toEqual([
"test reject 1",
"test reject 2"
])
}
})
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("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: {
'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" })
});
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([])
});
});