|
| 1 | +import { screen, waitFor } from "@testing-library/react" |
| 2 | +import userEvent from "@testing-library/user-event" |
| 3 | +import * as API from "api/api" |
| 4 | +import { rest } from "msw" |
| 5 | +import { history, MockUser, render } from "testHelpers/renderHelpers" |
| 6 | +import { server } from "testHelpers/server" |
| 7 | +import { Language as SetupLanguage } from "xServices/setup/setupXService" |
| 8 | +import { SetupPage } from "./SetupPage" |
| 9 | +import { Language as PageViewLanguage } from "./SetupPageView" |
| 10 | + |
| 11 | +const fillForm = async ({ |
| 12 | + username = "someuser", |
| 13 | + email = "someone@coder.com", |
| 14 | + password = "password", |
| 15 | + organization = "Coder", |
| 16 | +}: { |
| 17 | + username?: string |
| 18 | + email?: string |
| 19 | + password?: string |
| 20 | + organization?: string |
| 21 | +} = {}) => { |
| 22 | + const usernameField = screen.getByLabelText(PageViewLanguage.usernameLabel) |
| 23 | + const emailField = screen.getByLabelText(PageViewLanguage.emailLabel) |
| 24 | + const passwordField = screen.getByLabelText(PageViewLanguage.passwordLabel) |
| 25 | + const organizationField = screen.getByLabelText(PageViewLanguage.organizationLabel) |
| 26 | + await userEvent.type(organizationField, organization) |
| 27 | + await userEvent.type(usernameField, username) |
| 28 | + await userEvent.type(emailField, email) |
| 29 | + await userEvent.type(passwordField, password) |
| 30 | + const submitButton = screen.getByRole("button", { name: PageViewLanguage.create }) |
| 31 | + submitButton.click() |
| 32 | +} |
| 33 | + |
| 34 | +describe("Setup Page", () => { |
| 35 | + beforeEach(() => { |
| 36 | + history.replace("/setup") |
| 37 | + // appear logged out |
| 38 | + server.use( |
| 39 | + rest.get("/api/v2/users/me", (req, res, ctx) => { |
| 40 | + return res(ctx.status(401), ctx.json({ message: "no user here" })) |
| 41 | + }), |
| 42 | + ) |
| 43 | + }) |
| 44 | + |
| 45 | + it("shows validation error message", async () => { |
| 46 | + render(<SetupPage />) |
| 47 | + await fillForm({ email: "test" }) |
| 48 | + const errorMessage = await screen.findByText(PageViewLanguage.emailInvalid) |
| 49 | + expect(errorMessage).toBeDefined() |
| 50 | + }) |
| 51 | + |
| 52 | + it("shows generic error message", async () => { |
| 53 | + jest.spyOn(API, "createFirstUser").mockRejectedValueOnce({ |
| 54 | + data: "unknown error", |
| 55 | + }) |
| 56 | + render(<SetupPage />) |
| 57 | + await fillForm() |
| 58 | + const errorMessage = await screen.findByText(SetupLanguage.createFirstUserError) |
| 59 | + expect(errorMessage).toBeDefined() |
| 60 | + }) |
| 61 | + |
| 62 | + it("shows API error message", async () => { |
| 63 | + const fieldErrorMessage = "invalid username" |
| 64 | + server.use( |
| 65 | + rest.post("/api/v2/users/first", async (req, res, ctx) => { |
| 66 | + return res( |
| 67 | + ctx.status(400), |
| 68 | + ctx.json({ |
| 69 | + message: "invalid field", |
| 70 | + validations: [ |
| 71 | + { |
| 72 | + detail: fieldErrorMessage, |
| 73 | + field: "username", |
| 74 | + }, |
| 75 | + ], |
| 76 | + }), |
| 77 | + ) |
| 78 | + }), |
| 79 | + ) |
| 80 | + render(<SetupPage />) |
| 81 | + await fillForm() |
| 82 | + const errorMessage = await screen.findByText(fieldErrorMessage) |
| 83 | + expect(errorMessage).toBeDefined() |
| 84 | + }) |
| 85 | + |
| 86 | + it("redirects to workspaces page when success", async () => { |
| 87 | + render(<SetupPage />) |
| 88 | + |
| 89 | + // simulates the user will be authenticated |
| 90 | + server.use( |
| 91 | + rest.get("/api/v2/users/me", (req, res, ctx) => { |
| 92 | + return res(ctx.status(200), ctx.json(MockUser)) |
| 93 | + }), |
| 94 | + ) |
| 95 | + |
| 96 | + await fillForm() |
| 97 | + await waitFor(() => expect(history.location.pathname).toEqual("/workspaces")) |
| 98 | + }) |
| 99 | +}) |
0 commit comments