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

useCollapsedSplitColumn.test.tsx

58 lines2.2 KB

1import type { NavigationState } from "@react-navigation/native";
2import { act } from "react";
3import { createRoot } from "react-dom/client";
4import { expect, it, vi } from "vite-plus/test";
5​
6vi.mock("./workspace/workspace-detail-selection", () => ({
7 setNativelyPoppingWorkspaceRoute: () => {},
8}));
9​
10import { useCollapsedSplitColumn } from "./useCollapsedSplitColumn";
11​
12const home = { key: "home", name: "Home" };
13const left = { key: "thread-a", name: "Thread" };
14const picked = { key: "thread-b", name: "Thread" };
15​
16function routerState(routes: ReadonlyArray<{ key: string; name: string }>) {
17 return { key: "stack", index: routes.length - 1, routes } as unknown as NavigationState;
19​
20it("shows a thread picked while folded Back is still revealing the list", () => {
21 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
22 let state = routerState([home, left]);
23 const show = vi.fn();
24 const navigation = { getState: () => state, dispatch: vi.fn() };
25 const hostRef = { current: { show } as never };
26 let split: ReturnType<typeof useCollapsedSplitColumn> | undefined;
27 function Columns() {
28 split = useCollapsedSplitColumn({
29 hostRef,
30 usesSplitView: false,
31 compactColumn: state.index > 0 ? "secondary" : "primary",
32 primaryKey: home.key,
33 navigation,
34 });
35 return null;
36 }
37 const root = createRoot(document.createElement("div"));
38 const render = () => act(() => root.render(<Columns />));
39 try {
40 render();
41 act(() => split!.followAppearance("willDisappear", left.key));
42 act(() => split!.followAppearance("willAppear", home.key));
43 // The list is tappable from willAppear, so the next thread is picked before didAppear.
44 state = routerState([home, left, picked]);
45 render();
46 act(() => split!.followAppearance("didAppear", home.key));
47 expect(navigation.dispatch).toHaveBeenCalledWith(
48 expect.objectContaining({ type: "POP", source: left.key }),
49 );
50 // Back's pop keeps the picked thread, so the compact column stays "secondary" across renders.
51 state = routerState([home, picked]);
52 render();
53 expect(show).toHaveBeenLastCalledWith("secondary");
54 } finally {
55 act(() => root.unmount());
56 vi.unstubAllGlobals();
57 }
58});