kopia lustrzana https://gitlab.com/rysiekpl/libresilient
66 wiersze
2.3 KiB
JavaScript
66 wiersze
2.3 KiB
JavaScript
const makeServiceWorkerEnv = require('service-worker-mock');
|
|
|
|
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);
|
|
});
|
|
|
|
describe("plugin: fetch", () => {
|
|
beforeEach(() => {
|
|
Object.assign(global, makeServiceWorkerEnv());
|
|
jest.resetModules();
|
|
self.LibResilientPlugins = new Array()
|
|
self.LibResilientConfig = {
|
|
plugins: {
|
|
'fetch':{}
|
|
}
|
|
}
|
|
self.log = function(component, ...items) {
|
|
console.debug(component + ' :: ', ...items)
|
|
}
|
|
})
|
|
test("it should register in LibResilientPlugins", () => {
|
|
require("../../plugins/fetch.js");
|
|
expect(self.LibResilientPlugins[0].name).toEqual('fetch');
|
|
});
|
|
|
|
test("it should return data from fetch()", async () => {
|
|
require("../../plugins/fetch.js");
|
|
|
|
const response = await self.LibResilientPlugins[0].fetch('https://resilient.is/test.json');
|
|
|
|
expect(fetch).toHaveBeenCalled();
|
|
expect(await response.json()).toEqual({test: "success"})
|
|
expect(response.url).toEqual('https://resilient.is/test.json')
|
|
});
|
|
|
|
test("it should set the LibResilient headers", async () => {
|
|
require("../../plugins/fetch.js");
|
|
|
|
const response = await self.LibResilientPlugins[0].fetch('https://resilient.is/test.json');
|
|
|
|
expect(fetch).toHaveBeenCalled();
|
|
expect(await response.json()).toEqual({test: "success"})
|
|
expect(response.url).toEqual('https://resilient.is/test.json')
|
|
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')
|
|
});
|
|
});
|