Skip to content

Commit a57604c

Browse files
committed
fix(types): allow all elements
1 parent a997acd commit a57604c

File tree

7 files changed

+78
-83
lines changed

7 files changed

+78
-83
lines changed

docs/dom-testing-library/api-async.mdx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ All the async utils are built on top of `waitFor`.
1515
function waitFor<T>(
1616
callback: () => T | Promise<T>,
1717
options?: {
18-
container?: HTMLElement
18+
container?: Element
1919
timeout?: number
2020
interval?: number
2121
onTimeout?: (error: Error) => Error
@@ -70,7 +70,7 @@ changes. When any of those changes occur, it will re-run the callback.
7070
function waitForElementToBeRemoved<T>(
7171
callback: (() => T) | T,
7272
options?: {
73-
container?: HTMLElement
73+
container?: Element
7474
timeout?: number
7575
interval?: number
7676
onTimeout?: (error: Error) => Error
@@ -106,14 +106,14 @@ el.parentElement.removeChild(el)
106106
or an empty array:
107107

108108
```javascript
109-
waitForElementToBeRemoved(null).catch(err => console.log(err))
110-
waitForElementToBeRemoved(queryByText(/not here/i)).catch(err =>
109+
waitForElementToBeRemoved(null).catch((err) => console.log(err))
110+
waitForElementToBeRemoved(queryByText(/not here/i)).catch((err) =>
111111
console.log(err)
112112
)
113-
waitForElementToBeRemoved(queryAllByText(/not here/i)).catch(err =>
113+
waitForElementToBeRemoved(queryAllByText(/not here/i)).catch((err) =>
114114
console.log(err)
115115
)
116-
waitForElementToBeRemoved(() => getByText(/not here/i)).catch(err =>
116+
waitForElementToBeRemoved(() => getByText(/not here/i)).catch((err) =>
117117
console.log(err)
118118
)
119119

@@ -128,7 +128,7 @@ The options object is forwarded to `waitFor`.
128128
function wait<T>(
129129
callback: () => void,
130130
options?: {
131-
container?: HTMLElement
131+
container?: Element
132132
timeout?: number
133133
interval?: number
134134
mutationObserverOptions?: MutationObserverInit
@@ -148,7 +148,7 @@ practice to use an empty callback because it will make the tests more fragile.
148148

149149
```typescript
150150
function waitForDomChange<T>(options?: {
151-
container?: HTMLElement
151+
container?: Element
152152
timeout?: number
153153
mutationObserverOptions?: MutationObserverInit
154154
}): Promise<T>
@@ -165,7 +165,7 @@ changed:
165165
const container = document.createElement('div')
166166
waitForDomChange({ container })
167167
.then(() => console.log('DOM changed!'))
168-
.catch(err => console.log(`Error you need to deal with: ${err}`))
168+
.catch((err) => console.log(`Error you need to deal with: ${err}`))
169169
container.append(document.createElement('p'))
170170
// if 👆 was the only code affecting the container and it was not run,
171171
// waitForDomChange would throw an error
@@ -179,7 +179,7 @@ container
179179
```javascript
180180
const container = document.createElement('div')
181181
container.setAttribute('data-cool', 'true')
182-
waitForDomChange({ container }).then(mutationsList => {
182+
waitForDomChange({ container }).then((mutationsList) => {
183183
const mutation = mutationsList[0]
184184
console.log(
185185
`was cool: ${mutation.oldValue}\ncurrently cool: ${mutation.target.dataset.cool}`
@@ -211,7 +211,7 @@ changes.
211211
function waitForElement<T>(
212212
callback: () => T,
213213
options?: {
214-
container?: HTMLElement
214+
container?: Element
215215
timeout?: number
216216
mutationObserverOptions?: MutationObserverInit
217217
}

docs/dom-testing-library/api-events.mdx

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ id: api-events
33
title: Firing Events
44
---
55

6-
import Tabs from '@theme/Tabs';
7-
import TabItem from '@theme/TabItem';
6+
import Tabs from '@theme/Tabs'
7+
import TabItem from '@theme/TabItem'
88

99
> Most projects have a few use cases for `fireEvent`, but the majority of the
1010
> time you should probably use
@@ -13,7 +13,7 @@ import TabItem from '@theme/TabItem';
1313
## `fireEvent`
1414

1515
```typescript
16-
fireEvent(node: HTMLElement, event: Event)
16+
fireEvent(node: Element, event: Event)
1717
```
1818

1919
Fire DOM events.
@@ -32,7 +32,7 @@ fireEvent(
3232
## `fireEvent[eventName]`
3333

3434
```typescript
35-
fireEvent[eventName](node: HTMLElement, eventProperties: Object)
35+
fireEvent[eventName](node: Element, eventProperties: Object)
3636
```
3737

3838
Convenience methods for firing DOM events. Check out
@@ -107,7 +107,7 @@ You can find out which key code to use at
107107
## `createEvent[eventName]`
108108

109109
```typescript
110-
createEvent[eventName](node: HTMLElement, eventProperties: Object)
110+
createEvent[eventName](node: Element, eventProperties: Object)
111111
```
112112

113113
Convenience methods for creating DOM events that can then be fired by
@@ -149,20 +149,20 @@ bound callback.
149149
}>
150150
<TabItem value="react">
151151

152-
```jsx
153-
import { render, screen, fireEvent } from '@testing-library/react'
152+
```jsx
153+
import { render, screen, fireEvent } from '@testing-library/react'
154154

155-
const Button = ({ onClick, children }) => (
156-
<button onClick={onClick}>{children}</button>
157-
)
155+
const Button = ({ onClick, children }) => (
156+
<button onClick={onClick}>{children}</button>
157+
)
158158

159-
test('calls onClick prop when clicked', () => {
160-
const handleClick = jest.fn()
161-
render(<Button onClick={handleClick}>Click Me</Button>)
162-
fireEvent.click(screen.getByText(/click me/i))
163-
expect(handleClick).toHaveBeenCalledTimes(1)
164-
})
165-
```
159+
test('calls onClick prop when clicked', () => {
160+
const handleClick = jest.fn()
161+
render(<Button onClick={handleClick}>Click Me</Button>)
162+
fireEvent.click(screen.getByText(/click me/i))
163+
expect(handleClick).toHaveBeenCalledTimes(1)
164+
})
165+
```
166166

167167
</TabItem>
168168
</Tabs>

docs/dom-testing-library/api-helpers.mdx

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ id: api-helpers
33
title: Helpers
44
---
55

6-
import Tabs from '@theme/Tabs';
7-
import TabItem from '@theme/TabItem';
6+
import Tabs from '@theme/Tabs'
7+
import TabItem from '@theme/TabItem'
88

99
## Custom Queries
1010

@@ -68,8 +68,7 @@ module.exports = {
6868
The `buildQueries` helper allows you to create custom queries with all standard
6969
[variants](api-queries.mdx) of queries in testing-library.
7070

71-
See the
72-
[Add custom queries](react-testing-library/setup.mdx#add-custom-queries)
71+
See the [Add custom queries](react-testing-library/setup.mdx#add-custom-queries)
7372
section of the custom render guide for example usage.
7473

7574
### Using other assertion libraries
@@ -87,7 +86,7 @@ and add it here!
8786
## `getNodeText`
8887

8988
```typescript
90-
getNodeText(node: HTMLElement)
89+
getNodeText(node: Element)
9190
```
9291

9392
Returns the complete text content of an HTML element, removing any extra
@@ -138,32 +137,32 @@ could do:
138137
}>
139138
<TabItem value="native">
140139

141-
```js
142-
import { within } from '@testing-library/dom'
140+
```js
141+
import { within } from '@testing-library/dom'
143142

144-
const { getByText } = within(document.getElementById('messages'))
145-
const helloMessage = getByText('hello')
146-
```
143+
const { getByText } = within(document.getElementById('messages'))
144+
const helloMessage = getByText('hello')
145+
```
147146

148147
</TabItem>
149148
<TabItem value="react">
150149

151-
```jsx
152-
import { render, within } from '@testing-library/react'
150+
```jsx
151+
import { render, within } from '@testing-library/react'
153152

154-
const { getByLabelText } = render(<MyComponent />)
155-
const signinModal = getByLabelText('Sign In')
156-
within(signinModal).getByPlaceholderText('Username')
157-
```
153+
const { getByLabelText } = render(<MyComponent />)
154+
const signinModal = getByLabelText('Sign In')
155+
within(signinModal).getByPlaceholderText('Username')
156+
```
158157

159158
</TabItem>
160159
<TabItem value="cypress">
161160

162-
```js
163-
cy.get('form').within(() => {
164-
cy.findByText('Button Text').should('be.disabled')
165-
})
166-
```
161+
```js
162+
cy.get('form').within(() => {
163+
cy.findByText('Button Text').should('be.disabled')
164+
})
165+
```
167166

168167
</TabItem>
169168
</Tabs>
@@ -258,11 +257,7 @@ tree of a node. This can be helpful for instance when debugging tests.
258257
It is defined as:
259258
260259
```typescript
261-
function prettyDOM(
262-
node: HTMLElement,
263-
maxLength?: number,
264-
options?: Options
265-
): string
260+
function prettyDOM(node: Element, maxLength?: number, options?: Options): string
266261
```
267262

268263
It receives the root node to print out, an optional extra parameter to limit the

docs/dom-testing-library/api-queries.mdx

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ id: api-queries
33
title: Queries
44
---
55

6-
import Tabs from '@theme/Tabs';
7-
import TabItem from '@theme/TabItem';
6+
import Tabs from '@theme/Tabs'
7+
import TabItem from '@theme/TabItem'
88

99
## Variants
1010

@@ -127,13 +127,13 @@ screen.debug(screen.getAllByText('multi-test'))
127127
128128
```typescript
129129
getByLabelText(
130-
container: HTMLElement, // if you're using `screen`, then skip this argument
130+
container: Element, // if you're using `screen`, then skip this argument
131131
text: TextMatch,
132132
options?: {
133133
selector?: string = '*',
134134
exact?: boolean = true,
135135
normalizer?: NormalizerFn,
136-
}): HTMLElement
136+
}): Element
137137
```
138138

139139
This will search for the label that matches the given [`TextMatch`](#textmatch),
@@ -244,12 +244,12 @@ const inputNode = screen.getByLabelText('Username', { selector: 'input' })
244244
245245
```typescript
246246
getByPlaceholderText(
247-
container: HTMLElement, // if you're using `screen`, then skip this argument
247+
container: Element, // if you're using `screen`, then skip this argument
248248
text: TextMatch,
249249
options?: {
250250
exact?: boolean = true,
251251
normalizer?: NormalizerFn,
252-
}): HTMLElement
252+
}): Element
253253
```
254254

255255
This will search for all elements with a placeholder attribute and find one that
@@ -301,14 +301,14 @@ cy.findByPlaceholderText('Username').should('exist')
301301
302302
```typescript
303303
getByText(
304-
container: HTMLElement, // if you're using `screen`, then skip this argument
304+
container: Element, // if you're using `screen`, then skip this argument
305305
text: TextMatch,
306306
options?: {
307307
selector?: string = '*',
308308
exact?: boolean = true,
309309
ignore?: string|boolean = 'script, style',
310310
normalizer?: NormalizerFn,
311-
}): HTMLElement
311+
}): Element
312312
```
313313

314314
This will search for all elements that have a text node with `textContent`
@@ -375,12 +375,12 @@ If you'd rather disable this behavior, set `ignore` to `false`.
375375
376376
```typescript
377377
getByAltText(
378-
container: HTMLElement, // if you're using `screen`, then skip this argument
378+
container: Element, // if you're using `screen`, then skip this argument
379379
text: TextMatch,
380380
options?: {
381381
exact?: boolean = true,
382382
normalizer?: NormalizerFn,
383-
}): HTMLElement
383+
}): Element
384384
```
385385

386386
This will return the element (normally an `<img>`) that has the given `alt`
@@ -433,12 +433,12 @@ cy.findByAltText(/incredibles.*? poster/i).should('exist')
433433
434434
```typescript
435435
getByTitle(
436-
container: HTMLElement, // if you're using `screen`, then skip this argument
436+
container: Element, // if you're using `screen`, then skip this argument
437437
title: TextMatch,
438438
options?: {
439439
exact?: boolean = true,
440440
normalizer?: NormalizerFn,
441-
}): HTMLElement
441+
}): Element
442442
```
443443

444444
Returns the element that has the matching `title` attribute.
@@ -493,12 +493,12 @@ cy.findByTitle('Close').should('exist')
493493
494494
```typescript
495495
getByDisplayValue(
496-
container: HTMLElement, // if you're using `screen`, then skip this argument
496+
container: Element, // if you're using `screen`, then skip this argument
497497
value: TextMatch,
498498
options?: {
499499
exact?: boolean = true,
500500
normalizer?: NormalizerFn,
501-
}): HTMLElement
501+
}): Element
502502
```
503503

504504
Returns the `input`, `textarea`, or `select` element that has the matching
@@ -635,7 +635,7 @@ cy.findByDisplayValue('Alaska').should('exist')
635635
636636
```typescript
637637
getByRole(
638-
container: HTMLElement, // if you're using `screen`, then skip this argument
638+
container: Element, // if you're using `screen`, then skip this argument
639639
role: TextMatch,
640640
options?: {
641641
exact?: boolean = true,
@@ -647,7 +647,7 @@ getByRole(
647647
pressed?: boolean,
648648
queryFallbacks?: boolean,
649649
level?: number,
650-
}): HTMLElement
650+
}): Element
651651
```
652652

653653
Queries for elements with the given role (and it also accepts a
@@ -884,12 +884,12 @@ To learn more about the `aria-level` property, see
884884
885885
```typescript
886886
getByTestId(
887-
container: HTMLElement, // if you're using `screen`, then skip this argument
887+
container: Element, // if you're using `screen`, then skip this argument
888888
text: TextMatch,
889889
options?: {
890890
exact?: boolean = true,
891891
normalizer?: NormalizerFn,
892-
}): HTMLElement
892+
}): Element
893893
```
894894

895895
A shortcut to `` container.querySelector(`[data-testid="${yourId}"]`) `` (and it
@@ -1006,7 +1006,7 @@ To override normalization to remove some Unicode characters whilst keeping some
10061006

10071007
```javascript
10081008
screen.getByText('text', {
1009-
normalizer: str =>
1009+
normalizer: (str) =>
10101010
getDefaultNormalizer({ trim: false })(str).replace(/[\u200E-\u200F]*/g, ''),
10111011
})
10121012
```

0 commit comments

Comments
 (0)