better implementation of the fetch test

merge-requests/1/head
Michał 'rysiek' Woźniak 2021-08-28 19:50:56 +00:00
rodzic ba129551d4
commit edb0b68c44
1 zmienionych plików z 17 dodań i 11 usunięć

Wyświetl plik

@ -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"})
});
});