type-tests.ts
5.23 KB
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
import {
createEvent,
fireEvent,
isInaccessible,
queries,
buildQueries,
queryAllByAttribute,
screen,
waitFor,
waitForElementToBeRemoved,
MatcherOptions,
} 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.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.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 [
queryByAutomationId,
getAllByAutomationId,
getByAutomationId,
findAllByAutomationId,
findByAutomationId,
] = buildQueries(
queryAllByAutomationId,
() => 'Multiple Error',
() => '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')
}
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 () => {})
}
/*
eslint
@typescript-eslint/no-unnecessary-condition: "off",
import/no-extraneous-dependencies: "off"
*/