kopia lustrzana https://gitlab.com/rysiekpl/libresilient
service-worker: a bit more tests (ref. #8)
rodzic
bc1d274f63
commit
48ecda0968
|
@ -5,20 +5,21 @@ 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);
|
||||
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", () => {
|
||||
|
@ -27,23 +28,49 @@ describe("service-worker", () => {
|
|||
global.self = new ServiceWorkerGlobalScope()
|
||||
jest.resetModules();
|
||||
self.LibResilientPlugins = new Array()
|
||||
self.LibResilientConfig = {
|
||||
plugins: {
|
||||
'fetch': {},
|
||||
'cache':{}
|
||||
},
|
||||
loggedComponents: [
|
||||
'service-worker', 'cache', 'fetch'
|
||||
]
|
||||
}
|
||||
})
|
||||
|
||||
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)=>{
|
||||
|
@ -59,5 +86,54 @@ describe("service-worker", () => {
|
|||
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" })
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
@ -387,6 +387,7 @@ let callOnLibResilientPlugin = (call, args) => {
|
|||
if (typeof self.LibResilientPlugins[i][call] === 'function') {
|
||||
self.log('service-worker', 'Calling plugin ' + self.LibResilientPlugins[i].name + '.' + call + '()')
|
||||
// call it
|
||||
// TODO: check if args is an Array?
|
||||
return self.LibResilientPlugins[i][call].apply(null, args)
|
||||
}
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue