Repository navigation
Expand file tree
/
Copy pathjest_after_env_setup.js
More file actions
87 lines (71 loc) · 2.51 KB
/
Copy pathjest_after_env_setup.js
File metadata and controls
87 lines (71 loc) · 2.51 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Used for global imports before each test file is run (but after the Jest Environment is set up)
* https://jestjs.io/docs/configuration#setupfilesafterenv-array
*/
// React
import React from "react";
global.React = React;
// Jest dom (testing env)
import "@testing-library/jest-dom";
// Fail any test that writes to the console (e.g. React act(...) or key warnings).
// Tests that expect console output should mock it, e.g.
// jest.spyOn(console, "error").mockImplementation(() => {}), and assert on the calls.
import failOnConsole from "jest-fail-on-console";
failOnConsole({
shouldFailOnAssert: true,
shouldFailOnDebug: true,
shouldFailOnError: true,
shouldFailOnInfo: true,
shouldFailOnLog: true,
shouldFailOnWarn: true,
});
// Routes
window.Routes = global.Routes = require("./app/javascript/routes");
// Jest fetch mock
require("jest-fetch-mock").enableMocks();
// Stub Element.prototype.scrollIntoView (not implemented in JSDOM)
Object.defineProperty(Element.prototype, "scrollIntoView", {
value: jest.fn(),
writable: true,
configurable: true,
});
// Define HTMLElement.prototype.offsetParent
// Code from https://github.com/jsdom/jsdom/issues/1261#issuecomment-1765404346
Object.defineProperty(HTMLElement.prototype, "offsetParent", {
get() {
// eslint-disable-next-line @typescript-eslint/no-this-alias
for (let element = this; element; element = element.parentNode) {
if (element.style?.display?.toLowerCase() === "none") {
return null;
}
}
if (this.stye?.position?.toLowerCase() === "fixed") {
return null;
}
if (this.tagName.toLowerCase() in ["html", "body"]) {
return null;
}
return this.parentNode;
},
});
// Ensure icons are defined
import "./app/javascript/common/fontawesome_config";
// Ensure App element is defined for react-modal
import Modal from "react-modal";
beforeAll(() => Modal.setAppElement("body"));
// Apply Mousetrap.prototype.stopCallback override so shift+up/down work from form inputs
import "./app/javascript/Components/Result/keybinding";
// Ensure @testing-library/react cleanup function is called after every test
import {cleanup} from "@testing-library/react";
afterEach(cleanup);
import {
customLoadingProp,
customNoDataComponent,
customNoDataProps,
} from "/app/javascript/Components/Helpers/table_helpers";
import {ReactTableDefaults} from "react-table";
Object.assign(ReactTableDefaults, {
NoDataComponent: customNoDataComponent,
noDataProps: customNoDataProps,
LoadingComponent: customLoadingProp,
});