Skip to content

Commit 2d36d44

Browse files
committed
Fic previous tests
1 parent 5467119 commit 2d36d44

File tree

3 files changed

+103
-96
lines changed

3 files changed

+103
-96
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,117 @@
11
import { fireEvent, screen, waitFor } from "@testing-library/react"
22
import * as API from "../../../api/api"
33
import * as SecurityForm from "../../../components/SettingsSecurityForm/SettingsSecurityForm"
4-
import { renderWithAuth } from "../../../testHelpers/renderHelpers"
4+
import {
5+
renderWithAuth,
6+
waitForLoaderToBeRemoved,
7+
} from "../../../testHelpers/renderHelpers"
58
import { SecurityPage } from "./SecurityPage"
69
import i18next from "i18next"
7-
import { mockApiError } from "testHelpers/entities"
10+
import {
11+
MockAuthMethodsWithPasswordType,
12+
mockApiError,
13+
} from "testHelpers/entities"
814

915
const { t } = i18next
1016

11-
const renderPage = () => {
12-
return renderWithAuth(<SecurityPage />)
17+
const renderPage = async () => {
18+
const utils = renderWithAuth(<SecurityPage />)
19+
await waitForLoaderToBeRemoved()
20+
return utils
1321
}
1422

15-
const newData = {
23+
const newSecurityFormValues = {
1624
old_password: "password1",
1725
password: "password2",
1826
confirm_password: "password2",
1927
}
2028

21-
const fillAndSubmitForm = async () => {
22-
await waitFor(() => screen.findByLabelText("Old Password"))
29+
const fillAndSubmitSecurityForm = () => {
2330
fireEvent.change(screen.getByLabelText("Old Password"), {
24-
target: { value: newData.old_password },
31+
target: { value: newSecurityFormValues.old_password },
2532
})
2633
fireEvent.change(screen.getByLabelText("New Password"), {
27-
target: { value: newData.password },
34+
target: { value: newSecurityFormValues.password },
2835
})
2936
fireEvent.change(screen.getByLabelText("Confirm Password"), {
30-
target: { value: newData.confirm_password },
37+
target: { value: newSecurityFormValues.confirm_password },
3138
})
3239
fireEvent.click(screen.getByText(SecurityForm.Language.updatePassword))
3340
}
3441

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()
5554

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",
7657
})
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+
})
7765

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",
96105
})
97106

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",
115112
})
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)
116117
})

site/src/pages/UserSettingsPage/SecurityPage/SecurityPageView.stories.tsx

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import type { Meta, StoryObj } from "@storybook/react"
22
import { SecurityPageView } from "./SecurityPage"
33
import { action } from "@storybook/addon-actions"
4-
import { MockAuthMethods } from "testHelpers/entities"
4+
import {
5+
MockAuthMethods,
6+
MockAuthMethodsWithPasswordType,
7+
} from "testHelpers/entities"
58
import { ComponentProps } from "react"
69
import set from "lodash/fp/set"
7-
import { AuthMethods } from "api/typesGenerated"
810

911
const defaultArgs: ComponentProps<typeof SecurityPageView> = {
1012
security: {
@@ -46,23 +48,20 @@ export const NoOIDCAvailable: Story = {
4648
},
4749
}
4850

49-
const authMethodsWithPassword: AuthMethods = {
50-
...MockAuthMethods,
51-
me_login_type: "password",
52-
github: { enabled: true },
53-
oidc: { enabled: true, signInText: "", iconUrl: "" },
54-
}
55-
5651
export const UserLoginTypeIsPassword: Story = {
57-
args: set("oidc.section.authMethods", authMethodsWithPassword, defaultArgs),
52+
args: set(
53+
"oidc.section.authMethods",
54+
MockAuthMethodsWithPasswordType,
55+
defaultArgs,
56+
),
5857
}
5958

6059
export const ConfirmingOIDCConversion: Story = {
6160
args: set(
6261
"oidc.section",
6362
{
6463
...defaultArgs.oidc?.section,
65-
authMethods: authMethodsWithPassword,
64+
authMethods: MockAuthMethodsWithPasswordType,
6665
isConfirming: true,
6766
},
6867
defaultArgs,

site/src/testHelpers/entities.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,13 @@ export const MockAuthMethods: TypesGen.AuthMethods = {
10241024
convert_to_oidc_enabled: true,
10251025
}
10261026

1027+
export const MockAuthMethodsWithPasswordType: TypesGen.AuthMethods = {
1028+
...MockAuthMethods,
1029+
me_login_type: "password",
1030+
github: { enabled: true },
1031+
oidc: { enabled: true, signInText: "", iconUrl: "" },
1032+
}
1033+
10271034
export const MockGitSSHKey: TypesGen.GitSSHKey = {
10281035
user_id: "1fa0200f-7331-4524-a364-35770666caa7",
10291036
created_at: "2022-05-16T14:30:34.148205897Z",

0 commit comments

Comments
 (0)