kopia lustrzana https://gitlab.com/rysiekpl/libresilient
service-worker test added: config.js cached, no fetch needed (ref. #31)
rodzic
e7ef3bfade
commit
ab2c1249e1
|
@ -87,22 +87,22 @@ describe("service-worker", () => {
|
||||||
self.LibResilientConfig = null
|
self.LibResilientConfig = null
|
||||||
|
|
||||||
global.fetch.mockImplementation((url, init) => {
|
global.fetch.mockImplementation((url, init) => {
|
||||||
return Promise.resolve(
|
return Promise.resolve(
|
||||||
new Response(
|
new Response(
|
||||||
new Blob(
|
new Blob(
|
||||||
[JSON.stringify({ test: "fail" })],
|
[JSON.stringify({ test: "fail" })],
|
||||||
{type: "application/json"}
|
{type: "application/json"}
|
||||||
),
|
),
|
||||||
{
|
{
|
||||||
status: 404,
|
status: 404,
|
||||||
statusText: "Not Found",
|
statusText: "Not Found",
|
||||||
headers: {
|
headers: {
|
||||||
'ETag': 'TestingETagHeader'
|
'ETag': 'TestingETagHeader'
|
||||||
},
|
},
|
||||||
url: url
|
url: url
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
require("../service-worker.js");
|
require("../service-worker.js");
|
||||||
|
@ -116,28 +116,29 @@ describe("service-worker", () => {
|
||||||
expect(self.LibResilientConfig.defaultPluginTimeout).toBe(10000)
|
expect(self.LibResilientConfig.defaultPluginTimeout).toBe(10000)
|
||||||
expect(self.LibResilientConfig.plugins).toStrictEqual([{name: "fetch"},{name: "cache"}])
|
expect(self.LibResilientConfig.plugins).toStrictEqual([{name: "fetch"},{name: "cache"}])
|
||||||
expect(self.LibResilientConfig.loggedComponents).toStrictEqual(['service-worker', 'fetch', 'cache'])
|
expect(self.LibResilientConfig.loggedComponents).toStrictEqual(['service-worker', 'fetch', 'cache'])
|
||||||
|
expect(fetch).toHaveBeenCalled();
|
||||||
})
|
})
|
||||||
|
|
||||||
test("basic set-up: use default LibResilientConfig values when config.json not valid JSON", async () => {
|
test("basic set-up: use default LibResilientConfig values when config.json not valid JSON", async () => {
|
||||||
self.LibResilientConfig = null
|
self.LibResilientConfig = null
|
||||||
|
|
||||||
global.fetch.mockImplementation((url, init) => {
|
global.fetch.mockImplementation((url, init) => {
|
||||||
return Promise.resolve(
|
return Promise.resolve(
|
||||||
new Response(
|
new Response(
|
||||||
new Blob(
|
new Blob(
|
||||||
["not a JSON"],
|
["not a JSON"],
|
||||||
{type: "application/json"}
|
{type: "application/json"}
|
||||||
),
|
),
|
||||||
{
|
{
|
||||||
status: 200,
|
status: 200,
|
||||||
statusText: "OK",
|
statusText: "OK",
|
||||||
headers: {
|
headers: {
|
||||||
'ETag': 'TestingETagHeader'
|
'ETag': 'TestingETagHeader'
|
||||||
},
|
},
|
||||||
url: url
|
url: url
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
require("../service-worker.js");
|
require("../service-worker.js");
|
||||||
|
@ -151,28 +152,29 @@ describe("service-worker", () => {
|
||||||
expect(self.LibResilientConfig.defaultPluginTimeout).toBe(10000)
|
expect(self.LibResilientConfig.defaultPluginTimeout).toBe(10000)
|
||||||
expect(self.LibResilientConfig.plugins).toStrictEqual([{name: "fetch"},{name: "cache"}])
|
expect(self.LibResilientConfig.plugins).toStrictEqual([{name: "fetch"},{name: "cache"}])
|
||||||
expect(self.LibResilientConfig.loggedComponents).toStrictEqual(['service-worker', 'fetch', 'cache'])
|
expect(self.LibResilientConfig.loggedComponents).toStrictEqual(['service-worker', 'fetch', 'cache'])
|
||||||
|
expect(fetch).toHaveBeenCalled();
|
||||||
})
|
})
|
||||||
|
|
||||||
test("basic set-up: use default LibResilientConfig values when no valid 'plugins' field in config.json", async () => {
|
test("basic set-up: use default LibResilientConfig values when no valid 'plugins' field in config.json", async () => {
|
||||||
self.LibResilientConfig = null
|
self.LibResilientConfig = null
|
||||||
|
|
||||||
global.fetch.mockImplementation((url, init) => {
|
global.fetch.mockImplementation((url, init) => {
|
||||||
return Promise.resolve(
|
return Promise.resolve(
|
||||||
new Response(
|
new Response(
|
||||||
new Blob(
|
new Blob(
|
||||||
[JSON.stringify({loggedComponents: ['service-worker', 'fetch'], plugins: 'not a valid array'})],
|
[JSON.stringify({loggedComponents: ['service-worker', 'fetch'], plugins: 'not a valid array'})],
|
||||||
{type: "application/json"}
|
{type: "application/json"}
|
||||||
),
|
),
|
||||||
{
|
{
|
||||||
status: 200,
|
status: 200,
|
||||||
statusText: "OK",
|
statusText: "OK",
|
||||||
headers: {
|
headers: {
|
||||||
'ETag': 'TestingETagHeader'
|
'ETag': 'TestingETagHeader'
|
||||||
},
|
},
|
||||||
url: url
|
url: url
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
require("../service-worker.js");
|
require("../service-worker.js");
|
||||||
|
@ -186,6 +188,7 @@ describe("service-worker", () => {
|
||||||
expect(self.LibResilientConfig.defaultPluginTimeout).toBe(10000)
|
expect(self.LibResilientConfig.defaultPluginTimeout).toBe(10000)
|
||||||
expect(self.LibResilientConfig.plugins).toStrictEqual([{name: "fetch"},{name: "cache"}])
|
expect(self.LibResilientConfig.plugins).toStrictEqual([{name: "fetch"},{name: "cache"}])
|
||||||
expect(self.LibResilientConfig.loggedComponents).toStrictEqual(['service-worker', 'fetch', 'cache'])
|
expect(self.LibResilientConfig.loggedComponents).toStrictEqual(['service-worker', 'fetch', 'cache'])
|
||||||
|
expect(fetch).toHaveBeenCalled();
|
||||||
})
|
})
|
||||||
|
|
||||||
test("basic set-up: use default LibResilientConfig values when no valid 'loggedComponents' field in config.json", async () => {
|
test("basic set-up: use default LibResilientConfig values when no valid 'loggedComponents' field in config.json", async () => {
|
||||||
|
@ -221,6 +224,7 @@ describe("service-worker", () => {
|
||||||
expect(self.LibResilientConfig.defaultPluginTimeout).toBe(10000)
|
expect(self.LibResilientConfig.defaultPluginTimeout).toBe(10000)
|
||||||
expect(self.LibResilientConfig.plugins).toStrictEqual([{name: "fetch"},{name: "cache"}])
|
expect(self.LibResilientConfig.plugins).toStrictEqual([{name: "fetch"},{name: "cache"}])
|
||||||
expect(self.LibResilientConfig.loggedComponents).toStrictEqual(['service-worker', 'fetch', 'cache'])
|
expect(self.LibResilientConfig.loggedComponents).toStrictEqual(['service-worker', 'fetch', 'cache'])
|
||||||
|
expect(fetch).toHaveBeenCalled();
|
||||||
})
|
})
|
||||||
|
|
||||||
test("basic set-up: use default LibResilientConfig values when 'defaultPluginTimeout' field in config.json contains an invalid value", async () => {
|
test("basic set-up: use default LibResilientConfig values when 'defaultPluginTimeout' field in config.json contains an invalid value", async () => {
|
||||||
|
@ -256,6 +260,7 @@ describe("service-worker", () => {
|
||||||
expect(self.LibResilientConfig.defaultPluginTimeout).toBe(10000)
|
expect(self.LibResilientConfig.defaultPluginTimeout).toBe(10000)
|
||||||
expect(self.LibResilientConfig.plugins).toStrictEqual([{name: "fetch"},{name: "cache"}])
|
expect(self.LibResilientConfig.plugins).toStrictEqual([{name: "fetch"},{name: "cache"}])
|
||||||
expect(self.LibResilientConfig.loggedComponents).toStrictEqual(['service-worker', 'fetch', 'cache'])
|
expect(self.LibResilientConfig.loggedComponents).toStrictEqual(['service-worker', 'fetch', 'cache'])
|
||||||
|
expect(fetch).toHaveBeenCalled();
|
||||||
})
|
})
|
||||||
|
|
||||||
test("basic set-up: use config values from a valid config.json file", async () => {
|
test("basic set-up: use config values from a valid config.json file", async () => {
|
||||||
|
@ -291,6 +296,7 @@ describe("service-worker", () => {
|
||||||
expect(self.LibResilientConfig.defaultPluginTimeout).toBe(5000)
|
expect(self.LibResilientConfig.defaultPluginTimeout).toBe(5000)
|
||||||
expect(self.LibResilientConfig.plugins).toStrictEqual([{name: "cache"}])
|
expect(self.LibResilientConfig.plugins).toStrictEqual([{name: "cache"}])
|
||||||
expect(self.LibResilientConfig.loggedComponents).toStrictEqual(['service-worker', 'cache'])
|
expect(self.LibResilientConfig.loggedComponents).toStrictEqual(['service-worker', 'cache'])
|
||||||
|
expect(fetch).toHaveBeenCalled();
|
||||||
})
|
})
|
||||||
|
|
||||||
test("basic set-up: a valid config.json file gets cached", async () => {
|
test("basic set-up: a valid config.json file gets cached", async () => {
|
||||||
|
@ -327,6 +333,7 @@ describe("service-worker", () => {
|
||||||
expect(self.LibResilientConfig.defaultPluginTimeout).toBe(5000)
|
expect(self.LibResilientConfig.defaultPluginTimeout).toBe(5000)
|
||||||
expect(self.LibResilientConfig.plugins).toStrictEqual([{name: "cache"}])
|
expect(self.LibResilientConfig.plugins).toStrictEqual([{name: "cache"}])
|
||||||
expect(self.LibResilientConfig.loggedComponents).toStrictEqual(['service-worker', 'cache'])
|
expect(self.LibResilientConfig.loggedComponents).toStrictEqual(['service-worker', 'cache'])
|
||||||
|
expect(fetch).toHaveBeenCalled();
|
||||||
// this is silly but works, and is necessary because... who knows, honestly?
|
// this is silly but works, and is necessary because... who knows, honestly?
|
||||||
await new Promise(resolve => resolve(true))
|
await new Promise(resolve => resolve(true))
|
||||||
expect (await caches.open('v1').then((cache)=>{
|
expect (await caches.open('v1').then((cache)=>{
|
||||||
|
@ -338,6 +345,46 @@ describe("service-worker", () => {
|
||||||
})).toStrictEqual({loggedComponents: ['service-worker', 'cache'], plugins: [{name: "cache"}], defaultPluginTimeout: 5000})
|
})).toStrictEqual({loggedComponents: ['service-worker', 'cache'], plugins: [{name: "cache"}], defaultPluginTimeout: 5000})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test("basic set-up: a cached valid config.json file gets used, no fetch happens", async () => {
|
||||||
|
self.LibResilientConfig = null
|
||||||
|
|
||||||
|
var configData = {loggedComponents: ['service-worker', 'cache'], plugins: [{name: "cache"}], defaultPluginTimeout: 5000}
|
||||||
|
var configUrl = '/config.json'
|
||||||
|
var configResponse = new Response(
|
||||||
|
new Blob(
|
||||||
|
[JSON.stringify(configData)],
|
||||||
|
{type: "application/json"}
|
||||||
|
),
|
||||||
|
{
|
||||||
|
status: 200,
|
||||||
|
statusText: "OK",
|
||||||
|
headers: {
|
||||||
|
'ETag': 'TestingETagHeader'
|
||||||
|
},
|
||||||
|
url: configUrl
|
||||||
|
})
|
||||||
|
await caches
|
||||||
|
.open('v1')
|
||||||
|
.then((cache)=>{
|
||||||
|
return cache.put(configUrl, configResponse)
|
||||||
|
})
|
||||||
|
|
||||||
|
try {
|
||||||
|
require("../service-worker.js");
|
||||||
|
} catch(e) {}
|
||||||
|
await self.trigger('install')
|
||||||
|
// this is silly but works, and is necessary because
|
||||||
|
// event.waitUntil() in the install event handler is not handled correctly in NodeJS
|
||||||
|
await new Promise(resolve => resolve(true))
|
||||||
|
await self.trigger('activate')
|
||||||
|
expect(typeof self.LibResilientConfig).toEqual('object')
|
||||||
|
expect(self.LibResilientConfig.defaultPluginTimeout).toBe(5000)
|
||||||
|
expect(self.LibResilientConfig.plugins).toStrictEqual([{name: "cache"}])
|
||||||
|
expect(self.LibResilientConfig.loggedComponents).toStrictEqual(['service-worker', 'cache'])
|
||||||
|
expect(fetch).not.toHaveBeenCalled();
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
test("fetching content should work", async () => {
|
test("fetching content should work", async () => {
|
||||||
self.LibResilientConfig = {
|
self.LibResilientConfig = {
|
||||||
plugins: [{
|
plugins: [{
|
||||||
|
|
Ładowanie…
Reference in New Issue