Open History to see the last commit
1import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
2import {
3 createMemoryHistory,
4 createRootRoute,
5 createRoute,
6 createRouter,
7 Outlet,
8 RouterProvider,
9} from "@tanstack/react-router";
10import { page, userEvent } from "vite-plus/test/browser";
11import { describe, expect, it, vi } from "vite-plus/test";
12
13import { act, render } from "../../test/render";
14import { __setClientSettingsForTests } from "../hooks/useSettings";
15import { usePanelAnimationSettings, usePanelNavigationSuppression } from "../panelAnimations";
16
17vi.mock(import("../state/server"), async (original) => {
18 const actual = await original();
19 const { Atom } = await import("effect/reactivity");
20 const { compileResolvedKeybindingsConfig } = await import("@t3tools/shared/keybindings");
21 const keybindings = compileResolvedKeybindingsConfig([
22 { key: "mod+b", command: "sidebar.toggle", when: "!terminalFocus" },
23 ]);
24 return { ...actual, primaryServerKeybindingsAtom: Atom.make(keybindings) };
25});
26// The thread list needs live environments; the shortcut lives in the shell around it.
27vi.mock(import("./Sidebar"), () => ({ default: (() => null) as never }));
28
29import { AppSidebarLayout } from "./AppSidebarLayout";
30
31describe("sidebar toggle shortcut", () => {
32 it("leaves the key to a focused terminal when its rule excludes terminalFocus", async () => {
33 await page.viewport(1280, 800);
34 using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
35 await using screen = await render(
36 <AppSidebarLayout>
37 <div data-terminal-owner="drawer">
38 <textarea aria-label="Terminal input" />
39 </div>
40 <button type="button">Outside</button>
41 </AppSidebarLayout>,
42 );
43 const wrapper = screen.container.querySelector('[data-slot="sidebar-wrapper"]')!;
44
45 await userEvent.click(screen.getByRole("textbox", { name: "Terminal input" }));
46 await userEvent.keyboard("{Control>}b{/Control}");
47 expect(wrapper.getAttribute("data-sidebar-state")).toBe("expanded");
48
49 await userEvent.click(screen.getByRole("button", { name: "Outside" }));
50 await userEvent.keyboard("{Control>}b{/Control}");
51 await vi.waitFor(() => expect(wrapper.getAttribute("data-sidebar-state")).toBe("collapsed"));
52 });
53});
54
55function PanelAnimationReader() {
56 return (
57 <output data-panel-animation-reader="">{String(usePanelAnimationSettings().active)}</output>
58 );
59}
60
61function SuppressionProbe(props: { readonly navigationKey: string; readonly animated: boolean }) {
62 return (
63 <output>{String(usePanelNavigationSuppression(props.navigationKey, props.animated))}</output>
64 );
65}
66
67function ShellRoute() {
68 return (
69 <AppSidebarLayout>
70 <PanelAnimationReader />
71 <Outlet />
72 </AppSidebarLayout>
73 );
74}
75
76/** Every value `read` took on after this call, in order; frames may pass between assertions. */
77function recordChanges(target: Element, read: () => string | null) {
78 const seen: Array<string | null> = [];
79 let last = read();
80 const observer = new MutationObserver(() => {
81 const next = read();
82 if (next !== last) seen.push(next);
83 last = next;
84 });
85 observer.observe(target, {
86 attributes: true,
87 characterData: true,
88 childList: true,
89 subtree: true,
90 });
91 return { seen, [Symbol.dispose]: () => observer.disconnect() };
92}
93
94describe("panel animations across a navigation", () => {
95 it("stay on until the next route renders, then hold off until it has painted", async () => {
96 await page.viewport(1280, 800);
97 __setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, panelAnimationDurationMs: 200 });
98 const loaded = Promise.withResolvers<null>();
99 const root = createRootRoute({ component: ShellRoute });
100 const router = createRouter({
101 routeTree: root.addChildren([
102 createRoute({ getParentRoute: () => root, path: "/", component: () => <p>First</p> }),
103 createRoute({
104 getParentRoute: () => root,
105 path: "/next",
106 loader: () => loaded.promise,
107 component: () => <p>Next</p>,
108 }),
109 ]),
110 history: createMemoryHistory({ initialEntries: ["/"] }),
111 disableGlobalCatchBoundary: true,
112 });
113 await using screen = await render(<RouterProvider router={router} />, {
114 router: false, // the routes under test are this router's
115 });
116 const wrapper = screen.container.querySelector('[data-slot="sidebar-wrapper"]')!;
117 const reader = screen.container.querySelector("[data-panel-animation-reader]")!;
118 await vi.waitFor(() => {
119 expect(wrapper.getAttribute("data-panel-animations")).toBe("true");
120 expect(reader.textContent).toBe("true");
121 });
122 using attribute = recordChanges(wrapper, () => wrapper.getAttribute("data-panel-animations"));
123 using context = recordChanges(reader, () => reader.textContent);
124
125 let navigation = Promise.resolve();
126 await act(() => {
127 navigation = router.navigate({ href: "/next" });
128 });
129 expect(router.state.location.pathname).toBe("/next");
130 await expect.element(screen.getByText("First")).toBeInTheDocument();
131 expect([attribute.seen, context.seen]).toEqual([[], []]);
132
133 await act(async () => {
134 loaded.resolve(null);
135 await navigation;
136 });
137 await expect.element(screen.getByText("Next")).toBeInTheDocument();
138 await vi.waitFor(() =>
139 expect([attribute.seen, context.seen]).toEqual([
140 ["false", "true"],
141 ["false", "true"],
142 ]),
143 );
144 });
145
146 it("are never held off while they are switched off", async () => {
147 await using screen = await render(<SuppressionProbe navigationKey="/a" animated={false} />);
148 const output = screen.container.querySelector("output")!;
149 expect(output.textContent).toBe("false");
150 using suppressed = recordChanges(output, () => output.textContent);
151 await screen.rerender(<SuppressionProbe navigationKey="/b" animated={false} />);
152 expect(suppressed.seen).toEqual([]);
153 });
154});