Skip to content

Commit 432f702

Browse files
committed
pare down content for readability
1 parent 68a66e0 commit 432f702

File tree

1 file changed

+16
-49
lines changed

1 file changed

+16
-49
lines changed

packages/dev/s2-docs/pages/react-aria/testing.mdx

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default Layout;
55

66
import docs from 'docs:react-aria-components';
77
import testUtilDocs from 'docs:@react-aria/test-utils';
8+
import {InlineAlert, Heading, Content} from '@react-spectrum/s2'
89

910
export const section = 'Guides';
1011
export const description = 'Writing tests for apps built with React Aria';
@@ -13,28 +14,12 @@ export const description = 'Writing tests for apps built with React Aria';
1314

1415
<PageDescription>This page describes how to test an application built with React Aria. It documents the available testing utilities available for each aria pattern and how they can be used to simulate common user interactions.</PageDescription>
1516

16-
## Introduction
17-
18-
Running automated tests on your application helps ensure that it continues to work as expected over time.
19-
You can use testing tools like [React Testing Library](https://testing-library.com/docs/react-testing-library/intro) along with test runners like
20-
[Jest](https://jestjs.io) or [Mocha](https://mochajs.org) to test applications built with React Aria Components or hooks. These generally
21-
work quite well out of the box but there are a few things to consider to ensure your tests are the best they
22-
can be.
23-
24-
The information below covers best practices when writing tests, and be sure to checkout our [test utils](./react-aria-test-utils) that incorporate these
25-
strategies under the hood, helping streamline the test writing practice for you.
26-
2717
## Testing semantics
2818

29-
Many testing libraries expect you to query for elements in the DOM tree. For example, you might have a test
30-
that renders a login page, finds the username and password fields, and simulates filling them out and
31-
submitting the form.
32-
3319
The recommended way to query for React Aria Components and their internals is by semantics. React Aria
3420
Components implement [ARIA patterns](https://www.w3.org/TR/wai-aria-practices-1.2/). ARIA is a W3C standard
35-
that specifies the semantics for many UI components. This is used to expose them to assistive technology
36-
such as screen readers, but can also be used in tests to operate the application programmatically. These semantics
37-
are much less likely to change over time, and while other DOM nodes may be added or removed, the semantics are more likely to stay stable.
21+
that specifies the semantics for many UI components. Unlike the DOM structure of the component, these semantics are much less likely to change over time,
22+
making them ideal to query for.
3823

3924
The main attribute to look for when querying is the [role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques).
4025
This attribute represents the type of element a DOM node represents, e.g. a button, list option, or tab.
@@ -55,19 +40,12 @@ let option = tree.getByRole('button');
5540

5641
## Test ids
5742

58-
Querying by semantics covers many scenarios, but what if you have many buttons on a page? How do you find the specific button
59-
you're looking for in a test? In many cases this could be done by querying by the text in the button or an
60-
accessibility label associated with it, but sometimes this might change over time or may be affected by things like
61-
translations in different languages. In these cases, you may need a way to identify specific elements in tests, and that's
62-
where test ids come in.
43+
Querying by semantics covers many scenarios, but what if you have many buttons on a page or its text changes due to translations based on locale?
44+
In these cases, you may need a way to identify specific elements in tests, and that's where test ids come in.
6345

6446
React Aria Components pass all [data attributes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes)
6547
through to their underlying DOM nodes, which allows you to use an attribute like `data-testid` to identify
66-
a particular instance of a component. For example, you could add test ids to the two input elements
67-
in a login form and use them to find the username and password fields.
68-
69-
This example uses React Testing Library, but the idea could be applied in a similar way with other
70-
testing libraries.
48+
a particular instance of a component.
7149

7250
```tsx
7351
import {render} from '@testing-library/react';
@@ -95,25 +73,13 @@ let password = tree.getByTestId('password');
9573

9674
## Triggering events
9775

98-
Most testing libraries include a way to simulate events on an element. React Aria Components rely on
99-
many different browser events to support different devices and platforms, so it's important to simulate
100-
these correctly in your tests. For example, rather than only simulating a click event, the tests should
101-
simulate all of the events that would occur if a real user were interacting with the component.
102-
103-
For example, a click is really a `mousemove` and `mouseover` the target, followed
104-
by `mousedown`, `focus`, and `mouseup` events, and finally a `click` event. If you only simulated the `click`
105-
event, you would be missing all of these other preceding events that occur in real-world situations and this
106-
may make your test not work correctly. The implementation of the component may also change in the future to
107-
expect these events, making your test brittle. In addition, browsers have default behavior that occurs on
108-
certain events which would be missing, like focusing elements on mouse down, and toggling checkboxes on click.
76+
React Aria Components rely on many different browser events to support different devices and platforms, so it's important to simulate
77+
these correctly in your tests. For example, a click is really a `mousemove` and `mouseover` the target, followed
78+
by `mousedown`, `focus`, and `mouseup` events, and finally a `click` event.
10979

11080
The best way to handle this is with the [user-event](https://github.com/testing-library/user-event) library.
11181
This lets you trigger high level interactions like a user would, and the library handles firing all of the individual
112-
events that make up that interaction. If you use this library rather than firing events manually, your tests
113-
will simulate real-world behavior much better and work as expected.
114-
115-
`user-event` can handle many types of interactions, e.g. clicks, tabbing, typing, etc. This example shows how you could
116-
use it to render a login form and enter text in each field and click the submit button, just as a real user would.
82+
events that make up that interaction.
11783

11884
```tsx
11985
import {render} from '@testing-library/react';
@@ -140,16 +106,17 @@ TODO can't place this next to the header here
140106
<VersionBadge version="beta" />
141107

142108
[@react-aria/test-utils](https://www.npmjs.com/package/@react-aria/test-utils) is a set of testing utilities that aims to make writing unit tests easier for consumers of React Aria
143-
or for users who have built their own components following the respective ARIA pattern specification. By using the ARIA specification for any given component pattern as a source of truth,
144-
we can make assumptions about the existence of various aria attributes in a component. This allows us to navigate the component's DOM structure, simulate common interactions, and verify the
145-
the resulting state of the component.
109+
or for users who have built their own components following the respective ARIA pattern specification.
146110

147111
### Installation
148112

149113
<InstallCommand pkg="@react-aria/test-utils" flags="--dev" />
150114

151-
Please note that this library uses [@testing-library/react@16](https://www.npmjs.com/package/@testing-library/react) and [@testing-library/user-event@14](https://www.npmjs.com/package/@testing-library/user-event). This means that you need
152-
to be on React 18+ in order for these utilities to work.
115+
<InlineAlert variant="notice">
116+
<Heading>Requirements</Heading>
117+
<Content>Please note that this library uses [@testing-library/react@16](https://www.npmjs.com/package/@testing-library/react) and [@testing-library/user-event@14](https://www.npmjs.com/package/@testing-library/user-event). This means that you need to be on React 18+ in order for these utilities to work.</Content>
118+
</InlineAlert>
119+
153120

154121
### Setup
155122

0 commit comments

Comments
 (0)