forked from testing-library/react-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.tsx
133 lines (113 loc) · 3.62 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
import * as React from 'react'
import {render, fireEvent, screen, waitFor} 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'),
)
}
/*
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