|
| 1 | +import type { Meta, StoryObj } from "@storybook/react"; |
| 2 | +import { expect, spyOn, userEvent, within } from "@storybook/test"; |
| 3 | +import { API } from "api/api"; |
| 4 | +import { mockApiError } from "testHelpers/entities"; |
| 5 | +import { withGlobalSnackbar } from "testHelpers/storybook"; |
| 6 | +import ChangePasswordPage from "./ChangePasswordPage"; |
| 7 | + |
| 8 | +const meta: Meta<typeof ChangePasswordPage> = { |
| 9 | + title: "pages/ResetPasswordPage/ChangePasswordPage", |
| 10 | + component: ChangePasswordPage, |
| 11 | + args: { redirect: false }, |
| 12 | + decorators: [withGlobalSnackbar], |
| 13 | +}; |
| 14 | + |
| 15 | +export default meta; |
| 16 | +type Story = StoryObj<typeof ChangePasswordPage>; |
| 17 | + |
| 18 | +export const Default: Story = {}; |
| 19 | + |
| 20 | +export const Success: Story = { |
| 21 | + play: async ({ canvasElement }) => { |
| 22 | + spyOn(API, "changePasswordWithOTP").mockResolvedValueOnce(); |
| 23 | + const canvas = within(canvasElement); |
| 24 | + const user = userEvent.setup(); |
| 25 | + const newPasswordInput = await canvas.findByLabelText("Password *"); |
| 26 | + await user.type(newPasswordInput, "password"); |
| 27 | + const confirmPasswordInput = |
| 28 | + await canvas.findByLabelText("Confirm password *"); |
| 29 | + await user.type(confirmPasswordInput, "password"); |
| 30 | + await user.click(canvas.getByRole("button", { name: /reset password/i })); |
| 31 | + await canvas.findByText("Password reset successfully"); |
| 32 | + }, |
| 33 | +}; |
| 34 | + |
| 35 | +export const WrongConfirmationPassword: Story = { |
| 36 | + play: async ({ canvasElement }) => { |
| 37 | + spyOn(API, "changePasswordWithOTP").mockRejectedValueOnce( |
| 38 | + mockApiError({ |
| 39 | + message: "New password should be different from the old password", |
| 40 | + }), |
| 41 | + ); |
| 42 | + const canvas = within(canvasElement); |
| 43 | + const user = userEvent.setup(); |
| 44 | + const newPasswordInput = await canvas.findByLabelText("Password *"); |
| 45 | + await user.type(newPasswordInput, "password"); |
| 46 | + const confirmPasswordInput = |
| 47 | + await canvas.findByLabelText("Confirm password *"); |
| 48 | + await user.type(confirmPasswordInput, "different-password"); |
| 49 | + await user.click(canvas.getByRole("button", { name: /reset password/i })); |
| 50 | + await canvas.findByText("Passwords must match"); |
| 51 | + }, |
| 52 | +}; |
| 53 | + |
| 54 | +export const ServerError: Story = { |
| 55 | + play: async ({ canvasElement }) => { |
| 56 | + const serverError = |
| 57 | + "New password should be different from the old password"; |
| 58 | + spyOn(API, "changePasswordWithOTP").mockRejectedValueOnce( |
| 59 | + mockApiError({ |
| 60 | + message: serverError, |
| 61 | + }), |
| 62 | + ); |
| 63 | + const canvas = within(canvasElement); |
| 64 | + const user = userEvent.setup(); |
| 65 | + const newPasswordInput = await canvas.findByLabelText("Password *"); |
| 66 | + await user.type(newPasswordInput, "password"); |
| 67 | + const confirmPasswordInput = |
| 68 | + await canvas.findByLabelText("Confirm password *"); |
| 69 | + await user.type(confirmPasswordInput, "password"); |
| 70 | + await user.click(canvas.getByRole("button", { name: /reset password/i })); |
| 71 | + await canvas.findByText(serverError); |
| 72 | + }, |
| 73 | +}; |
0 commit comments