kopia lustrzana https://gitlab.com/rysiekpl/libresilient
any-of plugin tests: coverage 100% (ref. #8)
rodzic
ef71323cf9
commit
4a46175cfe
|
@ -1,18 +1,117 @@
|
|||
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);
|
||||
});
|
||||
|
||||
/*
|
||||
* we need a Promise.any() polyfill
|
||||
* so here it is
|
||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any
|
||||
*
|
||||
* TODO: remove once Promise.any() is implemented broadly
|
||||
*/
|
||||
if (typeof Promise.any === 'undefined') {
|
||||
Promise.any = async (promises) => {
|
||||
// Promise.all() is the polar opposite of Promise.any()
|
||||
// in that it returns as soon as there is a first rejection
|
||||
// but without it, it returns an array of resolved results
|
||||
return Promise.all(
|
||||
promises.map(p => {
|
||||
return new Promise((resolve, reject) =>
|
||||
// swap reject and resolve, so that we can use Promise.all()
|
||||
// and get the result we need
|
||||
Promise.resolve(p).then(reject, resolve)
|
||||
);
|
||||
})
|
||||
// now, swap errors and values back
|
||||
).then(
|
||||
err => Promise.reject(err),
|
||||
val => Promise.resolve(val)
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
describe("plugin: any-of", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
Object.assign(global, makeServiceWorkerEnv());
|
||||
jest.resetModules();
|
||||
self.LibResilientPlugins = new Array()
|
||||
self.LibResilientConfig = {
|
||||
plugins: {
|
||||
'any-of':{}
|
||||
'any-of': {
|
||||
plugins: {
|
||||
'fetch': {},
|
||||
'fetch': {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
self.log = function(component, ...items) {
|
||||
console.debug(component + ' :: ', ...items)
|
||||
}
|
||||
})
|
||||
|
||||
test("it should register in LibResilientPlugins", () => {
|
||||
require("../../plugins/any-of.js");
|
||||
expect(self.LibResilientPlugins[0].name).toEqual('any-of');
|
||||
});
|
||||
|
||||
test("it should return data from fetch()", async () => {
|
||||
require("../../plugins/any-of.js");
|
||||
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 throw an error when HTTP status is >= 400", async () => {
|
||||
|
||||
global.fetch.mockImplementation((url, init) => {
|
||||
const response = new Response(
|
||||
new Blob(
|
||||
["Not Found"],
|
||||
{type: "text/plain"}
|
||||
),
|
||||
{
|
||||
status: 404,
|
||||
statusText: "Not Found",
|
||||
url: url
|
||||
});
|
||||
return Promise.resolve(response);
|
||||
});
|
||||
|
||||
require("../../plugins/any-of.js");
|
||||
require("../../plugins/fetch.js");
|
||||
|
||||
expect.assertions(2);
|
||||
try {
|
||||
await self.LibResilientPlugins[0].fetch('https://resilient.is/test.json')
|
||||
} catch (e) {
|
||||
expect(e[0]).toBeInstanceOf(Error)
|
||||
}
|
||||
expect(fetch).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
@ -34,7 +34,8 @@
|
|||
let fetchContent = (url) => {
|
||||
self.log(config.name, `using: [${Object.keys(config.plugins).join(', ')}]!`)
|
||||
return Promise.any(
|
||||
LibResilientPlugins
|
||||
self
|
||||
.LibResilientPlugins
|
||||
.filter(p=>Object.keys(config.plugins).includes(p.name))
|
||||
.map(p=>p.fetch(url))
|
||||
)
|
||||
|
|
Ładowanie…
Reference in New Issue