-
Notifications
You must be signed in to change notification settings - Fork 153
/
setupTestWrapper.tsx
112 lines (95 loc) · 3.09 KB
/
setupTestWrapper.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { MockBoot } from "DevTools/MockBoot"
import { mount } from "enzyme"
import * as React from "react"
import { act } from "react-dom/test-utils"
import { type GraphQLTaggedNode, QueryRenderer } from "react-relay"
import type { OperationType } from "relay-runtime"
import { MockPayloadGenerator, createMockEnvironment } from "relay-test-utils"
import type { MockResolvers } from "relay-test-utils/lib/RelayMockPayloadGenerator"
type SetupTestWrapper<T extends OperationType> = {
Component: React.ComponentType<React.PropsWithChildren<T["response"]>>
query?: GraphQLTaggedNode
variables?: T["variables"]
}
/**
* @deprecated This method should _not_ be used for new tests. See
* `setupTestWrapperTL` which uses `@testing-library/react`.
*/
export const setupTestWrapper = <T extends OperationType>({
Component,
query,
variables = {},
}: SetupTestWrapper<T>) => {
const getWrapper = (
mockResolvers: MockResolvers = {},
componentProps: {} = {},
mockedEnv?: ReturnType<typeof createMockEnvironment>,
) => {
const env = mockedEnv ?? createMockEnvironment()
const TestRenderer = () => (
<MockBoot relayEnvironment={env}>
<QueryRenderer<T>
environment={env}
variables={variables}
query={query}
render={({ props, error }) => {
if (props) {
return (
<Component {...(componentProps || {})} {...(props as {})} />
)
} else if (error) {
console.error(error)
}
}}
/>
</MockBoot>
)
const mockResolveLastOperation = (mockResolvers: MockResolvers) => {
const operation = env.mock.getMostRecentOperation()
act(() => {
env.mock.resolve(
operation,
MockPayloadGenerator.generate(operation, mockResolvers),
)
})
const operationName = operation.request.node.operation.name
const operationVariables = operation.request.variables
return { operation, operationName, operationVariables }
}
const mockRejectLastOperation = (error: Error) => {
const operation = env.mock.getMostRecentOperation()
act(() => {
env.mock.reject(operation, error)
})
const operationName = operation.request.node.operation.name
const operationVariables = operation.request.variables
return { operation, operationName, operationVariables }
}
const wrapper = mount(
<ErrorBoundary>
<TestRenderer />
</ErrorBoundary>,
)
env.mock.resolveMostRecentOperation(operation => {
return MockPayloadGenerator.generate(operation, mockResolvers)
})
wrapper.update()
return {
wrapper,
env,
mockResolveLastOperation,
mockRejectLastOperation,
}
}
return { getWrapper }
}
class ErrorBoundary extends React.Component<React.PropsWithChildren> {
componentDidCatch(error) {
// Print an error to the console for a better debugging experience
console.log("Something went wrong while rendering a component")
console.log(error)
}
render() {
return this.props.children
}
}