Skip to content

Commit 7d6a03f

Browse files
committed
Tests - wip
1 parent f46799e commit 7d6a03f

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { act, screen } from "@testing-library/react"
2+
import userEvent from "@testing-library/user-event"
3+
import { rest } from "msw"
4+
import React from "react"
5+
import { Language as FormLanguage } from "../../../components/CreateUserForm/CreateUserForm"
6+
import { Language as FooterLanguage } from "../../../components/FormFooter/FormFooter"
7+
import { Language as UserLanguage } from "../../../xServices/users/usersXService"
8+
import { history, render } from "../../../testHelpers"
9+
import { server } from "../../../testHelpers/server"
10+
import { CreateUserPage, Language } from "./CreateUserPage"
11+
12+
const fillForm = async ({
13+
username = "testuser",
14+
email = "test@coder.com",
15+
password = "password",
16+
}: {
17+
username?: string
18+
email?: string
19+
password?: string
20+
}) => {
21+
const usernameField = screen.getByLabelText(FormLanguage.usernameLabel)
22+
const emailField = screen.getByLabelText(FormLanguage.emailLabel)
23+
const passwordField = screen.getByLabelText(FormLanguage.passwordLabel)
24+
await userEvent.type(usernameField, username)
25+
await userEvent.type(emailField, email)
26+
await userEvent.type(passwordField, password)
27+
const submitButton = await screen.findByText(FooterLanguage.defaultSubmitLabel)
28+
act(() => submitButton.click())
29+
}
30+
31+
describe("Create User Page", () => {
32+
beforeEach(() => {
33+
history.replace("/users/create")
34+
})
35+
36+
it("shows validation error message", async () => {
37+
render(<CreateUserPage />)
38+
await fillForm({ email: "test" })
39+
const errorMessage = await screen.findByText(FormLanguage.emailInvalid)
40+
expect(errorMessage).toBeDefined()
41+
})
42+
it("shows generic error message", async () => {
43+
server.use(
44+
rest.post("/api/v2/users", (req, res, ctx) => {
45+
Promise.reject("something went wrong")
46+
}),
47+
)
48+
render(<CreateUserPage />)
49+
await fillForm({})
50+
const errorMessage = await screen.findByText(Language.unknownError)
51+
expect(errorMessage).toBeDefined()
52+
})
53+
it("shows API error message", async () => {
54+
const fieldErrorMessage = "username already in use"
55+
server.use(
56+
rest.post("/api/v2/users", (req, res, ctx) => {
57+
return res(ctx.status(400), ctx.json({
58+
message: "invalid field",
59+
errors: [{
60+
detail: fieldErrorMessage,
61+
field: "username"
62+
}]
63+
}))
64+
}),
65+
)
66+
render(<CreateUserPage />)
67+
await fillForm({})
68+
const errorMessage = await screen.findByText(fieldErrorMessage)
69+
expect(errorMessage).toBeDefined()
70+
})
71+
it("shows success notification and redirects to users page", async () => {
72+
render(<CreateUserPage />)
73+
await fillForm({})
74+
const successMessage = screen.findByText(UserLanguage.createUserSuccess)
75+
expect(successMessage).toBeDefined()
76+
expect(history.location.pathname).toEqual("/users")
77+
})
78+
it("redirects to users page on cancel", () => {
79+
render(<CreateUserPage />)
80+
screen.findByText(FooterLanguage.cancelLabel)
81+
expect(history.location.pathname).toEqual("/users")
82+
})
83+
it("redirects to users page on close", () => {
84+
render(<CreateUserPage />)
85+
screen.findByText("ESC")
86+
expect(history.location.pathname).toEqual("/users")
87+
})
88+
})

0 commit comments

Comments
 (0)