-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathsetupTestWrapperTL.tsx
186 lines (163 loc) · 5.19 KB
/
setupTestWrapperTL.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { act, render, RenderResult } from "@testing-library/react"
import { MockBoot } from "DevTools/MockBoot"
import * as React from "react"
import {
GraphQLTaggedNode,
QueryRenderer,
RelayEnvironmentProvider,
Variables,
} from "react-relay"
import { OperationDescriptor, OperationType } from "relay-runtime"
import {
createMockEnvironment,
MockPayloadGenerator,
MockEnvironment,
} from "relay-test-utils"
import { MockResolvers } from "relay-test-utils/lib/RelayMockPayloadGenerator"
type SetupTestWrapper<T extends OperationType> = {
Component: React.ComponentType<React.PropsWithChildren<T["response"]>>
query?: GraphQLTaggedNode
variables?: T["variables"]
}
/**
* Creates a wrapper for testing Relay components using the `relay-test-utils`
* package, which will provide automatic fixture data for GraphQL queries.
*
* Note: If wanting to test a QueryRenderer, extract the render code into a
* fragment-like container and test that; `QueryRenderer` components aren't
* supported.
*
* @see https://relay.dev/docs/guides/testing-relay-components/
*
* @example
import { screen } from "@testing-library/react"
import { setupTestWrapperTL } from "DevTools/setupTestWrapperTL"
// Important! Don't forget to unmock relay, otherwise the following error
// will occur: "RelayModernMockEnvironment: There are no pending operations."
jest.unmock("react-relay")
const { renderWithRelay } = setupTestWrapperTL({
Component: (props: any) => {
return <ShowMetaFragmentContainer show={props.show} />
},
query: graphql`
query ExampleOverviewRoute_Test_Query($showID: String!) {
show(id: $showID) {
description
}
}
`,
variables: {
showID: 'some-show'
}
})
// Invoking without arguments will return dummy fixture data, provide by relay
renderWithRelay()
expect(screen.queryByText("Some text string")).toBeInTheDocument()
// If wanting to manually provide fixture data, can pass in an object that
// maps to the type and field resolvers.
renderWithRelay({
Show: () => ({
description: "Hello world!"
})
})
expect(screen.queryByText("Hello world")).toBeInTheDocument()
*/
/**
* Creates a React Testing Library-based wrapper for testing Relay components
* using the `relay-test-utils` package, which will provide automatic fixture
* data for GraphQL queries.
*
* @see https://relay.dev/docs/en/testing-relay-components
* @see https://testing-library.com/docs/react-testing-library/intro
*/
type RTLRenderResult = RenderResult<
typeof import("@testing-library/dom/types/queries"),
HTMLElement
>
type RenderWithRelay = RTLRenderResult & {
env: MockEnvironment
mockResolveLastOperation: (
mockResolvers: MockResolvers
) => {
operation: OperationDescriptor
operationName: string
operationVariables: Variables
}
mockRejectLastOperation: (
error: Error
) => {
operation: OperationDescriptor
operationName: string
operationVariables: Variables
}
}
export const setupTestWrapperTL = <T extends OperationType>({
Component,
query,
variables = {},
}: SetupTestWrapper<T>) => {
const renderWithRelay = (
mockResolvers: MockResolvers = {},
componentProps?: any,
mockedEnv?: ReturnType<typeof createMockEnvironment>
): RenderWithRelay => {
const env = mockedEnv ?? createMockEnvironment()
const TestRenderer = () =>
query ? (
<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>
) : (
<MockBoot relayEnvironment={env}>
<RelayEnvironmentProvider environment={env}>
<Component {...componentProps} />
</RelayEnvironmentProvider>
</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 view = render(<TestRenderer />)
act(() => {
env.mock.resolveMostRecentOperation(operation => {
return MockPayloadGenerator.generate(operation, mockResolvers)
})
})
return {
...view,
env,
mockResolveLastOperation,
mockRejectLastOperation,
}
}
return { renderWithRelay }
}