|
1 | 1 | import { fireEvent, screen, waitFor } from "@testing-library/react"
|
2 | 2 | import userEvent from "@testing-library/user-event"
|
3 | 3 | import { rest } from "msw"
|
4 |
| -import { render } from "testHelpers/renderHelpers" |
| 4 | +import { createMemoryRouter } from "react-router-dom" |
| 5 | +import { render, renderWithRouter } from "testHelpers/renderHelpers" |
5 | 6 | import { server } from "testHelpers/server"
|
6 | 7 | import { SetupPage } from "./SetupPage"
|
7 | 8 | import { Language as PageViewLanguage } from "./SetupPageView"
|
@@ -35,6 +36,12 @@ describe("Setup Page", () => {
|
35 | 36 | rest.get("/api/v2/users/me", (req, res, ctx) => {
|
36 | 37 | return res(ctx.status(401), ctx.json({ message: "no user here" }))
|
37 | 38 | }),
|
| 39 | + rest.get("/api/v2/users/first", (req, res, ctx) => { |
| 40 | + return res( |
| 41 | + ctx.status(404), |
| 42 | + ctx.json({ message: "no first user has been created" }), |
| 43 | + ) |
| 44 | + }), |
38 | 45 | )
|
39 | 46 | })
|
40 | 47 |
|
@@ -63,23 +70,109 @@ describe("Setup Page", () => {
|
63 | 70 | )
|
64 | 71 | }),
|
65 | 72 | )
|
| 73 | + |
66 | 74 | render(<SetupPage />)
|
67 | 75 | await fillForm()
|
68 | 76 | const errorMessage = await screen.findByText(fieldErrorMessage)
|
69 | 77 | expect(errorMessage).toBeDefined()
|
70 | 78 | })
|
71 | 79 |
|
72 |
| - it("redirects to workspaces page when success", async () => { |
| 80 | + it("redirects to the app when setup is successful", async () => { |
| 81 | + let userHasBeenCreated = false |
| 82 | + |
| 83 | + server.use( |
| 84 | + rest.get("/api/v2/users/me", (req, res, ctx) => { |
| 85 | + if (!userHasBeenCreated) { |
| 86 | + return res(ctx.status(401), ctx.json({ message: "no user here" })) |
| 87 | + } |
| 88 | + return res(ctx.status(200), ctx.json(MockUser)) |
| 89 | + }), |
| 90 | + rest.get("/api/v2/users/first", (req, res, ctx) => { |
| 91 | + if (!userHasBeenCreated) { |
| 92 | + return res( |
| 93 | + ctx.status(404), |
| 94 | + ctx.json({ message: "no first user has been created" }), |
| 95 | + ) |
| 96 | + } |
| 97 | + return res( |
| 98 | + ctx.status(200), |
| 99 | + ctx.json({ message: "hooray, someone exists!" }), |
| 100 | + ) |
| 101 | + }), |
| 102 | + rest.post("/api/v2/users/first", (req, res, ctx) => { |
| 103 | + userHasBeenCreated = true |
| 104 | + return res( |
| 105 | + ctx.status(200), |
| 106 | + ctx.json({ data: "user setup was successful!" }), |
| 107 | + ) |
| 108 | + }), |
| 109 | + ) |
| 110 | + |
73 | 111 | render(<SetupPage />)
|
| 112 | + await fillForm() |
| 113 | + await waitFor(() => expect(window.location).toBeAt("/")) |
| 114 | + }) |
| 115 | + |
| 116 | + it("redirects to login if setup has already completed", async () => { |
| 117 | + // simulates setup having already been completed |
| 118 | + server.use( |
| 119 | + rest.get("/api/v2/users/first", (req, res, ctx) => { |
| 120 | + return res( |
| 121 | + ctx.status(200), |
| 122 | + ctx.json({ message: "hooray, someone exists!" }), |
| 123 | + ) |
| 124 | + }), |
| 125 | + ) |
| 126 | + |
| 127 | + renderWithRouter( |
| 128 | + createMemoryRouter( |
| 129 | + [ |
| 130 | + { |
| 131 | + path: "/setup", |
| 132 | + element: <SetupPage />, |
| 133 | + }, |
| 134 | + { |
| 135 | + path: "/login", |
| 136 | + element: <h1>Login</h1>, |
| 137 | + }, |
| 138 | + ], |
| 139 | + { initialEntries: ["/setup"] }, |
| 140 | + ), |
| 141 | + ) |
74 | 142 |
|
| 143 | + await screen.findByText("Login") |
| 144 | + }) |
| 145 | + |
| 146 | + it("redirects to the app when already logged in", async () => { |
75 | 147 | // simulates the user will be authenticated
|
76 | 148 | server.use(
|
77 | 149 | rest.get("/api/v2/users/me", (req, res, ctx) => {
|
78 | 150 | return res(ctx.status(200), ctx.json(MockUser))
|
79 | 151 | }),
|
| 152 | + rest.get("/api/v2/users/first", (req, res, ctx) => { |
| 153 | + return res( |
| 154 | + ctx.status(200), |
| 155 | + ctx.json({ message: "hooray, someone exists!" }), |
| 156 | + ) |
| 157 | + }), |
80 | 158 | )
|
81 | 159 |
|
82 |
| - await fillForm() |
83 |
| - await waitFor(() => expect(window.location).toBeAt("/workspaces")) |
| 160 | + renderWithRouter( |
| 161 | + createMemoryRouter( |
| 162 | + [ |
| 163 | + { |
| 164 | + path: "/setup", |
| 165 | + element: <SetupPage />, |
| 166 | + }, |
| 167 | + { |
| 168 | + path: "/", |
| 169 | + element: <h1>Workspaces</h1>, |
| 170 | + }, |
| 171 | + ], |
| 172 | + { initialEntries: ["/setup"] }, |
| 173 | + ), |
| 174 | + ) |
| 175 | + |
| 176 | + await screen.findByText("Workspaces") |
84 | 177 | })
|
85 | 178 | })
|
0 commit comments