-
Notifications
You must be signed in to change notification settings - Fork 1
/
setupTests.tsx
94 lines (86 loc) · 3.1 KB
/
setupTests.tsx
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
// 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';
import { configure, render as rtlRender } from '@testing-library/react';
import { AppStateContext, AppStateContextState } from 'context/appStateContext';
import { StepWizardContext, StepWizardContextState } from 'context/stepWizardContext';
import { ReactElement, ReactNode, useReducer } from 'react';
import { IntlProvider } from 'react-intl';
import { flattenMessages, messages } from 'utils/message';
import links from 'translations/links.json';
import {
SoknadContext,
soknadContextInititalState,
SoknadContextState,
SoknadContextType,
} from 'context/soknadcontext/soknadContext';
import { soknadReducer } from 'context/soknadcontext/reducer';
jest.setTimeout(10 * 1000);
configure({ asyncUtilTimeout: 10 * 1000 });
jest.mock('next/router', () => ({
useRouter: jest.fn(),
}));
const tekster = { ...messages['nb'], ...flattenMessages({ applinks: links }) };
function render(ui: ReactElement, { locale = 'nb', ...options } = {}) {
function Wrapper({ children }: { children: ReactNode }) {
return (
<IntlProvider locale={locale} messages={tekster}>
{children}
</IntlProvider>
);
}
return rtlRender(ui, { wrapper: Wrapper, ...options });
}
function renderStepSoknadStandard(
stepName: string,
ui: ReactElement,
{ locale = 'nb', ...options } = {},
initialState?: SoknadContextState,
) {
function ProvidersWrapper({ children }: { children: ReactNode }) {
const initialSoknadContext = initialState || soknadContextInititalState;
const [state, dispatch] = useReducer(soknadReducer, initialSoknadContext);
const soknadContext: SoknadContextType = {
søknadState: state,
søknadDispatch: dispatch,
};
const wizardContext: StepWizardContextState = {
stepList: [{ name: stepName }],
currentStep: { name: stepName },
currentStepIndex: 0,
stepWizardDispatch: () => ({}),
};
const appContext: AppStateContextState = {
appState: {
sistLagret: 'i dag',
},
appStateDispatch: () => {},
};
return (
<IntlProvider locale={locale} messages={tekster}>
<AppStateContext.Provider value={{ ...appContext }}>
<SoknadContext.Provider value={{ ...soknadContext }}>
<StepWizardContext.Provider value={{ ...wizardContext }}>
{children}
</StepWizardContext.Provider>
,
</SoknadContext.Provider>
</AppStateContext.Provider>
</IntlProvider>
);
}
return rtlRender(ui, { wrapper: ProvidersWrapper, ...options });
}
// Mocker resize observer da jsdom ikke implementerer denne
class ResizeObserver {
observe() {}
unobserve() {}
disconnect() {}
}
window.ResizeObserver = ResizeObserver;
// Mocker scrollIntoView da jsdom ikke implementerer denne
window.HTMLElement.prototype.scrollIntoView = function () {};
export * from '@testing-library/react';
export { render, renderStepSoknadStandard };