|
| 1 | +import { fireEvent, screen, waitFor } from "@testing-library/react" |
| 2 | +import React from "react" |
| 3 | +import * as API from "../../../api/api" |
| 4 | +import { GlobalSnackbar } from "../../../components/GlobalSnackbar/GlobalSnackbar" |
| 5 | +import * as SecurityForm from "../../../components/SettingsSecurityForm/SettingsSecurityForm" |
| 6 | +import { renderWithAuth } from "../../../testHelpers/renderHelpers" |
| 7 | +import * as AuthXService from "../../../xServices/auth/authXService" |
| 8 | +import { Language, SecurityPage } from "./SecurityPage" |
| 9 | + |
| 10 | +const renderPage = () => { |
| 11 | + return renderWithAuth( |
| 12 | + <> |
| 13 | + <SecurityPage /> |
| 14 | + <GlobalSnackbar /> |
| 15 | + </>, |
| 16 | + ) |
| 17 | +} |
| 18 | + |
| 19 | +const newData = { |
| 20 | + old_password: "password1", |
| 21 | + password: "password2", |
| 22 | + confirm_password: "password2", |
| 23 | +} |
| 24 | + |
| 25 | +const fillAndSubmitForm = async () => { |
| 26 | + await waitFor(() => screen.findByLabelText("Old Password")) |
| 27 | + fireEvent.change(screen.getByLabelText("Old Password"), { target: { value: newData.old_password } }) |
| 28 | + fireEvent.change(screen.getByLabelText("New Password"), { target: { value: newData.password } }) |
| 29 | + fireEvent.change(screen.getByLabelText("Confirm Password"), { target: { value: newData.confirm_password } }) |
| 30 | + fireEvent.click(screen.getByText(SecurityForm.Language.updatePassword)) |
| 31 | +} |
| 32 | + |
| 33 | +describe("SecurityPage", () => { |
| 34 | + describe("when it is a success", () => { |
| 35 | + it("shows the success message", async () => { |
| 36 | + jest.spyOn(API, "updateUserPassword").mockImplementationOnce((_userId, _data) => Promise.resolve(undefined)) |
| 37 | + const { user } = renderPage() |
| 38 | + await fillAndSubmitForm() |
| 39 | + |
| 40 | + const successMessage = await screen.findByText(AuthXService.Language.successSecurityUpdate) |
| 41 | + expect(successMessage).toBeDefined() |
| 42 | + expect(API.updateUserPassword).toBeCalledTimes(1) |
| 43 | + expect(API.updateUserPassword).toBeCalledWith(user.id, newData) |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + describe("when the old_password is incorrect", () => { |
| 48 | + it("shows an error", async () => { |
| 49 | + jest.spyOn(API, "updateUserPassword").mockRejectedValueOnce({ |
| 50 | + isAxiosError: true, |
| 51 | + response: { |
| 52 | + data: { message: "Incorrect password.", errors: [{ detail: "Incorrect password.", field: "old_password" }] }, |
| 53 | + }, |
| 54 | + }) |
| 55 | + |
| 56 | + const { user } = renderPage() |
| 57 | + await fillAndSubmitForm() |
| 58 | + |
| 59 | + const errorMessage = await screen.findByText("Incorrect password.") |
| 60 | + expect(errorMessage).toBeDefined() |
| 61 | + expect(API.updateUserPassword).toBeCalledTimes(1) |
| 62 | + expect(API.updateUserPassword).toBeCalledWith(user.id, newData) |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + describe("when the password is invalid", () => { |
| 67 | + it("shows an error", async () => { |
| 68 | + jest.spyOn(API, "updateUserPassword").mockRejectedValueOnce({ |
| 69 | + isAxiosError: true, |
| 70 | + response: { |
| 71 | + data: { message: "Invalid password.", errors: [{ detail: "Invalid password.", field: "password" }] }, |
| 72 | + }, |
| 73 | + }) |
| 74 | + |
| 75 | + const { user } = renderPage() |
| 76 | + await fillAndSubmitForm() |
| 77 | + |
| 78 | + const errorMessage = await screen.findByText("Invalid password.") |
| 79 | + expect(errorMessage).toBeDefined() |
| 80 | + expect(API.updateUserPassword).toBeCalledTimes(1) |
| 81 | + expect(API.updateUserPassword).toBeCalledWith(user.id, newData) |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + describe("when it is an unknown error", () => { |
| 86 | + it("shows a generic error message", async () => { |
| 87 | + jest.spyOn(API, "updateUserPassword").mockRejectedValueOnce({ |
| 88 | + data: "unknown error", |
| 89 | + }) |
| 90 | + |
| 91 | + const { user } = renderPage() |
| 92 | + await fillAndSubmitForm() |
| 93 | + |
| 94 | + const errorMessage = await screen.findByText(Language.unknownError) |
| 95 | + expect(errorMessage).toBeDefined() |
| 96 | + expect(API.updateUserPassword).toBeCalledTimes(1) |
| 97 | + expect(API.updateUserPassword).toBeCalledWith(user.id, newData) |
| 98 | + }) |
| 99 | + }) |
| 100 | +}) |
0 commit comments