Skip to content

Commit 5a10ff0

Browse files
committed
refactor test util content so it is in each page
1 parent 50c9e07 commit 5a10ff0

22 files changed

+1201
-668
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import docs from 'docs:react-aria-components';
55
import vanillaDocs from 'docs:vanilla-starter/CheckboxGroup';
66
import '../../tailwind/tailwind.css';
77
import Anatomy from '@react-aria/checkbox/docs/checkboxgroup-anatomy.svg';
8+
import testUtilDocs from 'docs:@react-aria/test-utils';
9+
import {InstallCommand} from '../../src/InstallCommand';
10+
import {InlineAlert, Heading, Content} from '@react-spectrum/s2'
11+
import {PatternTestingFAQ} from '../../src/PatternTestingFAQ';
812

913
export const tags = ['input'];
1014

@@ -120,3 +124,58 @@ import {Form} from 'vanilla-starter/Form';;
120124
### CheckboxGroup
121125

122126
<PropTable component={docs.exports.CheckboxGroup} links={docs.links} />
127+
128+
## Testing
129+
130+
### Test utils
131+
132+
`@react-aria/test-utils` offers common checkbox group interaction utilities which you may find helpful when writing tests. To install, simply
133+
add it to your dev dependencies via your preferred package manager.
134+
135+
<InstallCommand pkg="@react-aria/test-utils" flags="--dev" />
136+
137+
<InlineAlert variant="notice">
138+
<Heading>Requirements</Heading>
139+
<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>
140+
</InlineAlert>
141+
142+
Once installed, you can access the `User` that `@react-aria/test-utils` provides in your test file as shown below. This user only needs to be initialized once and then can be used to generate
143+
the `CheckboxGroup` tester in your test cases. This gives you access to `CheckboxGroup` specific utilities that you can then call within your test to query for specific subcomponents or simulate common interactions.
144+
The example test case below shows how you might go about setting up the `CheckboxGroup` tester, use it to simulate checkbox selection, and verify the checkbox group's state after each interaction.
145+
146+
```ts
147+
// CheckboxGroup.test.ts
148+
import {render} from '@testing-library/react';
149+
import {User} from '@react-aria/test-utils';
150+
151+
let testUtilUser = new User({interactionType: 'mouse', advanceTimer: jest.advanceTimersByTime});
152+
// ...
153+
154+
it('CheckboxGroup can select multiple checkboxes', async function () {
155+
// Render your test component/app and initialize the checkbox group tester
156+
let {getByTestId} = render(
157+
<CheckboxGroup data-testid="test-checkboxgroup">
158+
...
159+
</CheckboxGroup>
160+
);
161+
let checkboxGroupTester = testUtilUser.createTester('CheckboxGroup', {root: getByTestId('test-checkboxgroup')});
162+
expect(checkboxGroupTester.selectedCheckboxes).toHaveLength(0);
163+
164+
await checkboxGroupTester.toggleCheckbox({checkbox: 0});
165+
expect(checkboxGroupTester.checkboxes[0]).toBeChecked();
166+
expect(checkboxGroupTester.selectedCheckboxes).toHaveLength(1);
167+
168+
await checkboxGroupTester.toggleCheckbox({checkbox: 4});
169+
expect(checkboxGroupTester.checkboxes[4]).toBeChecked();
170+
expect(checkboxGroupTester.selectedCheckboxes).toHaveLength(2);
171+
});
172+
```
173+
174+
See below for the full definition of the `User` and the `CheckboxGroup` tester.
175+
176+
<ClassAPI links={testUtilDocs.links} class={testUtilDocs.exports.User} />
177+
<ClassAPI links={testUtilDocs.links} class={testUtilDocs.exports.CheckboxGroupTester} />
178+
179+
### Testing FAQ
180+
181+
<PatternTestingFAQ patternName="checkbox group" />

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import {ComboBox as VanillaComboBox, ComboBoxItem} from 'vanilla-starter/ComboBo
66
import vanillaDocs from 'docs:vanilla-starter/ComboBox';
77
import '../../tailwind/tailwind.css';
88
import Anatomy from 'react-aria-components/docs/ComboBoxAnatomy.svg';
9+
import testUtilDocs from 'docs:@react-aria/test-utils';
10+
import {InstallCommand} from '../../src/InstallCommand';
11+
import {InlineAlert, Heading, Content} from '@react-spectrum/s2'
12+
import {PatternTestingFAQ} from '../../src/PatternTestingFAQ';
913

1014
export const tags = ['autocomplete', 'search', 'typeahead', 'input'];
1115

@@ -353,3 +357,57 @@ import {ComboBox, ComboBoxItem} from 'vanilla-starter/ComboBox';
353357
### ComboBox
354358

355359
<PropTable component={docs.exports.ComboBox} links={docs.links} />
360+
361+
## Testing
362+
363+
### Test utils
364+
365+
`@react-aria/test-utils` offers common combobox interaction utilities which you may find helpful when writing tests. To install, simply
366+
add it to your dev dependencies via your preferred package manager.
367+
368+
<InstallCommand pkg="@react-aria/test-utils" flags="--dev" />
369+
370+
<InlineAlert variant="notice">
371+
<Heading>Requirements</Heading>
372+
<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>
373+
</InlineAlert>
374+
375+
Once installed, you can access the `User` that `@react-aria/test-utils` provides in your test file as shown below. This user only needs to be initialized once and then can be used to generate
376+
the `ComboBox` tester in your test cases. This gives you access to `ComboBox` specific utilities that you can then call within your test to query for specific subcomponents or simulate common interactions.
377+
The example test case below shows how you might go about setting up the `ComboBox` tester, use it to simulate option selection, and verify the combobox's state after each interaction.
378+
379+
```ts
380+
// Combobox.test.ts
381+
import {render} from '@testing-library/react';
382+
import {User} from '@react-aria/test-utils';
383+
384+
let testUtilUser = new User({interactionType: 'mouse'});
385+
// ...
386+
387+
it('ComboBox can select an option via keyboard', async function () {
388+
// Render your test component/app and initialize the combobox tester
389+
let {getByTestId} = render(
390+
<ComboBox data-testid="test-combobox">
391+
...
392+
</ComboBox>
393+
);
394+
let comboboxTester = testUtilUser.createTester('ComboBox', {root: getByTestId('test-combobox'), interactionType: 'keyboard'});
395+
396+
await comboboxTester.open();
397+
expect(comboboxTester.listbox).toBeInTheDocument();
398+
399+
let options = comboboxTester.options();
400+
await comboboxTester.selectOption({option: options[0]});
401+
expect(comboboxTester.combobox.value).toBe('One');
402+
expect(comboboxTester.listbox).not.toBeInTheDocument();
403+
});
404+
```
405+
406+
See below for the full definition of the `User` and the `ComboBox` tester.
407+
408+
<ClassAPI links={testUtilDocs.links} class={testUtilDocs.exports.User} />
409+
<ClassAPI links={testUtilDocs.links} class={testUtilDocs.exports.ComboBoxTester} />
410+
411+
### Testing FAQ
412+
413+
<PatternTestingFAQ patternName="combobox" />

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import {GridList as VanillaGridList, GridListItem} from 'vanilla-starter/GridLis
66
import vanillaDocs from 'docs:vanilla-starter/GridList';
77
import '../../tailwind/tailwind.css';
88
import Anatomy from 'react-aria-components/docs/GridListAnatomy.svg';
9+
import testUtilDocs from 'docs:@react-aria/test-utils';
10+
import {InstallCommand} from '../../src/InstallCommand';
11+
import {InlineAlert, Heading, Content} from '@react-spectrum/s2'
12+
import {PatternTestingFAQ} from '../../src/PatternTestingFAQ';
913

1014
export const tags = ['list view'];
1115

@@ -923,3 +927,71 @@ function Example() {
923927
### GridListLoadMoreItem
924928

925929
<PropTable component={docs.exports.GridListLoadMoreItem} links={docs.links} showDescription />
930+
931+
## Testing
932+
933+
### General setup
934+
935+
GridList features long press interactions on its items depending on the item actions provided and if the user is interacting with the gridlist on
936+
a touch device. Please see the following sections in the general testing documentation for more information on how to handle these
937+
behaviors in your test suite.
938+
939+
[Timers](testing.html#timers)
940+
941+
[Long press](testing.html#simulating-user-long-press)
942+
943+
### Test utils
944+
945+
`@react-aria/test-utils` offers common gridlist interaction utilities which you may find helpful when writing tests. To install, simply
946+
add it to your dev dependencies via your preferred package manager.
947+
948+
<InstallCommand pkg="@react-aria/test-utils" flags="--dev" />
949+
950+
<InlineAlert variant="notice">
951+
<Heading>Requirements</Heading>
952+
<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>
953+
</InlineAlert>
954+
955+
Once installed, you can access the `User` that `@react-aria/test-utils` provides in your test file as shown below. This user only needs to be initialized once and then can be used to generate
956+
the `GridList` tester in your test cases. This gives you access to `GridList` specific utilities that you can then call within your test to query for specific subcomponents or simulate common interactions.
957+
The example test case below shows how you might go about setting up the `GridList` tester, use it to simulate item selection, and verify the gridlist's state after each interaction.
958+
959+
```ts
960+
// GridList.test.ts
961+
import {render, within} from '@testing-library/react';
962+
import {User} from '@react-aria/test-utils';
963+
964+
let testUtilUser = new User({interactionType: 'mouse'});
965+
// ...
966+
967+
it('GridList can select a row via keyboard', async function () {
968+
// Render your test component/app and initialize the gridlist tester
969+
let {getByTestId} = render(
970+
<GridList data-testid="test-gridlist" selectionMode="single">
971+
...
972+
</GridList>
973+
);
974+
let gridListTester = testUtilUser.createTester('GridList', {root: getByTestId('test-gridlist'), interactionType: 'keyboard'});
975+
976+
let row = gridListTester.rows[0];
977+
expect(within(row).getByRole('checkbox')).not.toBeChecked();
978+
expect(gridListTester.selectedRows).toHaveLength(0);
979+
980+
await gridListTester.toggleRowSelection({row: 0});
981+
expect(within(row).getByRole('checkbox')).toBeChecked();
982+
expect(gridListTester.selectedRows).toHaveLength(1);
983+
984+
await gridListTester.toggleRowSelection({row: 0});
985+
expect(within(row).getByRole('checkbox')).not.toBeChecked();
986+
expect(gridListTester.selectedRows).toHaveLength(0);
987+
});
988+
```
989+
990+
See below for the full definition of the `User` and the `GridList` tester.
991+
992+
<ClassAPI links={testUtilDocs.links} class={testUtilDocs.exports.User} />
993+
<ClassAPI links={testUtilDocs.links} class={testUtilDocs.exports.GridListTester} />
994+
995+
### Testing FAQ
996+
997+
<PatternTestingFAQ patternName="gridlist" />

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import vanillaDocs from 'docs:vanilla-starter/ListBox';
77
import '../../tailwind/tailwind.css';
88
import Anatomy from 'react-aria-components/docs/ListBoxAnatomy.svg';
99
import {InlineAlert, Heading, Content} from '@react-spectrum/s2'
10+
import testUtilDocs from 'docs:@react-aria/test-utils';
11+
import {InstallCommand} from '../../src/InstallCommand';
12+
import {PatternTestingFAQ} from '../../src/PatternTestingFAQ';
1013

1114
export const tags = ['options'];
1215

@@ -415,3 +418,62 @@ function Example() {
415418
### ListBoxLoadMoreItem
416419

417420
<PropTable component={docs.exports.ListBoxLoadMoreItem} links={docs.links} showDescription />
421+
422+
## Testing
423+
424+
### General setup
425+
426+
ListBox features long press interactions on its options depending on the option actions provided and if the user is interacting with the listbox on
427+
a touch device. Please see the following sections in the general testing documentation for more information on how to handle these
428+
behaviors in your test suite.
429+
430+
[Timers](testing.html#timers)
431+
432+
[Long press](testing.html#simulating-user-long-press)
433+
434+
### Test utils
435+
436+
`@react-aria/test-utils` offers common listbox interaction utilities which you may find helpful when writing tests. To install, simply
437+
add it to your dev dependencies via your preferred package manager.
438+
439+
<InstallCommand pkg="@react-aria/test-utils" flags="--dev" />
440+
441+
<InlineAlert variant="notice">
442+
<Heading>Requirements</Heading>
443+
<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>
444+
</InlineAlert>
445+
446+
Once installed, you can access the `User` that `@react-aria/test-utils` provides in your test file as shown below. This user only needs to be initialized once and then can be used to generate
447+
the `ListBox` tester in your test cases. This gives you access to `ListBox` specific utilities that you can then call within your test to query for specific subcomponents or simulate common interactions.
448+
The example test case below shows how you might go about setting up the `ListBox` tester, use it to simulate option selection, and verify the listbox's state after each interaction.
449+
450+
```ts
451+
// ListBox.test.ts
452+
import {render} from '@testing-library/react';
453+
import {User} from '@react-aria/test-utils';
454+
455+
let testUtilUser = new User({interactionType: 'mouse'});
456+
// ...
457+
458+
it('ListBox can select an option via keyboard', async function () {
459+
// Render your test component/app and initialize the listbox tester
460+
let {getByTestId} = render(
461+
<ListBox selectionMode="single" data-testid="test-listbox">
462+
...
463+
</ListBox>
464+
);
465+
let listboxTester = testUtilUser.createTester('ListBox', {root: getByTestId('test-listbox'), interactionType: 'keyboard'});
466+
467+
await listboxTester.toggleOptionSelection({option: 4});
468+
expect(listboxTester.options()[4]).toHaveAttribute('aria-selected', 'true');
469+
});
470+
```
471+
472+
See below for the full definition of the `User` and the `ListBox` tester.
473+
474+
<ClassAPI links={testUtilDocs.links} class={testUtilDocs.exports.User} />
475+
<ClassAPI links={testUtilDocs.links} class={testUtilDocs.exports.ListBoxTester} />
476+
477+
### Testing FAQ
478+
479+
<PatternTestingFAQ patternName="listbox" />

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import docs from 'docs:react-aria-components';
55
import '../../tailwind/tailwind.css';
66
import Anatomy from 'react-aria-components/docs/MenuAnatomy.svg';
77
import {InlineAlert, Heading, Content} from '@react-spectrum/s2'
8+
import testUtilDocs from 'docs:@react-aria/test-utils';
9+
import {InstallCommand} from '../../src/InstallCommand';
10+
import {PatternTestingFAQ} from '../../src/PatternTestingFAQ';
811

912
export const tags = ['dropdown'];
1013

@@ -577,3 +580,72 @@ import {ChevronDown} from 'lucide-react';
577580
### SubmenuTrigger
578581

579582
<PropTable component={docs.exports.SubmenuTrigger} links={docs.links} showDescription />
583+
584+
## Testing
585+
586+
### General setup
587+
588+
Menu features long press interactions depending on the actions provided and if the user is interacting with the menu on
589+
a touch device. Please see the following sections in the general testing documentation for more information on how to handle these
590+
behaviors in your test suite.
591+
592+
[Timers](testing.html#timers)
593+
594+
[Long press](testing.html#simulating-user-long-press)
595+
596+
### Test utils
597+
598+
`@react-aria/test-utils` offers common menu interaction utilities which you may find helpful when writing tests. To install, simply
599+
add it to your dev dependencies via your preferred package manager.
600+
601+
<InstallCommand pkg="@react-aria/test-utils" flags="--dev" />
602+
603+
<InlineAlert variant="notice">
604+
<Heading>Requirements</Heading>
605+
<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>
606+
</InlineAlert>
607+
608+
Once installed, you can access the `User` that `@react-aria/test-utils` provides in your test file as shown below. This user only needs to be initialized once and then can be used to generate
609+
the `Menu` tester in your test cases. This gives you access to `Menu` specific utilities that you can then call within your test to query for specific subcomponents or simulate common interactions.
610+
The example test case below shows how you might go about setting up the `Menu` tester, use it to find and open a submenu, and verify the menu's state after each interaction.
611+
612+
```ts
613+
// Menu.test.ts
614+
import {render} from '@testing-library/react';
615+
import {User} from '@react-aria/test-utils';
616+
617+
let testUtilUser = new User({interactionType: 'mouse'});
618+
// ...
619+
620+
it('Menu can open its submenu via keyboard', async function () {
621+
// Render your test component/app and initialize the menu tester
622+
let {getByTestId} = render(
623+
<MenuTrigger>
624+
<Button data-testid="test-menutrigger">Menu trigger</Button>
625+
...
626+
</MenuTrigger>
627+
);
628+
let menuTester = testUtilUser.createTester('Menu', {root: getByTestId('test-menutrigger'), interactionType: 'keyboard'});
629+
630+
await menuTester.open();
631+
expect(menuTester.menu).toBeInTheDocument();
632+
let submenuTriggers = menuTester.submenuTriggers;
633+
expect(submenuTriggers).toHaveLength(1);
634+
635+
let submenuTester = await menuTester.openSubmenu({submenuTrigger: 'Share…'});
636+
expect(submenuTester.menu).toBeInTheDocument();
637+
638+
await submenuTester.selectOption({option: submenuTester.options()[0]});
639+
expect(submenuTester.menu).not.toBeInTheDocument();
640+
expect(menuTester.menu).not.toBeInTheDocument();
641+
});
642+
```
643+
644+
See below for the full definition of the `User` and the `Menu` tester.
645+
646+
<ClassAPI links={testUtilDocs.links} class={testUtilDocs.exports.User} />
647+
<ClassAPI links={testUtilDocs.links} class={testUtilDocs.exports.MenuTester} />
648+
649+
### Testing FAQ
650+
651+
<PatternTestingFAQ patternName="menu" />

0 commit comments

Comments
 (0)