Skip to content

Commit d640886

Browse files
committed
docs: v1 of jest matchers docs
1 parent 2ada536 commit d640886

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

website/docs/JestMatchers.md

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
id: jest-matchers
3+
title: Jest Matchers
4+
---
5+
6+
# Element Existence
7+
8+
## `toBeOnTheScreen()`
9+
10+
```ts
11+
expect(element).toBeOnTheScreen()
12+
```
13+
14+
This allows you to assert whether element is in the element tree or not.
15+
16+
:::note
17+
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.
18+
:::
19+
20+
# Element State
21+
22+
## `toHaveTextContent()`
23+
24+
```ts
25+
expect(element).toHaveTextContent(text: TextMatch, options?: TextMatchOptions)
26+
```
27+
28+
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`.
29+
30+
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.
31+
32+
## `toHaveDisplayValue()`
33+
34+
```ts
35+
expect(element).toHaveDisplayValue(value: TextMatch, options?: TextMatchOptions)
36+
```
37+
38+
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`.
39+
40+
## `toHaveAccessibleValue()`
41+
42+
```ts
43+
expect(element).toHaveAccessibleValue(...)
44+
```
45+
46+
This allows you to assert whether the given element has specified accessible value.
47+
48+
## `toBeEnabled()` / `toBeDisabled`
49+
50+
```ts
51+
expect(element).toBeEnabled()
52+
expect(element).toBeDisabled()
53+
```
54+
55+
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.
56+
57+
:::note
58+
This matchers are direct negation of each other, and both are probivided to avoid double negations in your assertions.
59+
:::
60+
61+
62+
## `toBeSelected()`
63+
64+
```ts
65+
expect(element).toBeSelected()
66+
```
67+
68+
This allows you to assert whether the given element is selected from user's perspective. It relies on accessibility selected state as set by `aria-selected` or `accessibilityState.selected` props.
69+
70+
71+
## `toBeChecked()` / `toBePartiallyChecked()`
72+
73+
```ts
74+
expect(element).toBeChecked()
75+
expect(element).toBePartiallyChecked()
76+
```
77+
78+
These allows you to assert whether the given element is checked or partially checked from user's perspective. It relies on accessibility checked state as set by `aria-checked` or `accessibilityState.checked` props.
79+
80+
:::note
81+
* `toBeChecked()` matcher works only on elements with `checkbox` or `radio` role.
82+
* `toBePartiallyChecked()` matchers works only on elements with `checkbox` role.
83+
:::
84+
85+
## `toBeExpanded()` / `toBeCollapsed()`
86+
87+
```ts
88+
expect(element).toBeExpanded()
89+
expect(element).toBeCollapsed()
90+
```
91+
92+
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.
93+
94+
:::note
95+
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).
96+
:::
97+
98+
99+
## `toBeBusy()`
100+
101+
```ts
102+
expect(element).toBeBusy()
103+
```
104+
105+
This allows you to assert whether the given element is busy from user's perspective. It relies on accessibility selected state as set by `aria-busy` or `accessibilityState.busy` props.
106+
107+
# Element Styles
108+
109+
## `toBeVisible()`
110+
111+
```ts
112+
expect(element).toBeVisible()
113+
```
114+
115+
This allows you to assert whether the given element is visible from user's perspective.
116+
117+
The element is considered invisibile when it or any of its ancestors has `display: none` or `opacity: 0` styles, as well as when it's hidden from accessbility.
118+
119+
## `toHaveStyle()`
120+
121+
```ts
122+
expect(element).toHaveStyle(style: StyleProp<Style>)
123+
```
124+
125+
This allows you to assert whether the given element has given styles.
126+
127+
128+
# Other
129+
130+
## `toBeEmptyElement()`
131+
132+
```ts
133+
expect(element).toBeEmptyElement()
134+
```
135+
136+
This allows you to assert whether the given element does not have any host child elements nor text content.
137+
138+
139+
## `toContainElement()`
140+
141+
```ts
142+
expect(container).toContainElement(element: ReactTestInstance | null)
143+
```
144+
145+
This allows you to assert whether the given container element does contain another host element.
146+
147+
## `toHaveProp()`
148+
149+
```ts
150+
expect(element).toHaveProp(name: string, value?: unknown)
151+
```
152+
153+
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.
154+
155+
:::note
156+
This matchers should be treated as escape hatch to be used when all other matchers are not suitable.
157+
:::

0 commit comments

Comments
 (0)