forked from testing-library/dom-testing-library
/
type-tests.ts
269 lines (236 loc) 路 7.89 KB
/
type-tests.ts
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import {
createEvent,
fireEvent,
isInaccessible,
queries,
buildQueries,
queryAllByAttribute,
screen,
waitFor,
waitForElementToBeRemoved,
MatcherOptions,
BoundFunctions,
within,
} from '@testing-library/dom'
const {
getByText,
queryByText,
findByText,
getAllByText,
queryAllByText,
findAllByText,
queryAllByRole,
queryByRole,
findByRole,
} = queries
export async function testQueries() {
// element queries
const element = document.createElement('div')
getByText(element, 'foo')
getByText(element, 1)
queryByText(element, 'foo')
await findByText(element, 'foo')
await findByText(element, 'foo', undefined, {timeout: 10})
getAllByText(element, 'bar')
queryAllByText(element, 'bar')
await findAllByText(element, 'bar')
await findAllByText(element, 'bar', undefined, {timeout: 10})
// screen queries
screen.getByText('foo')
screen.getByText<HTMLDivElement>('foo')
screen.queryByText('foo')
await screen.findByText('foo')
await screen.findByText('foo', undefined, {timeout: 10})
screen.debug(screen.getAllByText('bar'))
screen.queryAllByText('bar')
await screen.findAllByText('bar')
await screen.findAllByRole<HTMLButtonElement>('button', {name: 'submit'})
await screen.findAllByText('bar', undefined, {timeout: 10})
}
export async function testQueryHelpers() {
const element = document.createElement('div')
const includesAutomationId = (content: string, automationId: string) =>
content.split(/\s+/).some(id => id === automationId)
const queryAllByAutomationId = (
container: HTMLElement,
automationId: string[] | string,
options?: MatcherOptions,
) =>
queryAllByAttribute(
'testId',
container,
content =>
Array.isArray(automationId)
? automationId.every(id => includesAutomationId(content, id))
: includesAutomationId(content, automationId),
options,
)
const createIdRelatedErrorHandler =
(errorMessage: string, defaultErrorMessage: string) =>
<T>(container: Element | null, ...args: T[]) => {
const [key, value] = args
if (!container) {
return 'Container element not specified'
}
if (key && value) {
return errorMessage
.replace('[key]', String(key))
.replace('[value]', String(value))
}
return defaultErrorMessage
}
const [
queryByAutomationId,
getAllByAutomationId,
getByAutomationId,
findAllByAutomationId,
findByAutomationId,
] = buildQueries(
queryAllByAutomationId,
createIdRelatedErrorHandler(
`Found multiple with key [key] and value [value]`,
'Multiple error',
),
createIdRelatedErrorHandler(
`Unable to find an element with the [key] attribute of: [value]`,
'Missing error',
),
)
queryByAutomationId(element, 'id')
getAllByAutomationId(element, 'id')
getByAutomationId(element, ['id', 'automationId'])
await findAllByAutomationId(element, 'id', {}, {timeout: 1000})
await findByAutomationId(element, 'id', {}, {timeout: 1000})
// test optional params too
await findAllByAutomationId(element, 'id', {})
await findByAutomationId(element, 'id', {})
await findAllByAutomationId(element, 'id')
await findByAutomationId(element, 'id')
await findAllByAutomationId(element, ['id', 'id'], {})
await findByAutomationId(element, ['id', 'id'], {})
await findAllByAutomationId(element, ['id', 'id'])
await findByAutomationId(element, ['id', 'id'])
const screenWithCustomQueries = within(document.body, {
...queries,
queryByAutomationId,
getAllByAutomationId,
getByAutomationId,
findAllByAutomationId,
findByAutomationId,
})
screenWithCustomQueries.queryByAutomationId('id')
screenWithCustomQueries.getAllByAutomationId('id')
screenWithCustomQueries.getByAutomationId(['id', 'automationId'])
await screenWithCustomQueries.findAllByAutomationId('id', {}, {timeout: 1000})
await screenWithCustomQueries.findByAutomationId('id', {}, {timeout: 1000})
}
export function testBoundFunctions() {
const boundfunctions = {} as BoundFunctions<{
customQueryOne: (container: HTMLElement, text: string) => HTMLElement
customQueryTwo: (
container: HTMLElement,
text: string,
text2: string,
) => HTMLElement
customQueryThree: (container: HTMLElement, number: number) => HTMLElement
}>
boundfunctions.customQueryOne('one')
boundfunctions.customQueryTwo('one', 'two')
boundfunctions.customQueryThree(3)
}
export async function testByRole() {
const element = document.createElement('button')
element.setAttribute('aria-hidden', 'true')
console.assert(queryByRole(element, 'button') === null)
console.assert(queryByRole(element, 'button', {hidden: true}) !== null)
console.assert(screen.queryByRole('button') === null)
console.assert(screen.queryByRole('button', {hidden: true}) !== null)
console.assert(
(await findByRole(element, 'button', undefined, {timeout: 10})) === null,
)
console.assert(
(await findByRole(element, 'button', {hidden: true}, {timeout: 10})) !==
null,
)
console.assert(
queryAllByRole(document.body, 'progressbar', {queryFallbacks: true})
.length === 1,
)
// `name` option
console.assert(queryByRole(element, 'button', {name: 'Logout'}) === null)
console.assert(queryByRole(element, 'button', {name: /^Log/}) === null)
console.assert(
queryByRole(element, 'button', {
name: (name, el) => name === 'Login' && el.hasAttribute('disabled'),
}) === null,
)
// allow to query for a role that isn't included in the types
console.assert(queryByRole(element, 'foo') === null)
console.assert(queryByRole(element, /foo/) === null)
console.assert(screen.queryByRole('foo') === null)
console.assert(screen.queryByRole(/foo/) === null)
}
export function testA11yHelper() {
const element = document.createElement('svg')
console.assert(!isInaccessible(element))
}
export function eventTest() {
fireEvent.popState(window, {
location: 'http://www.example.com/?page=1',
state: {page: 1},
})
// HTMLElement
const element = document.createElement('div')
fireEvent.click(getByText(element, 'foo'))
// ChildNode
const child = document.createElement('div')
element.appendChild(child)
if (!element.firstChild) {
// Narrow Type
throw new Error(`Can't find firstChild`)
}
fireEvent.click(element.firstChild)
// Custom event
const customEvent = createEvent('customEvent', element)
fireEvent(element, customEvent)
}
export async function testWaitFors() {
const element = document.createElement('div')
await waitFor(() => getByText(element, 'apple'))
await waitFor(() => getAllByText(element, 'apple'))
const result: HTMLSpanElement = await waitFor(() =>
getByText(element, 'apple'),
)
if (!result) {
// Use value
throw new Error(`Can't find result`)
}
element.innerHTML = '<span>apple</span>'
await waitForElementToBeRemoved(() => getByText(element, 'apple'), {
interval: 3000,
container: element,
timeout: 5000,
})
await waitForElementToBeRemoved(getByText(element, 'apple'))
await waitForElementToBeRemoved(getAllByText(element, 'apple'))
await waitFor(async () => {})
}
export async function testWithin() {
const container = within(document.body)
container.queryAllByLabelText('Some label')
container.getByText('Click me')
container.getByText<HTMLButtonElement>('Click me')
container.getAllByText<HTMLButtonElement>('Click me')
await container.findByRole('button', {name: /click me/i})
container.getByRole<HTMLButtonElement>('button', {name: /click me/i})
let withinQueries = within(document.body)
withinQueries = within(document.body)
withinQueries.getByRole<HTMLButtonElement>('button', {name: /click me/i})
withinQueries = within(document.body)
withinQueries.getByRole<HTMLButtonElement>('button', {name: /click me/i})
}
/*
eslint
@typescript-eslint/no-unnecessary-condition: "off",
import/no-extraneous-dependencies: "off"
*/