forked from testing-library/react-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.tsx
217 lines (180 loc) · 5.79 KB
/
test.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import * as React from 'react'
import {render, fireEvent, screen, waitFor, renderHook} from '.'
import * as pure from './pure'
export async function testRender() {
const view = render(<button />)
// single queries
view.getByText('foo')
view.queryByText('foo')
await view.findByText('foo')
// multiple queries
view.getAllByText('bar')
view.queryAllByText('bar')
await view.findAllByText('bar')
// helpers
const {container, rerender, debug} = view
expectType<HTMLElement, typeof container>(container)
return {container, rerender, debug}
}
export async function testPureRender() {
const view = pure.render(<button />)
// single queries
view.getByText('foo')
view.queryByText('foo')
await view.findByText('foo')
// multiple queries
view.getAllByText('bar')
view.queryAllByText('bar')
await view.findAllByText('bar')
// helpers
const {container, rerender, debug} = view
expectType<HTMLElement, typeof container>(container)
return {container, rerender, debug}
}
export function testRenderOptions() {
const container = document.createElement('div')
const options = {container}
const {container: returnedContainer} = render(<button />, options)
expectType<HTMLDivElement, typeof returnedContainer>(returnedContainer)
}
export function testSVGRenderOptions() {
const container = document.createElementNS(
'http://www.w3.org/2000/svg',
'svg',
)
const options = {container}
const {container: returnedContainer} = render(<path />, options)
expectType<SVGSVGElement, typeof returnedContainer>(returnedContainer)
}
export function testFireEvent() {
const {container} = render(<button />)
fireEvent.click(container)
}
export function testDebug() {
const {debug, getAllByTestId} = render(
<>
<h2 data-testid="testid">Hello World</h2>
<h2 data-testid="testid">Hello World</h2>
</>,
)
debug(getAllByTestId('testid'))
}
export async function testScreen() {
render(<button />)
await screen.findByRole('button')
}
export async function testWaitFor() {
const {container} = render(<button />)
fireEvent.click(container)
await waitFor(() => {})
}
export function testQueries() {
const {getByLabelText} = render(
<label htmlFor="usernameInput">Username</label>,
)
expectType<HTMLElement, ReturnType<typeof getByLabelText>>(
getByLabelText('Username'),
)
const container = document.createElement('div')
const options = {container}
const {getByText} = render(<div>Hello World</div>, options)
expectType<HTMLElement, ReturnType<typeof getByText>>(
getByText('Hello World'),
)
}
export function wrappedRender(
ui: React.ReactElement,
options?: pure.RenderOptions,
) {
const Wrapper = ({children}: {children: React.ReactElement}): JSX.Element => {
return <div>{children}</div>
}
return pure.render(ui, {wrapper: Wrapper, ...options})
}
export function wrappedRenderB(
ui: React.ReactElement,
options?: pure.RenderOptions,
) {
const Wrapper: React.FunctionComponent<{children?: React.ReactNode}> = ({
children,
}) => {
return <div>{children}</div>
}
return pure.render(ui, {wrapper: Wrapper, ...options})
}
export function wrappedRenderC(
ui: React.ReactElement,
options?: pure.RenderOptions,
) {
interface AppWrapperProps {
children?: React.ReactNode
userProviderProps?: {user: string}
}
const AppWrapperProps: React.FunctionComponent<AppWrapperProps> = ({
children,
userProviderProps = {user: 'TypeScript'},
}) => {
return <div data-testid={userProviderProps.user}>{children}</div>
}
return pure.render(ui, {wrapper: AppWrapperProps, ...options})
}
export function testBaseElement() {
const {baseElement: baseDefaultElement} = render(<div />)
expectType<HTMLElement, typeof baseDefaultElement>(baseDefaultElement)
const container = document.createElement('input')
const {baseElement: baseElementFromContainer} = render(<div />, {container})
expectType<typeof container, typeof baseElementFromContainer>(
baseElementFromContainer,
)
const baseElementOption = document.createElement('input')
const {baseElement: baseElementFromOption} = render(<div />, {
baseElement: baseElementOption,
})
expectType<typeof baseElementOption, typeof baseElementFromOption>(
baseElementFromOption,
)
}
export function testRenderHook() {
const {result, rerender, unmount} = renderHook(() => React.useState(2)[0])
expectType<number, typeof result.current>(result.current)
rerender()
unmount()
}
export function testRenderHookProps() {
const {result, rerender, unmount} = renderHook(
({defaultValue}) => React.useState(defaultValue)[0],
{initialProps: {defaultValue: 2}},
)
expectType<number, typeof result.current>(result.current)
rerender()
unmount()
}
/*
eslint
testing-library/prefer-explicit-assert: "off",
testing-library/no-wait-for-empty-callback: "off",
testing-library/no-debug: "off",
testing-library/prefer-screen-queries: "off"
*/
// https://stackoverflow.com/questions/53807517/how-to-test-if-two-types-are-exactly-the-same
type IfEquals<T, U, Yes = unknown, No = never> = (<G>() => G extends T
? 1
: 2) extends <G>() => G extends U ? 1 : 2
? Yes
: No
/**
* Issues a type error if `Expected` is not identical to `Actual`.
*
* `Expected` should be declared when invoking `expectType`.
* `Actual` should almost always we be a `typeof value` statement.
*
* Source: https://github.com/mui-org/material-ui/blob/6221876a4b468a3330ffaafa8472de7613933b87/packages/material-ui-types/index.d.ts#L73-L84
*
* @example `expectType<number | string, typeof value>(value)`
* TypeScript issues a type error since `value is not assignable to never`.
* This means `typeof value` is not identical to `number | string`
* @param actual
*/
declare function expectType<Expected, Actual>(
actual: IfEquals<Actual, Expected, Actual>,
): void