-
Notifications
You must be signed in to change notification settings - Fork 274
docs: Jest matchers docs #1506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
docs: Jest matchers docs #1506
Changes from 8 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d640886
docs: v1 of jest matchers docs
mdjastrzebski 5cf655f
chore: add TOC
mdjastrzebski 4a6d67d
chore: tweak heading levels
mdjastrzebski 2f4741d
docs: tweaks
mdjastrzebski 01d5050
docs: cross references
mdjastrzebski 1a43fc5
docs: tweaks
mdjastrzebski e175f31
docs: tweaks
mdjastrzebski 63ebf4a
docs: tweaks
mdjastrzebski c1d1b7b
Update website/docs/JestMatchers.md
mdjastrzebski e548226
Update website/docs/JestMatchers.md
mdjastrzebski 6e65f53
Update website/docs/JestMatchers.md
mdjastrzebski 2e356d0
Update website/docs/JestMatchers.md
mdjastrzebski 18b4227
Update website/docs/JestMatchers.md
mdjastrzebski b7d0086
Update website/docs/JestMatchers.md
mdjastrzebski 86317fc
Update website/docs/MigrationJestMatchers.md
mdjastrzebski 744a0c1
Update website/docs/MigrationJestMatchers.md
mdjastrzebski e1fa4e4
docs: spellchecking
mdjastrzebski 7f272dd
chore: final fixes
mdjastrzebski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
--- | ||
id: jest-matchers | ||
title: Jest Matchers | ||
--- | ||
|
||
:::note | ||
Built-in Jest matchers require RNTL v12.4.0 or later. | ||
::: | ||
|
||
This guide describes built-in Jest matchers, we recommend using these matchers as they provide more readable tests, better accessibility support and better developer experience. | ||
|
||
If you are already using legacy Jest Native matchers we have a [migration guide](migration-jest-native) for moving to the built-in matchers. | ||
|
||
import TOCInline from '@theme/TOCInline'; | ||
|
||
<TOCInline toc={toc} /> | ||
|
||
## Element Existence | ||
|
||
### `toBeOnTheScreen()` | ||
|
||
```ts | ||
expect(element).toBeOnTheScreen() | ||
``` | ||
|
||
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. | ||
|
||
## Element Content | ||
|
||
### `toHaveTextContent()` | ||
|
||
```ts | ||
expect(element).toHaveTextContent(t | ||
text: string | RegExp, | ||
options?: { | ||
exact?: boolean; | ||
normalizer?: (text: string) => string; | ||
}, | ||
) | ||
``` | ||
|
||
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`. | ||
|
||
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. | ||
mdjastrzebski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### `toContainElement()` | ||
|
||
```ts | ||
expect(container).toContainElement( | ||
element: ReactTestInstance | null, | ||
) | ||
``` | ||
|
||
This allows you to assert whether the given container element does contain another host element. | ||
|
||
### `toBeEmptyElement()` | ||
|
||
```ts | ||
expect(element).toBeEmptyElement() | ||
``` | ||
|
||
This allows you to assert whether the given element does not have any host child elements nor text content. | ||
|
||
|
||
|
||
|
||
|
||
## Element State | ||
|
||
### `toHaveDisplayValue()` | ||
|
||
```ts | ||
expect(element).toHaveDisplayValue( | ||
value: string | RegExp, | ||
options?: { | ||
exact?: boolean; | ||
normalizer?: (text: string) => string; | ||
}, | ||
) | ||
``` | ||
|
||
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`. | ||
|
||
### `toHaveAccessibilityValue()` | ||
|
||
```ts | ||
expect(element).toHaveAccessibilityValue( | ||
value: { | ||
min?: number; | ||
max?: number; | ||
now?: number; | ||
text?: string | RegExp; | ||
}, | ||
) | ||
``` | ||
|
||
This allows you to assert whether the given element has specified accessible value. | ||
|
||
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. | ||
|
||
When querying by `text` entry a string or `RegExp` might be used. | ||
|
||
|
||
### `toBeEnabled()` / `toBeDisabled` {#tobeenabled} | ||
|
||
```ts | ||
expect(element).toBeEnabled() | ||
expect(element).toBeDisabled() | ||
``` | ||
|
||
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. | ||
|
||
:::note | ||
This matchers are negation of each other, and both are probivided to avoid double negations in your assertions. | ||
mdjastrzebski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
::: | ||
|
||
|
||
### `toBeSelected()` | ||
|
||
```ts | ||
expect(element).toBeSelected() | ||
``` | ||
|
||
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. | ||
|
||
|
||
### `toBeChecked()` / `toBePartiallyChecked()` {#tobechecked} | ||
|
||
```ts | ||
expect(element).toBeChecked() | ||
expect(element).toBePartiallyChecked() | ||
``` | ||
|
||
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. | ||
|
||
:::note | ||
* `toBeChecked()` matcher works only on elements with `checkbox` or `radio` role. | ||
* `toBePartiallyChecked()` matchers works only on elements with `checkbox` role. | ||
mdjastrzebski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
::: | ||
|
||
### `toBeExpanded()` / `toBeCollapsed()` {#tobeexpanded} | ||
|
||
```ts | ||
expect(element).toBeExpanded() | ||
expect(element).toBeCollapsed() | ||
``` | ||
|
||
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. | ||
|
||
:::note | ||
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). | ||
mdjastrzebski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
::: | ||
|
||
|
||
### `toBeBusy()` | ||
|
||
```ts | ||
expect(element).toBeBusy() | ||
``` | ||
|
||
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. | ||
|
||
## Element Styles | ||
|
||
### `toBeVisible()` | ||
|
||
```ts | ||
expect(element).toBeVisible() | ||
``` | ||
|
||
This allows you to assert whether the given element is visible from user's perspective. | ||
|
||
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. | ||
mdjastrzebski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### `toHaveStyle()` | ||
|
||
```ts | ||
expect(element).toHaveStyle( | ||
style: StyleProp<Style>, | ||
) | ||
``` | ||
|
||
This allows you to assert whether the given element has given styles. | ||
|
||
## Other | ||
|
||
### `toHaveAccessibleName()` | ||
|
||
```ts | ||
expect(element).toHaveAccessibleName( | ||
name?: string | RegExp, | ||
options?: { | ||
exact?: boolean; | ||
normalizer?: (text: string) => string; | ||
}, | ||
) | ||
``` | ||
|
||
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`. | ||
|
||
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. | ||
|
||
When `name` parameter is `undefined` it will only check if element has any accessible name. | ||
|
||
### `toHaveProp()` | ||
|
||
```ts | ||
expect(element).toHaveProp( | ||
name: string, | ||
value?: unknown, | ||
) | ||
``` | ||
|
||
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. | ||
|
||
:::note | ||
This matchers should be treated as escape hatch to be used when all other matchers are not suitable. | ||
mdjastrzebski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
::: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
--- | ||
id: migration-jest-native | ||
title: Migration from Jest Native matchers | ||
--- | ||
|
||
This guide describes steps necessary to migrate from [legacy Jest Native matchers v5](https://github.com/testing-library/jest-native) to [built-in Jest matchers](jest-matchers). | ||
mdjastrzebski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import TOCInline from '@theme/TOCInline'; | ||
|
||
<TOCInline toc={toc} /> | ||
|
||
## General notes | ||
|
||
All of built-in Jest matchers provided by React Native Testing Library are supporting only host elements. This should not be an issue, as all RNTL v12 queries already return only host elements. When this guide states that given matcher should work the same it assumes behavior only only host elements. If you need to assert status of composite elements use Jest Native matchers in [legacy mode](#gradual-migration). | ||
|
||
## Usage | ||
|
||
You can use the built-in matchers by adding following line to your `jest-setup.ts` file: | ||
|
||
```ts | ||
import '@testing-library/react-native/extend-expect'; | ||
``` | ||
|
||
### Gradual migration | ||
|
||
You can use the built-in matchers alongside with legacy Jest Native matchers by changing their import in your `jest-setup.ts` file: | ||
|
||
```ts | ||
// Replace this: | ||
// import '@testing-library/jest-native/extend-expect'; | ||
|
||
// With this: | ||
import '@testing-library/react-native/extend-expect'; | ||
import '@testing-library/jest-native/legacy-extend-expect'; | ||
``` | ||
|
||
In such case legacy matchers will be available using `legacy_` prefix, e.g.: | ||
|
||
```ts | ||
expect(element).legacy_toHaveAccessibilityState({ busy: true }); | ||
``` | ||
|
||
## Migration details | ||
|
||
### Matchers not requiring changes | ||
|
||
Following matchers should work the same: | ||
* [`toBeEmptyElement()`](jest-matchers#tobeemptyelement) | ||
* [`toBeEnabled()` / `toBeDisabled()`](jest-matchers#tobeenabled) | ||
* [`toBeOnTheScreen()`](jest-matchers#tobeonthescreen) | ||
* [`toBeVisible()`](jest-matchers#tobevisible) | ||
* [`toContainElement()`](jest-matchers#tocontainelement) | ||
* [`toHaveAccessibilityValue()`](jest-matchers#tohaveaccessibilityvalue) | ||
* [`toHaveDisplayValue()`](jest-matchers#tohavedisplayvalue) | ||
* [`toHaveProp()`](jest-matchers#tohaveprop) | ||
* [`toHaveStyle()`](jest-matchers#tohavestyle) | ||
* [`toHaveTextContent()`](jest-matchers#tohavetextcontent) | ||
|
||
### Replaced matchers | ||
|
||
`toHaveAccessibilityState()` matcher has been replaced by following matchers: | ||
* enabled state: [`toBeEnabled()` / `toBeDisabled()`](jest-matchers#tobeenabled) | ||
* checked state: [`toBeChecked()` / `toBePartiallyChecked()`](jest-matchers#tobechecked) | ||
* selected state: [`toBeSelected()`](jest-matchers#tobeselected) | ||
* expanded state: [`toBeExpanded()` / `toBeCollapsed()`](jest-matchers#tobeexpanded) | ||
* busy state: [`toBeBusy()`](jest-matchers#tobebusy) | ||
|
||
The new matchers support both `accessbililityState` and `aria-*` props. | ||
|
||
### Added matchers | ||
|
||
New [`toHaveAccessibleName()`](jest-matchers#tohaveaccessiblename) has been added. | ||
|
||
### Noteworthy details | ||
|
||
You should be aware of following changes: | ||
* [`toBeEnabled()` / `toBeDisabled()`](jest-matchers#tobeenabled) matchers also check the disabled state for element ancestors and not only the element itself | ||
mdjastrzebski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* [`toBeChecked()`](jest-matchers#tobechecked) matcher supports only elements with `checkbox` or `radio` role | ||
* [`toBePartiallyChecked()`](jest-matchers#tobechecked) matchers supports only elements with `checkbox` role | ||
mdjastrzebski marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.