Skip to content

Commit 987096d

Browse files
committed
add new test util docs to old website
1 parent 143761b commit 987096d

File tree

19 files changed

+271
-17
lines changed

19 files changed

+271
-17
lines changed

packages/@react-aria/test-utils/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ export {pointerMap} from './userEventMaps';
1616
export {User} from './user';
1717
// TODO: had to export these for the docs, but not sure why I didn't have to do
1818
// so for the v3 docs?
19+
export {CheckboxGroupTester} from './checkboxgroup';
1920
export {ComboBoxTester} from './combobox';
21+
export {DialogTester} from './dialog';
2022
export {GridListTester} from './gridlist';
2123
export {ListBoxTester} from './listbox';
2224
export {MenuTester} from './menu';
25+
export {RadioGroupTester} from './radiogroup';
2326
export {SelectTester} from './select';
2427
export {TableTester} from './table';
2528
export {TabsTester} from './tabs';

packages/@react-spectrum/checkbox/docs/CheckboxGroup.mdx

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import {Layout} from '@react-spectrum/docs';
1111
export default Layout;
1212

1313
import docs from 'docs:@react-spectrum/checkbox';
14+
import checkboxgroupUtil from 'docs:@react-aria/test-utils/src/checkboxgroup.ts';
1415
import packageData from '@react-spectrum/checkbox/package.json';
15-
import {HeaderInfo, PropTable, PageDescription} from '@react-spectrum/docs';
16+
import {HeaderInfo, PropTable, PageDescription, VersionBadge, ClassAPI} from '@react-spectrum/docs';
1617

1718
```jsx import
1819
import {Checkbox, CheckboxGroup} from '@react-spectrum/checkbox';
@@ -332,3 +333,43 @@ See the [MDN docs](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/
332333
<Checkbox value="basketball">Basketball</Checkbox>
333334
</CheckboxGroup>
334335
```
336+
337+
## Testing
338+
339+
### Test utils <VersionBadge version="beta" style={{marginLeft: 4, verticalAlign: 'bottom'}} />
340+
341+
`@react-spectrum/test-utils` offers common checkbox group interaction utilities which you may find helpful when writing tests. See [here](./testing.html#react-spectrum-test-utils) for more information on how to setup these utilities
342+
in your tests. Below is the full definition of the checkbox group tester and a sample of how you could use it in your test suite.
343+
344+
```ts
345+
// CheckboxGroup.test.ts
346+
import {render, within} from '@testing-library/react';
347+
import {theme} from '@react-spectrum/theme-default';
348+
import {User} from '@react-spectrum/test-utils';
349+
350+
let testUtilUser = new User({interactionType: 'mouse', advanceTimer: jest.advanceTimersByTime});
351+
// ...
352+
353+
it('CheckboxGroup can select multiple checkboxes', async function () {
354+
// Render your test component/app and initialize the checkbox group tester
355+
let {getByTestId} = render(
356+
<Provider theme={defaultTheme}>
357+
<CheckboxGroup data-testid="test-checkboxgroup">
358+
...
359+
</CheckboxGroup>
360+
</Provider>
361+
);
362+
let checkboxGroupTester = testUtilUser.createTester('CheckboxGroup', {root: getByTestId('test-checkboxgroup')});
363+
expect(checkboxGroupTester.selectedCheckboxes).toHaveLength(0);
364+
365+
await checkboxGroupTester.toggleCheckbox({checkbox: 0});
366+
expect(checkboxGroupTester.checkboxes[0]).toBeChecked();
367+
expect(checkboxGroupTester.selectedCheckboxes).toHaveLength(1);
368+
369+
await checkboxGroupTester.toggleCheckbox({checkbox: 4});
370+
expect(checkboxGroupTester.checkboxes[4]).toBeChecked();
371+
expect(checkboxGroupTester.selectedCheckboxes).toHaveLength(2);
372+
});
373+
```
374+
375+
<ClassAPI links={checkboxgroupUtil.links} class={checkboxgroupUtil.exports.CheckboxGroupTester} />

packages/@react-spectrum/combobox/docs/ComboBox.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ isn't sufficient when resolving issues in your own test cases.
996996

997997
### Test utils <VersionBadge version="beta" style={{marginLeft: 4, verticalAlign: 'bottom'}} />
998998

999-
`@react-spectrum/test-utils` offers common combobox interaction utilities which you may find helpful when writing tests. See [here](../react-aria/testing.html#react-aria-test-utils) for more information on how to setup these utilities
999+
`@react-spectrum/test-utils` offers common combobox interaction utilities which you may find helpful when writing tests. See [here](./testing.html#react-spectrum-test-utils) for more information on how to setup these utilities
10001000
in your tests. Below is the full definition of the combobox tester and a sample of how you could use it in your test suite.
10011001

10021002
```ts

packages/@react-spectrum/dialog/docs/Dialog.mdx

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export default Layout;
1212

1313
import DialogAnatomy from './images/DialogAnatomy.svg';
1414
import docs from 'docs:@react-spectrum/dialog';
15-
import {Image, HeaderInfo, PropTable, PageDescription} from '@react-spectrum/docs';
15+
import dialogUtil from 'docs:@react-aria/test-utils/src/dialog.ts';
16+
import {Image, HeaderInfo, PropTable, PageDescription, VersionBadge, ClassAPI} from '@react-spectrum/docs';
1617
import packageData from '@react-spectrum/dialog/package.json';
1718
import styles from '@react-spectrum/docs/src/docs.css';
1819

@@ -398,3 +399,43 @@ respectively for container sizing considerations. Modal sizes on mobile devices
398399
)}
399400
</DialogTrigger>
400401
```
402+
403+
## Testing
404+
405+
### Test utils <VersionBadge version="beta" style={{marginLeft: 4, verticalAlign: 'bottom'}} />
406+
407+
`@react-spectrum/test-utils` offers common dialog interaction utilities which you may find helpful when writing tests. See [here](./testing.html#react-spectrum-test-utils) for more information on how to setup these utilities
408+
in your tests. Below is the full definition of the dialog tester and a sample of how you could use it in your test suite.
409+
410+
```ts
411+
// Dialog.test.ts
412+
import {render, within} from '@testing-library/react';
413+
import {theme} from '@react-spectrum/theme-default';
414+
import {User} from '@react-spectrum/test-utils';
415+
416+
let testUtilUser = new User({interactionType: 'mouse', advanceTimer: jest.advanceTimersByTime});
417+
// ...
418+
419+
it('Dialog can be opened and closed', async function () {
420+
// Render your test component/app and initialize the dialog tester
421+
let {getByTestId, getByRole} = render(
422+
<Provider theme={defaultTheme}>
423+
<DialogTrigger>
424+
<ActionButton>Trigger</ActionButton>
425+
<Dialog>
426+
...
427+
</Dialog>
428+
</DialogTrigger>
429+
</Provider>
430+
);
431+
let button = getByRole('button');
432+
let dialogTester = testUtilUser.createTester('Dialog', {root: button, overlayType: 'modal'});
433+
await dialogTester.open();
434+
let dialog = dialogTester.dialog;
435+
expect(dialog).toBeVisible();
436+
await dialogTester.close();
437+
expect(dialog).not.toBeInTheDocument();
438+
});
439+
```
440+
441+
<ClassAPI links={dialogUtil.links} class={dialogUtil.exports.DialogTester} />

packages/@react-spectrum/list/docs/ListView.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ isn't sufficient when resolving issues in your own test cases.
11951195

11961196
### Test utils <VersionBadge version="beta" style={{marginLeft: 4, verticalAlign: 'bottom'}} />
11971197

1198-
`@react-spectrum/test-utils` offers common gridlist interaction utilities which you may find helpful when writing tests. See [here](../react-aria/testing.html#react-aria-test-utils) for more information on how to setup these utilities
1198+
`@react-spectrum/test-utils` offers common gridlist interaction utilities which you may find helpful when writing tests. See [here](./testing.html#react-spectrum-test-utils) for more information on how to setup these utilities
11991199
in your tests. Below is the full definition of the gridlist tester and a sample of how you could use it in your test suite.
12001200

12011201
```ts

packages/@react-spectrum/listbox/docs/ListBox.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ isn't sufficient when resolving issues in your own test cases.
413413

414414
### Test utils <VersionBadge version="beta" style={{marginLeft: 4, verticalAlign: 'bottom'}} />
415415

416-
`@react-spectrum/test-utils` offers common listbox interaction utilities which you may find helpful when writing tests. See [here](../react-aria/testing.html#react-aria-test-utils) for more information on how to setup these utilities
416+
`@react-spectrum/test-utils` offers common listbox interaction utilities which you may find helpful when writing tests. See [here](./testing.html#react-spectrum-test-utils) for more information on how to setup these utilities
417417
in your tests. Below is the full definition of the listbox tester and a sample of how you could use it in your test suite.
418418

419419
```ts

packages/@react-spectrum/menu/docs/MenuTrigger.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ isn't sufficient when resolving issues in your own test cases.
260260

261261
### Test utils <VersionBadge version="beta" style={{marginLeft: 4, verticalAlign: 'bottom'}} />
262262

263-
`@react-spectrum/test-utils` offers common menu interaction utilities which you may find helpful when writing tests. See [here](../react-aria/testing.html#react-aria-test-utils) for more information on how to setup these utilities
263+
`@react-spectrum/test-utils` offers common menu interaction utilities which you may find helpful when writing tests. See [here](./testing.html#react-spectrum-test-utils) for more information on how to setup these utilities
264264
in your tests. Below is the full definition of the menu tester and a sample of how you could use it in your test suite.
265265

266266
```ts

packages/@react-spectrum/picker/docs/Picker.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ isn't sufficient when resolving issues in your own test cases.
592592

593593
### Test utils <VersionBadge version="beta" style={{marginLeft: 4, verticalAlign: 'bottom'}} />
594594

595-
`@react-spectrum/test-utils` offers common select interaction utilities which you may find helpful when writing tests. See [here](../react-aria/testing.html#react-aria-test-utils) for more information on how to setup these utilities
595+
`@react-spectrum/test-utils` offers common select interaction utilities which you may find helpful when writing tests. See [here](./testing.html#react-spectrum-test-utils) for more information on how to setup these utilities
596596
in your tests. Below is the full definition of the select tester and a sample of how you could use it in your test suite.
597597

598598
```ts

packages/@react-spectrum/radio/docs/RadioGroup.mdx

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import {Layout} from '@react-spectrum/docs';
1111
export default Layout;
1212

1313
import docs from 'docs:@react-spectrum/radio';
14+
import radiogroupUtil from 'docs:@react-aria/test-utils/src/radiogroup.ts';
1415
import packageData from '@react-spectrum/radio/package.json';
15-
import {HeaderInfo, PropTable, PageDescription} from '@react-spectrum/docs';
16+
import {HeaderInfo, PropTable, PageDescription, VersionBadge, ClassAPI} from '@react-spectrum/docs';
1617

1718
```jsx import
1819
import {Radio, RadioGroup} from '@react-spectrum/radio';
@@ -306,3 +307,43 @@ See the [MDN docs](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/
306307
<Radio value="dragon">Dragon</Radio>
307308
</RadioGroup>
308309
```
310+
311+
## Testing
312+
313+
### Test utils <VersionBadge version="beta" style={{marginLeft: 4, verticalAlign: 'bottom'}} />
314+
315+
`@react-spectrum/test-utils` offers common radio group interaction utilities which you may find helpful when writing tests. See [here](./testing.html#react-spectrum-test-utils) for more information on how to setup these utilities
316+
in your tests. Below is the full definition of the radio group tester and a sample of how you could use it in your test suite.
317+
318+
```ts
319+
// RadioGroup.test.ts
320+
import {render, within} from '@testing-library/react';
321+
import {theme} from '@react-spectrum/theme-default';
322+
import {User} from '@react-spectrum/test-utils';
323+
324+
let testUtilUser = new User({interactionType: 'mouse', advanceTimer: jest.advanceTimersByTime});
325+
// ...
326+
327+
it('RadioGroup can switch the selected radio', async function () {
328+
// Render your test component/app and initialize the radiogroup tester
329+
let {getByRole} = render(
330+
<Provider theme={defaultTheme}>
331+
<RadioGroup>
332+
...
333+
</RadioGroup>
334+
</Provider>
335+
);
336+
337+
let radioGroupTester = testUtilUser.createTester('RadioGroup', {root: getByRole('radiogroup')});
338+
let radios = radioGroupTester.radios;
339+
expect(radioGroupTester.selectedRadio).toBeFalsy();
340+
341+
await radioGroupTester.triggerRadio({radio: radios[0]});
342+
expect(radioGroupTester.selectedRadio).toBe(radios[0]);
343+
344+
await radioGroupTester.triggerRadio({radio: radios[1]});
345+
expect(radioGroupTester.selectedRadio).toBe(radios[1]);
346+
});
347+
```
348+
349+
<ClassAPI links={radiogroupUtil.links} class={radiogroupUtil.exports.RadioGroupTester} />

packages/@react-spectrum/table/docs/TableView.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1964,7 +1964,7 @@ isn't sufficient when resolving issues in your own test cases.
19641964

19651965
### Test utils <VersionBadge version="beta" style={{marginLeft: 4, verticalAlign: 'bottom'}} />
19661966

1967-
`@react-spectrum/test-utils` offers common table interaction utilities which you may find helpful when writing tests. See [here](../react-aria/testing.html#react-aria-test-utils) for more information on how to setup these utilities
1967+
`@react-spectrum/test-utils` offers common table interaction utilities which you may find helpful when writing tests. See [here](./testing.html#react-spectrum-test-utils) for more information on how to setup these utilities
19681968
in your tests. Below is the full definition of the table tester and a sample of how you could use it in your test suite.
19691969

19701970
```ts

0 commit comments

Comments
 (0)