-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestUtils.tsx
54 lines (48 loc) · 1.76 KB
/
testUtils.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
import { FunctionComponent, ReactElement } from "react";
import { render, RenderOptions, RenderResult } from "@testing-library/react";
import { MockedProvider, MockedResponse } from "@apollo/client/testing";
import { MemoryRouter as Router, Routes, Route } from "react-router-dom";
interface IRenderer {
render: (ui: ReactElement, options?: RenderOptions) => RenderResult;
withGql: (mocks?: MockedResponse[]) => IRenderer;
withRouter: (config?: {
initialEntries?: string[];
route?: string;
}) => IRenderer;
}
const baseWrapper: FunctionComponent = ({ children }) => <>{children}</>;
export const renderer = makeRenderer(baseWrapper);
function makeRenderer(Providers: FunctionComponent): IRenderer {
return {
render: (ui: ReactElement, options: RenderOptions = {}) =>
render(ui, { ...options, wrapper: Providers }),
withGql: makeWithGql(Providers),
withRouter: makeWithRouter(Providers),
};
}
function makeWithGql(ParentProviders: FunctionComponent) {
return function withGql(mocks: MockedResponse[] = []) {
const Providers: FunctionComponent = ({ children }) => (
<ParentProviders>
<MockedProvider mocks={mocks} addTypename={false}>
{children}
</MockedProvider>
</ParentProviders>
);
return makeRenderer(Providers);
};
}
function makeWithRouter(ParentProviders: FunctionComponent) {
return function withRouter({ initialEntries = ["/"], route = "/" } = {}) {
const Providers: FunctionComponent = ({ children }) => (
<ParentProviders>
<Router initialEntries={initialEntries} initialIndex={0}>
<Routes>
<Route path={route} element={<>{children}</>} />
</Routes>
</Router>
</ParentProviders>
);
return makeRenderer(Providers);
};
}