Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 15f7d35

Browse files
committed
RED: add unit tests for AccountForm username field
1 parent e44f7ad commit 15f7d35

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { screen } from "@testing-library/react"
2+
import { MockUser2 } from "../../testHelpers/entities"
3+
import { render } from "../../testHelpers/renderHelpers"
4+
import { AccountForm, AccountFormValues } from "./SettingsAccountForm"
5+
6+
// NOTE: it does not matter what the role props of MockUser are set to,
7+
// only that editable is set to true or false. This is passed from
8+
// the call to /authorization done by authXService
9+
describe("AccountForm", () => {
10+
describe("for owners", () => {
11+
it("allows updating username", async () => {
12+
// Given
13+
const mockInitialValues: AccountFormValues = {
14+
username: MockUser2.username,
15+
editable: true,
16+
}
17+
18+
// When
19+
render(
20+
<AccountForm
21+
email={MockUser2.email}
22+
initialValues={mockInitialValues}
23+
isLoading={false}
24+
onSubmit={() => {
25+
return
26+
}}
27+
/>,
28+
)
29+
30+
// Then
31+
const el = await screen.findByLabelText("Username")
32+
expect(el).toBeEnabled()
33+
})
34+
})
35+
36+
describe("for user admins", () => {
37+
it("allows updating username", async () => {
38+
// Given
39+
const mockInitialValues: AccountFormValues = {
40+
username: MockUser2.username,
41+
editable: true,
42+
}
43+
44+
// When
45+
render(
46+
<AccountForm
47+
email={MockUser2.email}
48+
initialValues={mockInitialValues}
49+
isLoading={false}
50+
onSubmit={() => {
51+
return
52+
}}
53+
/>,
54+
)
55+
56+
// Then
57+
const el = await screen.findByLabelText("Username")
58+
expect(el).toBeEnabled()
59+
})
60+
})
61+
62+
describe("for members", () => {
63+
it("allows updating username", async () => {
64+
// Given
65+
const mockInitialValues: AccountFormValues = {
66+
username: MockUser2.username,
67+
editable: false,
68+
}
69+
70+
// When
71+
render(
72+
<AccountForm
73+
email={MockUser2.email}
74+
initialValues={mockInitialValues}
75+
isLoading={false}
76+
onSubmit={() => {
77+
return
78+
}}
79+
/>,
80+
)
81+
82+
// Then
83+
const el = await screen.findByLabelText("Username")
84+
expect(el).toBeDisabled()
85+
})
86+
})
87+
})

site/src/components/SettingsAccountForm/SettingsAccountForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { getFormHelpersWithError, nameValidator, onChangeTrimmed } from "../../u
77
import { LoadingButton } from "../LoadingButton/LoadingButton"
88
import { Stack } from "../Stack/Stack"
99

10-
interface AccountFormValues {
10+
export interface AccountFormValues {
1111
username: string
1212
}
1313

site/src/testHelpers/entities.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ export const MockUser: TypesGen.User = {
6161
roles: [MockOwnerRole],
6262
}
6363

64+
export const MockUserAdmin: TypesGen.User = {
65+
id: "test-user",
66+
username: "TestUser",
67+
68+
created_at: "",
69+
status: "active",
70+
organization_ids: ["fc0774ce-cc9e-48d4-80ae-88f7a4d4a8b0"],
71+
roles: [MockUserAdminRole],
72+
}
73+
6474
export const MockUser2: TypesGen.User = {
6575
id: "test-user-2",
6676
username: "TestUser2",

0 commit comments

Comments
 (0)