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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/*
* SonarQube
* Copyright (C) 2009-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { RenderOptions, RenderResult, render as rtlRender } from '@testing-library/react';
import userEvent, { UserEvent } from '@testing-library/user-event';
import { Options as UserEventsOptions } from '@testing-library/user-event/dist/types/options';
import { InitialEntry } from 'history';
import { identity, kebabCase } from 'lodash';
import React, { PropsWithChildren, ReactNode } from 'react';
import { HelmetProvider } from 'react-helmet-async';
import { IntlProvider, ReactIntlErrorCode } from 'react-intl';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
type RenderResultWithUser = RenderResult & { user: UserEvent };
export function render(
ui: React.ReactElement,
options?: RenderOptions,
userEventOptions?: UserEventsOptions,
): RenderResultWithUser {
return { ...rtlRender(ui, options), user: userEvent.setup(userEventOptions) };
}
type RenderContextOptions = Omit<RenderOptions, 'wrapper'> & {
initialEntries?: InitialEntry[];
userEventOptions?: UserEventsOptions;
};
export function renderWithContext(
ui: React.ReactElement,
{ userEventOptions, ...options }: RenderContextOptions = {},
) {
return render(ui, { ...options, wrapper: getContextWrapper() }, userEventOptions);
}
interface RenderRouterOptions {
additionalRoutes?: ReactNode;
}
export function renderWithRouter(
ui: React.ReactElement,
options: RenderContextOptions & RenderRouterOptions = {},
) {
const { additionalRoutes, userEventOptions, ...renderOptions } = options;
function RouterWrapper({ children }: React.PropsWithChildren<object>) {
return (
<HelmetProvider>
<IntlWrapper>
<MemoryRouter>
<Routes>
<Route element={children} path="/" />
{additionalRoutes}
</Routes>
</MemoryRouter>
</IntlWrapper>
</HelmetProvider>
);
}
return render(ui, { ...renderOptions, wrapper: RouterWrapper }, userEventOptions);
}
function getContextWrapper() {
return function ContextWrapper({ children }: React.PropsWithChildren<object>) {
return (
<HelmetProvider>
<IntlWrapper>{children}</IntlWrapper>
</HelmetProvider>
);
};
}
export function mockComponent(name: string, transformProps: (props: any) => any = identity) {
function MockedComponent({ ...props }: PropsWithChildren<any>) {
return React.createElement('mocked-' + kebabCase(name), transformProps(props));
}
MockedComponent.displayName = `mocked(${name})`;
return MockedComponent;
}
export const debounceTimer = jest
.fn()
.mockImplementation((callback: (...args: unknown[]) => void, timeout: number) => {
let timeoutId: number;
const debounced = jest.fn((...args: unknown[]) => {
window.clearTimeout(timeoutId);
timeoutId = window.setTimeout(() => {
callback(...args);
}, timeout);
});
(debounced as typeof debounced & { cancel: () => void }).cancel = jest.fn(() => {
window.clearTimeout(timeoutId);
});
return debounced;
});
export function IntlWrapper({
children,
messages = {},
}: {
children: ReactNode;
messages?: Record<string, string>;
}) {
return (
<IntlProvider
defaultLocale="en"
locale="en"
messages={messages}
onError={(e) => {
// ignore missing translations, there are none!
if (
e.code !== ReactIntlErrorCode.MISSING_TRANSLATION &&
e.code !== ReactIntlErrorCode.UNSUPPORTED_FORMATTER
) {
// eslint-disable-next-line no-console
console.error(e);
}
}}
>
{children}
</IntlProvider>
);
}
|