OpenMediaCenter/src/setupTests.js

55 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-05-31 11:47:57 +00:00
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom/extend-expect';
2020-06-12 15:57:30 +00:00
import {configure} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import GlobalInfos from './utils/GlobalInfos';
2020-06-12 15:57:30 +00:00
configure({adapter: new Adapter()});
/**
* prepares fetch api for a virtual test call
* @param response the response fetch should give you back
* @returns {jest.Mock<any, any>} a jest mock function simulating a fetch
*/
global.prepareFetchApi = (response) => {
const mockJsonPromise = Promise.resolve(response);
const mockFetchPromise = Promise.resolve({
json: () => mockJsonPromise,
text: () => mockJsonPromise,
status: 200
});
return (jest.fn().mockImplementation(() => mockFetchPromise));
};
/**
* prepares a failing virtual fetch api call
* @returns {jest.Mock<any, any>} a jest moch function simulating a failing fetch call
*/
global.prepareFailingFetchApi = () => {
const mockFetchPromise = Promise.reject('myreason');
return (jest.fn().mockImplementation(() => mockFetchPromise));
};
global.callAPIMock = (resonse) => {
2020-12-29 19:39:30 +00:00
const helpers = require('./utils/Api');
helpers.callAPI = jest.fn().mockImplementation((_, __, func1) => {func1(resonse);});
helpers.callApiUnsafe = jest.fn().mockImplementation((_, __, func1) => {func1(resonse);});
2020-12-29 19:39:30 +00:00
};
// code to run before each test
global.beforeEach(() => {
// empty fetch response implementation for each test
global.fetch = prepareFetchApi({});
// todo with callAPIMock
2020-12-29 19:39:30 +00:00
});
global.afterEach(() => {
// clear all mocks after each test
jest.resetAllMocks();
2020-12-29 19:39:30 +00:00
});