Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Sidebar.search.browser.test.tsx

65 lines2.7 KB

1import { createMemoryHistory } from "@tanstack/react-router";
2import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
3import type * as Context from "effect/Context";
4import { expect, it, vi } from "vite-plus/test";
5import { page, userEvent } from "vite-plus/test/browser";
6​
7import "~/index.css";
8​
9import { AppRoot } from "~/AppRoot";
10import { __setClientSettingsForTests } from "~/hooks/useSettings";
11import * as primaryAuth from "~/environments/primary/auth";
12import { getRouter } from "~/router";
13import {
14 ENVIRONMENT_ID,
15 installFakeEnvironment,
16 threadWithHistory,
17 type FakeDriver,
18} from "../../test/fakeEnvironment";
19import { render } from "../../test/render";
20​
21vi.mock("~/environments/primary/auth", { spy: true });
22vi.mock("~/components/ThreadRouteView", () => ({ ThreadRouteView: () => null }));
23​
24vi.mock("../../../../packages/client-runtime/src/connection/driver.ts", async (importOriginal) => {
25 const [actual, { fakeDriver }, Layer] = await Promise.all([
26 importOriginal<{ readonly ConnectionDriver: Context.Key<unknown, FakeDriver> }>(),
27 import("../../test/fakeEnvironment"),
28 import("effect/Layer"),
29 ]);
30 return { ...actual, layer: Layer.succeed(actual.ConnectionDriver, fakeDriver) };
31});
32​
33it("keeps the search query and results after opening a result", async () => {
34 const first = threadWithHistory("search-first", 1);
35 const second = threadWithHistory("search-second", 1);
36 using _environment = installFakeEnvironment([first, second]);
37 __setClientSettingsForTests({
38 ...DEFAULT_CLIENT_SETTINGS,
39 onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
40 });
41 using _auth = vi
42 .spyOn(primaryAuth, "resolveInitialServerAuthGateState")
43 .mockResolvedValue({ status: "authenticated" });
44 await page.viewport(1400, 900);
45 const router = getRouter(
46 createMemoryHistory({ initialEntries: [`/${ENVIRONMENT_ID}/${second.id}`] }),
47 );
48 await router.load();
49 // AppRoot owns the application router.
50 await using screen = await render(<AppRoot router={router} />, { router: false });
51 const search = screen.getByRole("combobox", { name: "Search threads" });
52 const results = screen.getByRole("listbox", { name: "Thread search results" });
53 await vi.waitFor(() => {
54 expect(router.state.location.pathname).toBe(`/${ENVIRONMENT_ID}/${second.id}`);
55 });
56 await userEvent.fill(search, "search-");
57 await expect.element(results.getByRole("option")).toHaveLength(2);
58​
59 await results.getByRole("option", { name: new RegExp(first.title) }).click();
60 await vi.waitFor(() => {
61 expect(router.state.location.pathname).toBe(`/${ENVIRONMENT_ID}/${first.id}`);
62 });
63 await expect.element(search).toHaveValue("search-");
64 await expect.element(results.getByRole("option")).toHaveLength(2);
65});