-
Notifications
You must be signed in to change notification settings - Fork 3
/
jest.setup.js
94 lines (78 loc) · 2.42 KB
/
jest.setup.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Optional: configure or set up a testing framework before each test.
// If you delete this file, remove `setupFilesAfterEnv` from `jest.config.js`
// Learn more: https://github.com/testing-library/jest-dom
import "@testing-library/jest-dom";
import { faker } from "@faker-js/faker";
import { setConfig } from "next/config";
import { Settings } from "luxon";
Settings.defaultLocale = "en";
Settings.defaultZone = "Europe/Brussels";
Settings.now = jest.fn().mockImplementation(() => Date.now());
// @ts-ignore
import { publicRuntimeConfig } from "./next.config";
// Mock ResizeObserver
class ResizeObserver {
observe() {}
unobserve() {}
disconnect() {}
}
global.ResizeObserver = ResizeObserver;
// Make sure you can use "publicRuntimeConfig" within tests.
setConfig({
publicRuntimeConfig: {
...publicRuntimeConfig,
GRAPHQL_ENDPOINT: "http://localhost:3000/graphql/",
},
});
beforeEach(() => {
// Set seed for faker
faker.seed(1);
});
afterEach(() => {
window.localStorage.clear();
});
// Mock browser confirm
window.confirm = jest.fn();
jest.mock("react-markdown", () => (props) => {
return props.children || null;
});
jest.mock("react", () => {
const actualReact = jest.requireActual("react");
return {
...actualReact,
useId() {
const ref = actualReact.useRef(faker.string.uuid());
return ref.current;
},
};
});
jest.mock("next-i18next", () => ({
I18nextProvider: jest.fn(),
useTranslation: () => ({ t: (key) => key }),
__esModule: true,
}));
jest.mock("next/router", () => require("next-router-mock"));
// This is needed for mocking 'next/link':
jest.mock("next/dist/client/router", () => require("next-router-mock"));
// https://github.com/scottrippey/next-router-mock/issues/58#issuecomment-1182861712
// Fixes the navigation using links
jest.mock("next/dist/shared/lib/router-context.shared-runtime", () => {
const { createContext } = require("react");
const router = require("next-router-mock").default;
const RouterContext = createContext(router);
return { RouterContext };
});
// Mock the IntersectionObserver
const intersectionObserverMock = () => ({
observe: () => null,
unobserve: () => null,
disconnect: () => null,
});
window.IntersectionObserver = jest
.fn()
.mockImplementation(intersectionObserverMock);
// Mock @headlessui/react to disable animations
jest.mock("@headlessui/react", () => ({
__esModule: true,
...jest.requireActual("@headlessui/react"),
}));