forked from ProjectMirador/mirador
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setupJest.js
69 lines (55 loc) · 1.94 KB
/
setupJest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Setup Jest to mock fetch
import { JSDOM } from 'jsdom'; // eslint-disable-line import/no-extraneous-dependencies
import fetch from 'jest-fetch-mock'; // eslint-disable-line import/no-extraneous-dependencies
import Enzyme from 'enzyme'; // eslint-disable-line import/no-extraneous-dependencies
import Adapter from 'enzyme-adapter-react-16'; // eslint-disable-line import/no-extraneous-dependencies
const jsdom = new JSDOM('<!doctype html><html><body><div id="main"></div></body></html>');
const { window } = jsdom;
jest.setTimeout(10000);
window.HTMLCanvasElement.prototype.getContext = () => {};
jest.setMock('isomorphic-unfetch', fetch);
global.fetch = require('jest-fetch-mock'); // eslint-disable-line import/no-extraneous-dependencies
global.window = window;
global.navigator = {
userAgent: 'node.js',
};
/* eslint-disable require-jsdoc, class-methods-use-this */
class IntersectionObserverPolyfill {
observe() {
}
disconnect() {
}
}
/* eslint-enable require-jsdoc, class-methods-use-this */
global.IntersectionObserver = IntersectionObserverPolyfill;
/** */
function Path2D() {
}
global.Path2D = Path2D;
/**
* copy object property descriptors from `src` to `target`
* @param {*} src
* @param {*} target
*/
const copyProps = (src, target) => {
Object.defineProperties(target, {
...Object.getOwnPropertyDescriptors(src),
...Object.getOwnPropertyDescriptors(target),
});
};
/*
avoid 'ReferenceError: HTMLElement is not defined'
see https://github.com/airbnb/enzyme/blob/master/docs/guides/jsdom.md
for further information
*/
copyProps(window, global);
Enzyme.configure({ adapter: new Adapter() });
jest.mock('react-i18next', () => ({
// this mock makes sure any components using the translate HoC receive the t function as a prop
withTranslation: () => (Component) => {
Component.defaultProps = { // eslint-disable-line no-param-reassign
...Component.defaultProps, t: k => k,
};
return Component;
},
}));