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-10 13:41:41 +00:00
|
|
|
|
2020-06-12 15:57:30 +00:00
|
|
|
import {configure} from 'enzyme';
|
2020-06-10 13:41:41 +00:00
|
|
|
import Adapter from 'enzyme-adapter-react-16';
|
2020-12-17 20:53:22 +00:00
|
|
|
import GlobalInfos from './utils/GlobalInfos';
|
2020-06-10 13:41:41 +00:00
|
|
|
|
2020-06-12 15:57:30 +00:00
|
|
|
configure({adapter: new Adapter()});
|
2020-08-05 17:55:51 +00:00
|
|
|
|
2020-08-12 17:50:25 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2020-08-05 17:55:51 +00:00
|
|
|
global.prepareFetchApi = (response) => {
|
|
|
|
const mockJsonPromise = Promise.resolve(response);
|
|
|
|
const mockFetchPromise = Promise.resolve({
|
|
|
|
json: () => mockJsonPromise,
|
2020-10-21 19:14:45 +00:00
|
|
|
text: () => mockJsonPromise
|
2020-08-05 17:55:51 +00:00
|
|
|
});
|
|
|
|
return (jest.fn().mockImplementation(() => mockFetchPromise));
|
2020-10-25 18:48:23 +00:00
|
|
|
};
|
2020-08-05 17:55:51 +00:00
|
|
|
|
2020-08-12 17:50:25 +00:00
|
|
|
/**
|
|
|
|
* prepares a failing virtual fetch api call
|
|
|
|
* @returns {jest.Mock<any, any>} a jest moch function simulating a failing fetch call
|
|
|
|
*/
|
2020-08-05 17:55:51 +00:00
|
|
|
global.prepareFailingFetchApi = () => {
|
2020-10-25 18:48:23 +00:00
|
|
|
const mockFetchPromise = Promise.reject('myreason');
|
2020-08-05 17:55:51 +00:00
|
|
|
return (jest.fn().mockImplementation(() => mockFetchPromise));
|
2020-10-25 18:48:23 +00:00
|
|
|
};
|
2020-12-11 18:23:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* prepares a viewbinding instance
|
|
|
|
* @param func a mock function to be called
|
|
|
|
*/
|
|
|
|
global.prepareViewBinding = (func) => {
|
|
|
|
GlobalInfos.getViewBinding = () => {
|
|
|
|
return {
|
|
|
|
changeRootElement: func,
|
|
|
|
returnToLastElement: func
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-12-17 20:53:22 +00:00
|
|
|
|
|
|
|
global.callAPIMock = (resonse) => {
|
|
|
|
const helpers = require("./utils/Api");
|
|
|
|
helpers.callAPI = jest.fn().mockImplementation((_, __, func1) => {func1(resonse)});
|
|
|
|
}
|
|
|
|
|
|
|
|
// code to run before each test
|
|
|
|
global.beforeEach(() => {
|
|
|
|
// empty fetch response implementation for each test
|
|
|
|
global.fetch = prepareFetchApi({});
|
|
|
|
// todo with callAPIMock
|
|
|
|
})
|
|
|
|
|
|
|
|
global.afterEach(() => {
|
|
|
|
// clear all mocks after each test
|
|
|
|
jest.resetAllMocks();
|
|
|
|
})
|
|
|
|
|