kopia lustrzana https://gitlab.com/rysiekpl/libresilient
service-worker tests for propagating the Request() init data to plugins (ref. #18)
rodzic
28b0368569
commit
e3e811e29d
|
@ -169,6 +169,76 @@ describe("service-worker", () => {
|
|||
expect(await response.json()).toEqual({ test: "success" })
|
||||
});
|
||||
|
||||
test("plugins should receive the Request() init data", async () => {
|
||||
self.LibResilientConfig = {
|
||||
plugins: [{
|
||||
name: 'reject-all'
|
||||
},{
|
||||
name: 'resolve-all'
|
||||
}],
|
||||
loggedComponents: [
|
||||
'service-worker'
|
||||
]
|
||||
}
|
||||
let rejectingFetch = jest.fn((request, init)=>{ return Promise.reject(request); })
|
||||
let resolvingFetch = jest.fn((request, init)=>{
|
||||
return Promise.resolve(
|
||||
new Response(
|
||||
new Blob(
|
||||
[JSON.stringify({ test: "success" })],
|
||||
{type: "application/json"}
|
||||
),
|
||||
{
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
headers: {
|
||||
'ETag': 'TestingETagHeader'
|
||||
},
|
||||
url: self.location.origin + '/test.json'
|
||||
})
|
||||
)
|
||||
})
|
||||
global.LibResilientPluginConstructors.set('reject-all', ()=>{
|
||||
return {
|
||||
name: 'reject-all',
|
||||
description: 'Reject all requests.',
|
||||
version: '0.0.1',
|
||||
fetch: rejectingFetch
|
||||
}
|
||||
})
|
||||
global.LibResilientPluginConstructors.set('resolve-all', ()=>{
|
||||
return {
|
||||
name: 'resolve-all',
|
||||
description: 'Resolve all requests.',
|
||||
version: '0.0.1',
|
||||
fetch: resolvingFetch
|
||||
}
|
||||
})
|
||||
|
||||
var initTest = {
|
||||
method: "GET",
|
||||
headers: new Headers({"x-stub": "STUB"}),
|
||||
mode: "mode-stub",
|
||||
credentials: "credentials-stub",
|
||||
cache: "cache-stub",
|
||||
referrer: "referrer-stub",
|
||||
// these are not implemented by service-worker-mock
|
||||
// https://github.com/zackargyle/service-workers/blob/master/packages/service-worker-mock/models/Request.js#L20
|
||||
redirect: undefined,
|
||||
integrity: undefined,
|
||||
cache: undefined
|
||||
}
|
||||
|
||||
require("../service-worker.js");
|
||||
|
||||
var response = await self.trigger('fetch', new Request('/test.json', initTest))
|
||||
expect(rejectingFetch).toHaveBeenCalled();
|
||||
expect(resolvingFetch).toHaveBeenCalled();
|
||||
expect(await response.json()).toEqual({ test: "success" })
|
||||
expect(rejectingFetch).toHaveBeenCalledWith('https://www.test.com/test.json', initTest)
|
||||
expect(resolvingFetch).toHaveBeenCalledWith('https://www.test.com/test.json', initTest)
|
||||
});
|
||||
|
||||
test("defaultPluginTimeout should be respected", async () => {
|
||||
jest.useFakeTimers()
|
||||
self.LibResilientConfig = {
|
||||
|
@ -575,6 +645,110 @@ describe("service-worker", () => {
|
|||
})).toEqual({ test: "success" })
|
||||
});
|
||||
|
||||
test("after a retrieval from a stashing plugin, background plugin should receive the Request() init data", async () => {
|
||||
self.LibResilientConfig = {
|
||||
plugins: [{
|
||||
name: 'stashing-test'
|
||||
},{
|
||||
name: 'resolve-all'
|
||||
}],
|
||||
loggedComponents: [
|
||||
'service-worker'
|
||||
]
|
||||
}
|
||||
let resolvingFetch = jest.fn((request, init)=>{
|
||||
return Promise.resolve(
|
||||
new Response(
|
||||
new Blob(
|
||||
[JSON.stringify({ test: "success" })],
|
||||
{type: "application/json"}
|
||||
),
|
||||
{
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
headers: {
|
||||
'X-LibResilient-Method': 'resolve-all',
|
||||
'X-LibResilient-ETag': 'TestingETagHeader'
|
||||
},
|
||||
url: self.location.origin + '/test.json'
|
||||
})
|
||||
)
|
||||
})
|
||||
let resolvingFetch2 = jest.fn((request, init)=>{
|
||||
return Promise.resolve(
|
||||
new Response(
|
||||
new Blob(
|
||||
[JSON.stringify({ test: "success2" })],
|
||||
{type: "application/json"}
|
||||
),
|
||||
{
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
headers: {
|
||||
'ETag': 'NewTestingETagHeader'
|
||||
},
|
||||
url: self.location.origin + '/test.json'
|
||||
})
|
||||
)
|
||||
})
|
||||
let stashingStash = jest.fn(async (response, url)=>{
|
||||
expect(await response.json()).toEqual({ test: "success2" })
|
||||
expect(response.headers.get('ETag')).toEqual('NewTestingETagHeader')
|
||||
})
|
||||
|
||||
global.LibResilientPluginConstructors.set('stashing-test', ()=>{
|
||||
return {
|
||||
name: 'stashing-test',
|
||||
description: 'Mock stashing plugin.',
|
||||
version: '0.0.1',
|
||||
fetch: resolvingFetch,
|
||||
stash: stashingStash
|
||||
}
|
||||
})
|
||||
global.LibResilientPluginConstructors.set('resolve-all', ()=>{
|
||||
return {
|
||||
name: 'resolve-all',
|
||||
description: 'Resolve all requests.',
|
||||
version: '0.0.1',
|
||||
fetch: resolvingFetch2
|
||||
}
|
||||
})
|
||||
|
||||
var testClient = new Client()
|
||||
self.clients.clients.push(testClient)
|
||||
var fetchedDiffersFound = false
|
||||
testClient.addEventListener('message', event => {
|
||||
if (event.data.fetchedDiffers) {
|
||||
fetchedDiffersFound = true
|
||||
}
|
||||
})
|
||||
|
||||
require("../service-worker.js");
|
||||
|
||||
var initTest = {
|
||||
method: "GET",
|
||||
headers: new Headers({"x-stub": "STUB"}),
|
||||
mode: "mode-stub",
|
||||
credentials: "credentials-stub",
|
||||
cache: "cache-stub",
|
||||
referrer: "referrer-stub",
|
||||
// these are not implemented by service-worker-mock
|
||||
// https://github.com/zackargyle/service-workers/blob/master/packages/service-worker-mock/models/Request.js#L20
|
||||
redirect: undefined,
|
||||
integrity: undefined,
|
||||
cache: undefined
|
||||
}
|
||||
|
||||
var response = await self.trigger('fetch', {
|
||||
request: new Request('/test.json', initTest),
|
||||
clientId: testClient.id
|
||||
})
|
||||
expect(resolvingFetch).toHaveBeenCalled();
|
||||
expect(await response.json()).toEqual({ test: "success" })
|
||||
expect(resolvingFetch).toHaveBeenCalledWith('https://www.test.com/test.json', initTest);
|
||||
expect(resolvingFetch2).toHaveBeenCalledWith('https://www.test.com/test.json', initTest);
|
||||
});
|
||||
|
||||
test("unstashing content explicitly should work", async () => {
|
||||
self.LibResilientConfig = {
|
||||
plugins: [{
|
||||
|
|
Ładowanie…
Reference in New Issue