|
1 | 1 | import { fireEvent, screen, waitFor } from "@testing-library/react"
|
2 | 2 | import * as API from "../../../api/api"
|
3 | 3 | import * as SecurityForm from "../../../components/SettingsSecurityForm/SettingsSecurityForm"
|
4 |
| -import { renderWithAuth } from "../../../testHelpers/renderHelpers" |
| 4 | +import { |
| 5 | + renderWithAuth, |
| 6 | + waitForLoaderToBeRemoved, |
| 7 | +} from "../../../testHelpers/renderHelpers" |
5 | 8 | import { SecurityPage } from "./SecurityPage"
|
6 | 9 | import i18next from "i18next"
|
7 |
| -import { mockApiError } from "testHelpers/entities" |
| 10 | +import { |
| 11 | + MockAuthMethodsWithPasswordType, |
| 12 | + mockApiError, |
| 13 | +} from "testHelpers/entities" |
8 | 14 |
|
9 | 15 | const { t } = i18next
|
10 | 16 |
|
11 |
| -const renderPage = () => { |
12 |
| - return renderWithAuth(<SecurityPage />) |
| 17 | +const renderPage = async () => { |
| 18 | + const utils = renderWithAuth(<SecurityPage />) |
| 19 | + await waitForLoaderToBeRemoved() |
| 20 | + return utils |
13 | 21 | }
|
14 | 22 |
|
15 |
| -const newData = { |
| 23 | +const newSecurityFormValues = { |
16 | 24 | old_password: "password1",
|
17 | 25 | password: "password2",
|
18 | 26 | confirm_password: "password2",
|
19 | 27 | }
|
20 | 28 |
|
21 |
| -const fillAndSubmitForm = async () => { |
22 |
| - await waitFor(() => screen.findByLabelText("Old Password")) |
| 29 | +const fillAndSubmitSecurityForm = () => { |
23 | 30 | fireEvent.change(screen.getByLabelText("Old Password"), {
|
24 |
| - target: { value: newData.old_password }, |
| 31 | + target: { value: newSecurityFormValues.old_password }, |
25 | 32 | })
|
26 | 33 | fireEvent.change(screen.getByLabelText("New Password"), {
|
27 |
| - target: { value: newData.password }, |
| 34 | + target: { value: newSecurityFormValues.password }, |
28 | 35 | })
|
29 | 36 | fireEvent.change(screen.getByLabelText("Confirm Password"), {
|
30 |
| - target: { value: newData.confirm_password }, |
| 37 | + target: { value: newSecurityFormValues.confirm_password }, |
31 | 38 | })
|
32 | 39 | fireEvent.click(screen.getByText(SecurityForm.Language.updatePassword))
|
33 | 40 | }
|
34 | 41 |
|
35 |
| -describe("SecurityPage", () => { |
36 |
| - describe("when it is a success", () => { |
37 |
| - it("shows the success message", async () => { |
38 |
| - jest |
39 |
| - .spyOn(API, "updateUserPassword") |
40 |
| - .mockImplementationOnce((_userId, _data) => Promise.resolve(undefined)) |
41 |
| - const { user } = renderPage() |
42 |
| - await fillAndSubmitForm() |
43 |
| - |
44 |
| - const expectedMessage = t("securityUpdateSuccessMessage", { |
45 |
| - ns: "userSettingsPage", |
46 |
| - }) |
47 |
| - const successMessage = await screen.findByText(expectedMessage) |
48 |
| - expect(successMessage).toBeDefined() |
49 |
| - expect(API.updateUserPassword).toBeCalledTimes(1) |
50 |
| - expect(API.updateUserPassword).toBeCalledWith(user.id, newData) |
51 |
| - |
52 |
| - await waitFor(() => expect(window.location).toBeAt("/")) |
53 |
| - }) |
54 |
| - }) |
| 42 | +beforeEach(() => { |
| 43 | + jest |
| 44 | + .spyOn(API, "getAuthMethods") |
| 45 | + .mockResolvedValue(MockAuthMethodsWithPasswordType) |
| 46 | +}) |
| 47 | + |
| 48 | +test("update password successfully", async () => { |
| 49 | + jest |
| 50 | + .spyOn(API, "updateUserPassword") |
| 51 | + .mockImplementationOnce((_userId, _data) => Promise.resolve(undefined)) |
| 52 | + const { user } = await renderPage() |
| 53 | + fillAndSubmitSecurityForm() |
55 | 54 |
|
56 |
| - describe("when the old_password is incorrect", () => { |
57 |
| - it("shows an error", async () => { |
58 |
| - jest.spyOn(API, "updateUserPassword").mockRejectedValueOnce( |
59 |
| - mockApiError({ |
60 |
| - message: "Incorrect password.", |
61 |
| - validations: [ |
62 |
| - { detail: "Incorrect password.", field: "old_password" }, |
63 |
| - ], |
64 |
| - }), |
65 |
| - ) |
66 |
| - |
67 |
| - const { user } = renderPage() |
68 |
| - await fillAndSubmitForm() |
69 |
| - |
70 |
| - const errorMessage = await screen.findAllByText("Incorrect password.") |
71 |
| - expect(errorMessage).toBeDefined() |
72 |
| - expect(errorMessage).toHaveLength(2) |
73 |
| - expect(API.updateUserPassword).toBeCalledTimes(1) |
74 |
| - expect(API.updateUserPassword).toBeCalledWith(user.id, newData) |
75 |
| - }) |
| 55 | + const expectedMessage = t("securityUpdateSuccessMessage", { |
| 56 | + ns: "userSettingsPage", |
76 | 57 | })
|
| 58 | + const successMessage = await screen.findByText(expectedMessage) |
| 59 | + expect(successMessage).toBeDefined() |
| 60 | + expect(API.updateUserPassword).toBeCalledTimes(1) |
| 61 | + expect(API.updateUserPassword).toBeCalledWith(user.id, newSecurityFormValues) |
| 62 | + |
| 63 | + await waitFor(() => expect(window.location).toBeAt("/")) |
| 64 | +}) |
77 | 65 |
|
78 |
| - describe("when the password is invalid", () => { |
79 |
| - it("shows an error", async () => { |
80 |
| - jest.spyOn(API, "updateUserPassword").mockRejectedValueOnce( |
81 |
| - mockApiError({ |
82 |
| - message: "Invalid password.", |
83 |
| - validations: [{ detail: "Invalid password.", field: "password" }], |
84 |
| - }), |
85 |
| - ) |
86 |
| - |
87 |
| - const { user } = renderPage() |
88 |
| - await fillAndSubmitForm() |
89 |
| - |
90 |
| - const errorMessage = await screen.findAllByText("Invalid password.") |
91 |
| - expect(errorMessage).toBeDefined() |
92 |
| - expect(errorMessage).toHaveLength(2) |
93 |
| - expect(API.updateUserPassword).toBeCalledTimes(1) |
94 |
| - expect(API.updateUserPassword).toBeCalledWith(user.id, newData) |
95 |
| - }) |
| 66 | +test("update password with incorrect old password", async () => { |
| 67 | + jest.spyOn(API, "updateUserPassword").mockRejectedValueOnce( |
| 68 | + mockApiError({ |
| 69 | + message: "Incorrect password.", |
| 70 | + validations: [{ detail: "Incorrect password.", field: "old_password" }], |
| 71 | + }), |
| 72 | + ) |
| 73 | + |
| 74 | + const { user } = await renderPage() |
| 75 | + fillAndSubmitSecurityForm() |
| 76 | + |
| 77 | + const errorMessage = await screen.findAllByText("Incorrect password.") |
| 78 | + expect(errorMessage).toBeDefined() |
| 79 | + expect(errorMessage).toHaveLength(2) |
| 80 | + expect(API.updateUserPassword).toBeCalledTimes(1) |
| 81 | + expect(API.updateUserPassword).toBeCalledWith(user.id, newSecurityFormValues) |
| 82 | +}) |
| 83 | + |
| 84 | +test("update password with invalid password", async () => { |
| 85 | + jest.spyOn(API, "updateUserPassword").mockRejectedValueOnce( |
| 86 | + mockApiError({ |
| 87 | + message: "Invalid password.", |
| 88 | + validations: [{ detail: "Invalid password.", field: "password" }], |
| 89 | + }), |
| 90 | + ) |
| 91 | + |
| 92 | + const { user } = await renderPage() |
| 93 | + fillAndSubmitSecurityForm() |
| 94 | + |
| 95 | + const errorMessage = await screen.findAllByText("Invalid password.") |
| 96 | + expect(errorMessage).toBeDefined() |
| 97 | + expect(errorMessage).toHaveLength(2) |
| 98 | + expect(API.updateUserPassword).toBeCalledTimes(1) |
| 99 | + expect(API.updateUserPassword).toBeCalledWith(user.id, newSecurityFormValues) |
| 100 | +}) |
| 101 | + |
| 102 | +test("update password when submit returns an unknown error", async () => { |
| 103 | + jest.spyOn(API, "updateUserPassword").mockRejectedValueOnce({ |
| 104 | + data: "unknown error", |
96 | 105 | })
|
97 | 106 |
|
98 |
| - describe("when it is an unknown error", () => { |
99 |
| - it("shows a generic error message", async () => { |
100 |
| - jest.spyOn(API, "updateUserPassword").mockRejectedValueOnce({ |
101 |
| - data: "unknown error", |
102 |
| - }) |
103 |
| - |
104 |
| - const { user } = renderPage() |
105 |
| - await fillAndSubmitForm() |
106 |
| - |
107 |
| - const errorText = t("warningsAndErrors.somethingWentWrong", { |
108 |
| - ns: "common", |
109 |
| - }) |
110 |
| - const errorMessage = await screen.findByText(errorText) |
111 |
| - expect(errorMessage).toBeDefined() |
112 |
| - expect(API.updateUserPassword).toBeCalledTimes(1) |
113 |
| - expect(API.updateUserPassword).toBeCalledWith(user.id, newData) |
114 |
| - }) |
| 107 | + const { user } = await renderPage() |
| 108 | + fillAndSubmitSecurityForm() |
| 109 | + |
| 110 | + const errorText = t("warningsAndErrors.somethingWentWrong", { |
| 111 | + ns: "common", |
115 | 112 | })
|
| 113 | + const errorMessage = await screen.findByText(errorText) |
| 114 | + expect(errorMessage).toBeDefined() |
| 115 | + expect(API.updateUserPassword).toBeCalledTimes(1) |
| 116 | + expect(API.updateUserPassword).toBeCalledWith(user.id, newSecurityFormValues) |
116 | 117 | })
|
0 commit comments