Skip to content

Commit

Permalink
fix(odyssey-react-mui): fixing tests to use getByRole and canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
AnirudhMani-okta committed Sep 27, 2023
1 parent aa85df0 commit cd7feed
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/odyssey-react-mui/src/PasswordField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ const PasswordField = forwardRef<HTMLInputElement, PasswordFieldProps>(
)
}
id={id}
inputProps={{ role: "textbox" }}
name={nameOverride ?? id}
onChange={onChange}
onFocus={onFocus}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import { Meta, StoryObj } from "@storybook/react";
import { screen } from "@storybook/testing-library";
import { within } from "@storybook/testing-library";
import {
PasswordField,
PasswordFieldProps,
Expand Down Expand Up @@ -86,29 +86,30 @@ const storybookMeta: Meta<PasswordFieldProps> = {
export default storybookMeta;

export const Default: StoryObj<PasswordFieldProps> = {
play: async ({ args, canvasElement, step }) => {
play: async ({ canvasElement, step }) => {
await step("toggle password", async () => {
const fieldElement = canvasElement.querySelector(
`#${args.id}`
) as HTMLInputElement;
expect(fieldElement.type).toBe("password");
const canvas = within(canvasElement);
const fieldElement = canvas.getByRole("textbox", {
name: "Password",
});
expect(fieldElement).toHaveAttribute("type", "password");

const buttonElement = screen.getByRole("button", {
const buttonElement = canvas.getByRole("button", {
name: odysseyTranslate("passwordfield.icon.label.show"),
});
if (buttonElement) {
userEvent.type(fieldElement, "qwerty");
userEvent.click(buttonElement);
userEvent.tab();
await waitFor(() => {
expect(fieldElement.type).toBe("text");
expect(fieldElement).toHaveAttribute("type", "text");
expect(buttonElement.ariaLabel).toBe(
odysseyTranslate("passwordfield.icon.label.hide")
);
});
userEvent.click(buttonElement);
await waitFor(() => {
expect(fieldElement.type).toBe("password");
expect(fieldElement).toHaveAttribute("type", "password");
expect(buttonElement.ariaLabel).toBe(
odysseyTranslate("passwordfield.icon.label.show")
);
Expand All @@ -125,14 +126,15 @@ export const NoShowPassword: StoryObj<PasswordFieldProps> = {
args: {
hasShowPassword: false,
},
play: async ({ args, canvasElement, step }) => {
play: async ({ canvasElement, step }) => {
await step("toggle password", async () => {
const fieldElement = canvasElement.querySelector(
`#${args.id}`
) as HTMLInputElement;
expect(fieldElement.type).toBe("password");
const canvas = within(canvasElement);
const fieldElement = canvas.getByRole("textbox", {
name: "Password",
});
expect(fieldElement).toHaveAttribute("type", "password");

const buttonElement = screen.queryByRole("button", {
const buttonElement = canvas.queryByRole("button", {
name: odysseyTranslate("passwordfield.icon.label.show"),
});
expect(buttonElement).toBe(null);
Expand Down

0 comments on commit cd7feed

Please sign in to comment.