-
-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(docs): started creating testing recipes
- Loading branch information
Showing
5 changed files
with
227 additions
and
3 deletions.
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
apps/docs/src/app/(main)/(markdown)/testing/recipes/page.mdx
This file contains 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,141 @@ | ||
# Recipes | ||
|
||
This page will provide common testing recipes for components through ReactMD. | ||
|
||
# Autocomplete | ||
|
||
TODO | ||
|
||
# Checkbox | ||
|
||
TODO | ||
|
||
# Dialog/Sheet | ||
|
||
TODO | ||
|
||
# Expansion Panel | ||
|
||
TODO | ||
|
||
# Menu | ||
|
||
TODO | ||
|
||
# Radio | ||
|
||
TODO | ||
|
||
# Select | ||
|
||
> All of these examples will use the [Simple Select](/components/select#simple-select) example code. | ||
## Find and Change Value | ||
|
||
This example showcases how to: | ||
|
||
- find the `Select` component | ||
- find and verify the current selected option | ||
- change the selected option | ||
|
||
```tsx | ||
import { screen, rmdRender, userEvent } from "@react-md/core/test-utils"; | ||
|
||
it("should be able to change value", async () => { | ||
const user = userEvent.setup(); | ||
rmdRender(<SimpleSelect />); | ||
|
||
// this is the clickable element that allows the listbox of options to appear | ||
const select = screen.getByRole("combobox", { name: "Label" }); | ||
// this stores the current value | ||
const selectValue = screen.getByRole("textbox", { hidden: true }); | ||
expect(selectValue).toHaveValue(""); | ||
|
||
await user.click(select); | ||
// the `name` should be the accessible text in any of the available options | ||
await user.click(screen.getByRole("option", { name: "Option 1" })); | ||
expect(selectValue).toHaveValue("a"); | ||
|
||
await user.click(select); | ||
|
||
// the `Option 1` should now be selected | ||
expect(() => | ||
screen.getByRole("option", { name: "Option 1", selected: true }) | ||
).not.toThrow(); | ||
}); | ||
``` | ||
|
||
## Verify the Display Value | ||
|
||
This example showcases how to find and verify the selected option's display | ||
value while the Select listbox is closed using the `getSelectParts` test util query. | ||
|
||
```tsx | ||
import { | ||
getSelectParts, | ||
screen, | ||
rmdRender, | ||
userEvent, | ||
} from "@react-md/core/test-utils"; | ||
|
||
it("should be able to verify the display value", async () => { | ||
const user = userEvent.setup(); | ||
rmdRender(<SimpleSelect />); | ||
|
||
const { select, selectValue, selectDisplay } = getSelectParts({ | ||
name: "Label", | ||
}); | ||
// this isn't required, but added to show what element this is | ||
expect(selectDisplay).toHaveClass("rmd-selected-option"); | ||
|
||
// there is currently no selected value | ||
expect(selectDisplay).toHaveTextContent(""); | ||
|
||
await user.click(select); | ||
await user.click(screen.getByRole("option", { name: "Option 1" })); | ||
expect(selectValue).toHaveValue("a"); | ||
expect(selectDisplay).toHaveTextContent("Option 1"); | ||
}); | ||
``` | ||
|
||
# Slider | ||
|
||
TODO | ||
|
||
# Snackbar | ||
|
||
TODO | ||
|
||
# Switch | ||
|
||
## Find and Toggle | ||
|
||
The `Switch` is an extension of an `<input type="checkbox">` with | ||
`role="switch"`, so the element can be changed just like a `Checkbox`. | ||
|
||
```tsx | ||
import { screen, rmdRender, userEvent } from "@react-md/core/test-utils"; | ||
|
||
it("should be able to change the checked state", async () => { | ||
const user = userEvent.setup(); | ||
rmdRender(<Switch label="Label" />); | ||
|
||
const switchElement = screen.getByRole("switch", { name: "Label" }); | ||
expect(switchElement).not.toBeChecked(); | ||
|
||
await user.click(switchElement); | ||
expect(switchElement).toBeChecked(); | ||
}); | ||
``` | ||
|
||
# Tabs | ||
|
||
TODO | ||
|
||
# Tooltip | ||
|
||
TODO | ||
|
||
# Tree | ||
|
||
TODO |
This file contains 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
This file contains 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
This file contains 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
This file contains 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,74 @@ | ||
import { | ||
type BoundFunctions, | ||
type ByRoleOptions, | ||
type queries, | ||
screen, | ||
within, | ||
} from "@testing-library/dom"; | ||
|
||
/** | ||
* @since 6.0.0 | ||
*/ | ||
export interface SelectParts { | ||
select: HTMLDivElement; | ||
selectValue: HTMLInputElement; | ||
selectDisplay: HTMLDivElement; | ||
} | ||
|
||
/** | ||
* @since 6.0.0 | ||
*/ | ||
export interface GetSelectPartsOptions extends ByRoleOptions { | ||
/** @defaultValue `screen` */ | ||
container?: BoundFunctions<typeof queries>; | ||
} | ||
|
||
/** | ||
* @example Simple Example | ||
* ```tsx | ||
* import { | ||
* getSelectParts, | ||
* screen, | ||
* rmdRender, | ||
* userEvent, | ||
* } from "@react-md/core/test-utils"; | ||
* | ||
* it("should be able to verify the display value", async () => { | ||
* const user = userEvent.setup(); | ||
* rmdRender(<SimpleSelect />); | ||
* | ||
* const { select, selectValue, selectDisplay } = getSelectParts({ | ||
* name: "Label", | ||
* }); | ||
* // this isn't required, but added to show what element this is | ||
* expect(selectDisplay).toHaveClass("rmd-selected-option"); | ||
* | ||
* // there is currently no selected value | ||
* expect(selectDisplay).toHaveTextContent(""); | ||
* | ||
* await user.click(select); | ||
* await user.click(screen.getByRole("option", { name: "Option 1" })); | ||
* expect(selectValue).toHaveValue("a"); | ||
* expect(selectDisplay).toHaveTextContent("Option 1"); | ||
* }); | ||
* ``` | ||
* | ||
* @since 6.0.0 | ||
*/ | ||
export function getSelectParts(options: GetSelectPartsOptions): SelectParts { | ||
const { container = screen, ...byRoleOptions } = options; | ||
const select = container.getByRole<HTMLDivElement>("combobox", byRoleOptions); | ||
const selectValue = within(select).getByRole<HTMLInputElement>("textbox", { | ||
hidden: true, | ||
}); | ||
const selectDisplay = select.firstElementChild; | ||
if (!(selectDisplay instanceof HTMLDivElement)) { | ||
throw new Error("Unable to find the `Select` display value element"); | ||
} | ||
|
||
return { | ||
select, | ||
selectValue, | ||
selectDisplay, | ||
}; | ||
} |