You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/docs/JestMatchers.md
+75-26Lines changed: 75 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -15,40 +15,84 @@ import TOCInline from '@theme/TOCInline';
15
15
expect(element).toBeOnTheScreen()
16
16
```
17
17
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.
19
19
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.
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`.
33
35
34
36
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.
35
37
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.
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`.
43
75
44
76
### `toHaveAccessibleValue()`
45
77
46
78
```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
+
)
48
87
```
49
88
50
89
This allows you to assert whether the given element has specified accessible value.
51
90
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
+
52
96
### `toBeEnabled()` / `toBeDisabled`
53
97
54
98
```ts
@@ -59,7 +103,7 @@ expect(element).toBeDisabled()
59
103
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.
60
104
61
105
:::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.
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.
97
141
98
142
:::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).
100
144
:::
101
145
102
146
@@ -123,35 +167,40 @@ The element is considered invisibile when it or any of its ancestors has `displa
This allows you to assert whether the given element has given styles.
130
176
131
-
132
177
## Other
133
178
134
-
### `toBeEmptyElement()`
179
+
### `toHaveAccessibleName()`
135
180
136
181
```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
+
)
138
189
```
139
190
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`.
141
192
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.
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.
@@ -475,7 +476,7 @@ Queries that take a `TextMatch` also accept an object as the second argument tha
475
476
476
477
`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.
477
478
478
-
### Normalization
479
+
####Normalization
479
480
480
481
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.
481
482
@@ -490,7 +491,7 @@ Specifying a value for `normalizer` replaces the built-in normalization, but you
490
491
-`trim`: Defaults to `true`. Trims leading and trailing whitespace.
491
492
-`collapseWhitespace`: Defaults to `true`. Collapses inner whitespace (newlines, tabs repeated spaces) into a single space.
0 commit comments