-
Notifications
You must be signed in to change notification settings - Fork 2
/
setUpTest.tsx
47 lines (39 loc) · 1.33 KB
/
setUpTest.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
import '@testing-library/jest-dom';
import { toHaveNoViolations } from 'jest-axe';
import React, { ReactElement, ReactNode } from 'react';
import { IntlProvider } from 'react-intl';
import { render as rtlRender } from '@testing-library/react';
import messagesNb from 'lib/translations/nb.json';
require('jest-fetch-mock').enableMocks();
jest.mock('next/router', () => ({
useRouter: jest.fn(),
}));
expect.extend(toHaveNoViolations);
function flattenMessages(nestedMessages: object, prefix = ''): Record<string, string> {
return Object.keys(nestedMessages).reduce((messages, key) => {
// @ts-ignore
let value = nestedMessages[key];
let prefixedKey = prefix ? `${prefix}.${key}` : key;
if (typeof value === 'string') {
// @ts-ignore
messages[prefixedKey] = value;
} else {
Object.assign(messages, flattenMessages(value, prefixedKey));
}
return messages;
}, {});
}
function render(ui: ReactElement, { locale = 'nb', ...options } = {}) {
function Wrapper({ children }: { children: ReactNode }) {
return (
<IntlProvider locale={locale} messages={flattenMessages(messagesNb)}>
{children}
</IntlProvider>
);
}
return rtlRender(ui, { wrapper: Wrapper, ...options });
}
// re-export everything
export * from '@testing-library/react';
// override render method
export { render };