Skip to content

Commit aa0b4d2

Browse files
committed
test(web): align auth route expectations
1 parent cce7c0d commit aa0b4d2

2 files changed

Lines changed: 30 additions & 22 deletions

File tree

packages/web/src/shells/desktop-shell.test.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,21 @@ describe("DesktopShell auth gating", () => {
8080
expect(screen.queryByText("LoginPage")).not.toBeInTheDocument();
8181
});
8282

83-
it("keeps / on WelcomePage when auth is enabled and user is unauthenticated", () => {
83+
it("redirects / to /login when auth is enabled and user is unauthenticated", async () => {
8484
const store = createStore();
8585
store.set(connectionStatusAtom, "connected");
8686
store.set(authEnabledAtom, true);
8787
store.set(authenticatedAtom, false);
8888

8989
renderShell(store);
9090

91-
expect(screen.getByText("WelcomePage")).toBeInTheDocument();
92-
expect(screen.queryByText("LoginPage")).not.toBeInTheDocument();
91+
await waitFor(() => {
92+
expect(window.location.pathname).toBe("/login");
93+
expect(screen.getByText("LoginPage")).toBeInTheDocument();
94+
});
9395
});
9496

95-
it("does not bootstrap workspaces from / when auth is enabled and user is unauthenticated", () => {
97+
it("does not bootstrap workspaces from / before redirecting to /login when auth is enabled and user is unauthenticated", async () => {
9698
const sendCommand = vi.fn();
9799
const store = createStore();
98100
store.set(connectionStatusAtom, "connected");
@@ -113,9 +115,11 @@ describe("DesktopShell auth gating", () => {
113115

114116
renderShell(store);
115117

116-
expect(window.location.pathname).toBe("/");
117-
expect(screen.getByText("WelcomePage")).toBeInTheDocument();
118-
expect(sendCommand).not.toHaveBeenCalled();
118+
await waitFor(() => {
119+
expect(window.location.pathname).toBe("/login");
120+
expect(screen.getByText("LoginPage")).toBeInTheDocument();
121+
expect(sendCommand).not.toHaveBeenCalled();
122+
});
119123
});
120124

121125
it("redirects /workspace to /login when auth is enabled and user is unauthenticated", async () => {
@@ -247,7 +251,7 @@ describe("DesktopShell auth gating", () => {
247251
expect(screen.getByText("Page not found")).toBeInTheDocument();
248252
});
249253

250-
it("does not rewrite /auth to /login on desktop when auth is required and user is unauthenticated", async () => {
254+
it("rewrites /auth to /login on desktop when auth is required and user is unauthenticated", async () => {
251255
window.history.replaceState({}, "", "/auth");
252256

253257
const store = createStore();
@@ -258,9 +262,8 @@ describe("DesktopShell auth gating", () => {
258262
renderShell(store);
259263

260264
await waitFor(() => {
261-
expect(screen.getByText("Page not found")).toBeInTheDocument();
262-
expect(window.location.pathname).toBe("/auth");
263-
expect(screen.queryByText("LoginPage")).not.toBeInTheDocument();
265+
expect(window.location.pathname).toBe("/login");
266+
expect(screen.getByText("LoginPage")).toBeInTheDocument();
264267
});
265268
});
266269

packages/web/src/shells/mobile-shell/index.test.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Session } from "@coder-studio/core";
2-
import { fireEvent, render, screen, waitFor, within } from "@testing-library/react";
2+
import { act, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
33
import userEvent from "@testing-library/user-event";
44
import { createStore, Provider } from "jotai";
55
import type { ReactNode } from "react";
@@ -820,7 +820,7 @@ describe("MobileShell Phase 2 workspace", () => {
820820
expect(screen.queryByRole("button", { name: "Switch workspace" })).not.toBeInTheDocument();
821821
});
822822

823-
it("does not bootstrap workspaces from / on mobile when auth is enabled and user is unauthenticated", () => {
823+
it("does not bootstrap workspaces from / on mobile before redirecting to /login when auth is enabled and user is unauthenticated", async () => {
824824
const sendCommand = vi.fn();
825825
const store = createStore();
826826
store.set(connectionStatusAtom, "connected");
@@ -851,9 +851,11 @@ describe("MobileShell Phase 2 workspace", () => {
851851
</Provider>
852852
);
853853

854-
expect(screen.getByText("WelcomePage")).toBeInTheDocument();
855-
expect(screen.getByTestId("location-display")).toHaveTextContent("/");
856-
expect(sendCommand).not.toHaveBeenCalled();
854+
await waitFor(() => {
855+
expect(screen.getByText("LoginPage")).toBeInTheDocument();
856+
expect(screen.getByTestId("location-display")).toHaveTextContent("/login");
857+
expect(sendCommand).not.toHaveBeenCalled();
858+
});
857859
});
858860

859861
it("keeps the explicit /login route on mobile when auth is enabled and user is unauthenticated", async () => {
@@ -910,7 +912,7 @@ describe("MobileShell Phase 2 workspace", () => {
910912
expect(screen.getByText("Page not found")).toBeInTheDocument();
911913
});
912914

913-
it("does not rewrite /auth to /login on mobile when auth is required and user is unauthenticated", async () => {
915+
it("rewrites /auth to /login on mobile when auth is required and user is unauthenticated", async () => {
914916
const store = createStore();
915917
store.set(connectionStatusAtom, "connected");
916918
store.set(authEnabledAtom, true);
@@ -926,9 +928,8 @@ describe("MobileShell Phase 2 workspace", () => {
926928
);
927929

928930
await waitFor(() => {
929-
expect(screen.getByText("Page not found")).toBeInTheDocument();
930-
expect(screen.getByTestId("location-display")).toHaveTextContent("/auth");
931-
expect(screen.queryByText("LoginPage")).not.toBeInTheDocument();
931+
expect(screen.getByText("LoginPage")).toBeInTheDocument();
932+
expect(screen.getByTestId("location-display")).toHaveTextContent("/login");
932933
});
933934
});
934935

@@ -1458,12 +1459,16 @@ describe("MobileShell Phase 2 workspace", () => {
14581459
expect(store.get(visibleMobileSessionIdAtom)).toBe("sess_2");
14591460
});
14601461

1461-
store.set(pendingFocusSessionAtom, "sess_1");
1462+
await act(async () => {
1463+
store.set(pendingFocusSessionAtom, "sess_1");
1464+
});
14621465

14631466
await waitFor(() => {
14641467
expect(screen.getByTestId("mobile-session-card")).toHaveTextContent("sess_1");
14651468
});
1466-
expect(store.get(visibleMobileSessionIdAtom)).toBe("sess_1");
1469+
await waitFor(() => {
1470+
expect(store.get(visibleMobileSessionIdAtom)).toBe("sess_1");
1471+
});
14671472

14681473
unmount();
14691474

0 commit comments

Comments
 (0)