kopia lustrzana https://gitlab.com/rysiekpl/libresilient
better implementation of the fetch test
rodzic
ba129551d4
commit
edb0b68c44
|
@ -1,15 +1,21 @@
|
|||
const makeServiceWorkerEnv = require('service-worker-mock');
|
||||
|
||||
global.fetch = require('node-fetch');
|
||||
jest.mock('node-fetch', () => {
|
||||
const context = {
|
||||
then: jest.fn().mockImplementation(() => {
|
||||
const response = { test: "success" };
|
||||
return Promise.resolve(response);
|
||||
})
|
||||
};
|
||||
return jest.fn(() => context);
|
||||
});
|
||||
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: []
|
||||
});
|
||||
return Promise.resolve(response);
|
||||
});
|
||||
|
||||
describe("plugin: fetch", () => {
|
||||
beforeEach(() => {
|
||||
|
@ -33,8 +39,8 @@ describe("plugin: fetch", () => {
|
|||
test("it should return data from fetch()", async () => {
|
||||
require("../../plugins/fetch.js");
|
||||
|
||||
const returnedJSON = await self.LibResilientPlugins[0].fetch('https://resilient.is/test.json');
|
||||
const response = await self.LibResilientPlugins[0].fetch('https://resilient.is/test.json');
|
||||
expect(fetch).toHaveBeenCalled();
|
||||
expect(returnedJSON).toEqual({test: "success"})
|
||||
expect(await response.json()).toEqual({test: "success"})
|
||||
});
|
||||
});
|
||||
|
|
Ładowanie…
Reference in New Issue