Skip to content

Commit 2f4741d

Browse files
committed
docs: tweaks
1 parent 4a6d67d commit 2f4741d

File tree

2 files changed

+85
-35
lines changed

2 files changed

+85
-35
lines changed

website/docs/JestMatchers.md

Lines changed: 75 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,84 @@ import TOCInline from '@theme/TOCInline';
1515
expect(element).toBeOnTheScreen()
1616
```
1717

18-
This allows you to assert whether element is in the element tree or not.
18+
This allows you to assert whether element is attached to the element tree or not. If you hold a reference to an element and it gets unmounted during the test it will no longer pass this assertion.
1919

20-
:::note
21-
This matchers requires element to be attached to the element tree. This might be useful if your holding a reference to an element and the element gets unmounted during the test.
22-
:::
23-
24-
## Element State
20+
## Element Content
2521

2622
### `toHaveTextContent()`
2723

2824
```ts
29-
expect(element).toHaveTextContent(text: TextMatch, options?: TextMatchOptions)
25+
expect(element).toHaveTextContent(t
26+
text: string | RegExp,
27+
options?: {
28+
exact?: boolean;
29+
normalizer?: (text: string) => string;
30+
},
31+
)
3032
```
3133

32-
This allows you to assert whether the given element has a text content or not. It accepts either `string` or `RegExp` matchers, as well as `TextMatchOptions`.
34+
This allows you to assert whether the given element has a text content or not. It accepts either `string` or `RegExp` matchers, as well as [text match options](Queries.md#text-match-options) of `exact` and `normalizer`.
3335

3436
When `text` parameter is `undefined` it will only check for existence of text content, and when `text` is defined it will check if the actual text content matches passed value.
3537

38+
### `toContainElement()`
39+
40+
```ts
41+
expect(container).toContainElement(
42+
element: ReactTestInstance | null,
43+
)
44+
```
45+
46+
This allows you to assert whether the given container element does contain another host element.
47+
48+
### `toBeEmptyElement()`
49+
50+
```ts
51+
expect(element).toBeEmptyElement()
52+
```
53+
54+
This allows you to assert whether the given element does not have any host child elements nor text content.
55+
56+
57+
58+
59+
60+
## Element State
61+
3662
### `toHaveDisplayValue()`
3763

3864
```ts
39-
expect(element).toHaveDisplayValue(value: TextMatch, options?: TextMatchOptions)
65+
expect(element).toHaveDisplayValue(
66+
value: string | RegExp,
67+
options?: {
68+
exact?: boolean;
69+
normalizer?: (text: string) => string;
70+
},
71+
)
4072
```
4173

42-
This allows you to assert whether the given `TextInput` element has specified display value. It accepts either `string` or `RegExp` matchers, as well as `TextMatchOptions`.
74+
This allows you to assert whether the given `TextInput` element has specified display value. It accepts either `string` or `RegExp` matchers, as well as [text match options](Queries.md#text-match-options) of `exact` and `normalizer`.
4375

4476
### `toHaveAccessibleValue()`
4577

4678
```ts
47-
expect(element).toHaveAccessibleValue(...)
79+
expect(element).toHaveAccessibleValue(
80+
value: {
81+
min?: number;
82+
max?: number;
83+
now?: number;
84+
text?: string | RegExp;
85+
},
86+
)
4887
```
4988

5089
This allows you to assert whether the given element has specified accessible value.
5190

91+
This matcher will assert accessibility value based on `aria-valuemin`, `aria-valuemax`, `aria-valuenow`, `aria-valuetext` and `accessibilityValue` props. Only defined value entires will be used in the assertion, the element might have additional accessibility value entries and still be matched.
92+
93+
When querying by `text` entry a string or `RegExp` might be used.
94+
95+
5296
### `toBeEnabled()` / `toBeDisabled`
5397

5498
```ts
@@ -59,7 +103,7 @@ expect(element).toBeDisabled()
59103
These allows you to assert whether the given element is enabled or disabled from user's perspective. It relies on accessibility disabled state as set by `aria-disabled` or `accessibilityState.disabled` props. It will considers given element disabled when it or any of its ancestors is disabled.
60104

61105
:::note
62-
This matchers are direct negation of each other, and both are probivided to avoid double negations in your assertions.
106+
This matchers are negation of each other, and both are probivided to avoid double negations in your assertions.
63107
:::
64108

65109

@@ -96,7 +140,7 @@ expect(element).toBeCollapsed()
96140
These allows you to assert whether the given element is expanded or collapsed from user's perspective. It relies on accessibility disabled state as set by `aria-expanded` or `accessibilityState.expanded` props.
97141

98142
:::note
99-
This matchers are direct negation of each other for expandable elements (elements with explicit `aria-expanded` or `accessibilityState.expanded` props). However, both with fail for non-expandable elements (ones without explicit `aria-expanded` or `accessibilityState.expanded` props).
143+
This matchers are negation of each other for expandable elements (elements with explicit `aria-expanded` or `accessibilityState.expanded` props). However, both won't pass for non-expandable elements (ones without explicit `aria-expanded` or `accessibilityState.expanded` props).
100144
:::
101145

102146

@@ -123,35 +167,40 @@ The element is considered invisibile when it or any of its ancestors has `displa
123167
### `toHaveStyle()`
124168

125169
```ts
126-
expect(element).toHaveStyle(style: StyleProp<Style>)
170+
expect(element).toHaveStyle(
171+
style: StyleProp<Style>,
172+
)
127173
```
128174

129175
This allows you to assert whether the given element has given styles.
130176

131-
132177
## Other
133178

134-
### `toBeEmptyElement()`
179+
### `toHaveAccessibleName()`
135180

136181
```ts
137-
expect(element).toBeEmptyElement()
182+
expect(element).toHaveAccessibleName(
183+
name?: string | RegExp,
184+
options?: {
185+
exact?: boolean;
186+
normalizer?: (text: string) => string;
187+
},
188+
)
138189
```
139190

140-
This allows you to assert whether the given element does not have any host child elements nor text content.
191+
This allows you to assert whether the given element has specified accessible name. It accepts either `string` or `RegExp` matchers, as well as [text match options](Queries.md#text-match-options) of `exact` and `normalizer`.
141192

193+
Accessible name will be computed based on `aria-labelledby`, `accessibilityLabelledBy`, `aria-label`, `accessibilityLabel` props, in the absence of these props, element text content will be used.
142194

143-
### `toContainElement()`
144-
145-
```ts
146-
expect(container).toContainElement(element: ReactTestInstance | null)
147-
```
148-
149-
This allows you to assert whether the given container element does contain another host element.
195+
When `name` parameter is `undefined` it will only check if element has any accessible name.
150196

151197
### `toHaveProp()`
152198

153199
```ts
154-
expect(element).toHaveProp(name: string, value?: unknown)
200+
expect(element).toHaveProp(
201+
name: string,
202+
value?: unknown,
203+
)
155204
```
156205

157206
This allows you to assert whether the given element has a given prop. When `value` parameter is `undefined` it will only check for existence of prop, and when `value` is defined it will check if the actual value matches passed value.

website/docs/Queries.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ getByDisplayValue(
184184
exact?: boolean;
185185
normalizer?: (text: string) => string;
186186
includeHiddenElements?: boolean;
187-
}
187+
},
188188
): ReactTestInstance;
189189
```
190190

@@ -208,7 +208,7 @@ getByTestId(
208208
exact?: boolean;
209209
normalizer?: (text: string) => string;
210210
includeHiddenElements?: boolean;
211-
}
211+
},
212212
): ReactTestInstance;
213213
```
214214

@@ -236,7 +236,7 @@ getByLabelText(
236236
exact?: boolean;
237237
normalizer?: (text: string) => string;
238238
includeHiddenElements?: boolean;
239-
}
239+
},
240240
): ReactTestInstance;
241241
```
242242

@@ -264,7 +264,7 @@ getByHintText(
264264
exact?: boolean;
265265
normalizer?: (text: string) => string;
266266
includeHiddenElements?: boolean;
267-
}
267+
},
268268
): ReactTestInstance;
269269
```
270270

@@ -305,7 +305,7 @@ getByA11yState(
305305
},
306306
options?: {
307307
includeHiddenElements?: boolean;
308-
}
308+
},
309309
): ReactTestInstance;
310310
```
311311

@@ -367,7 +367,7 @@ getByA11yValue(
367367
},
368368
options?: {
369369
includeHiddenElements?: boolean;
370-
}
370+
},
371371
): ReactTestInstance;
372372
```
373373

@@ -457,7 +457,8 @@ screen.getByText('Goodbye World');
457457
screen.getByText(/hello world/);
458458
```
459459

460-
### Precision
460+
### Options {#text-match-options}
461+
#### Precision
461462

462463
```typescript
463464
type TextMatchOptions = {
@@ -475,7 +476,7 @@ Queries that take a `TextMatch` also accept an object as the second argument tha
475476

476477
`exact` option defaults to `true` but if you want to search for a text slice or make text matching case-insensitive you can override it. That being said we advise you to use regex in more complex scenarios.
477478

478-
### Normalization
479+
#### Normalization
479480

480481
Before running any matching logic against text, it is automatically normalized. By default, normalization consists of trimming whitespace from the start and end of text, and collapsing multiple adjacent whitespace characters into a single space.
481482

@@ -490,7 +491,7 @@ Specifying a value for `normalizer` replaces the built-in normalization, but you
490491
- `trim`: Defaults to `true`. Trims leading and trailing whitespace.
491492
- `collapseWhitespace`: Defaults to `true`. Collapses inner whitespace (newlines, tabs repeated spaces) into a single space.
492493

493-
#### Normalization Examples
494+
##### Normalization Examples
494495

495496
To perform a match against text without trimming:
496497

0 commit comments

Comments
 (0)