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

PreviewPanelShell.browser.test.tsx

168 lines7.5 KB

1import type { ReactNode } from "react";
2import { createPortal } from "react-dom";
3import { describe, expect, it } from "vite-plus/test";
4import { page, userEvent } from "vite-plus/test/browser";
5​
6import { act, render, type Screen } from "../../../test/render";
7import { PreviewPanelShell } from "./PreviewPanelShell";
8​
9function Workspace(props: { open: boolean; present?: boolean; children?: ReactNode }) {
10 return (
11 <div>
12 <div data-workspace-area="composer">
13 <input aria-label="Message New thread" />
14 </div>
15 <button type="button" aria-label="Toggle right panel" data-right-panel-toggle />
16 {(props.present ?? true) ? (
17 <PreviewPanelShell mode="inline" open={props.open}>
18 {props.children ?? <div role="region" aria-label="Open a surface" tabIndex={-1} />}
19 </PreviewPanelShell>
20 ) : null}
21 </div>
22 );
24​
25// Mirrors rightPanel.toggle from the composer: the panel opens and the shortcut focuses its content.
26async function openPanelFromComposer(screen: Screen) {
27 await userEvent.click(screen.getByRole("textbox", { name: "Message New thread" }));
28 await screen.rerender(<Workspace open />);
29 await act(() => screen.getByRole("region", { name: "Open a surface" }).element().focus());
31​
32describe("PreviewPanelShell focus", () => {
33 it("returns focus to the composer when a panel opened from it collapses", async () => {
34 await using screen = await render(<Workspace open={false} />);
35 await openPanelFromComposer(screen);
36​
37 await screen.rerender(<Workspace open={false} />);
38​
39 await expect.element(screen.getByRole("textbox", { name: "Message New thread" })).toHaveFocus();
40 });
41​
42 it("returns focus to the composer when a panel opened from it unmounts", async () => {
43 await using screen = await render(<Workspace open={false} />);
44 await openPanelFromComposer(screen);
45​
46 await screen.rerender(<Workspace open={false} present={false} />);
47​
48 await expect.element(screen.getByRole("textbox", { name: "Message New thread" })).toHaveFocus();
49 });
50​
51 it("returns focus to the composer when the panel's content takes focus as it mounts", async () => {
52 // A terminal surface focuses itself from its ref callback, before the shell's own ref attaches.
53 const focusOnMount = (element: HTMLElement | null) => element?.focus();
54 const panel = (present: boolean) => (
55 <Workspace open present={present}>
56 <div role="region" aria-label="Terminal" tabIndex={-1} ref={focusOnMount} />
57 </Workspace>
58 );
59 await using screen = await render(panel(false));
60 await userEvent.click(screen.getByRole("textbox", { name: "Message New thread" }));
61 await screen.rerender(panel(true));
62 await expect.element(screen.getByRole("region", { name: "Terminal" })).toHaveFocus();
63​
64 await screen.rerender(panel(false));
65​
66 await expect.element(screen.getByRole("textbox", { name: "Message New thread" })).toHaveFocus();
67 });
68​
69 it("keeps the composer as the origin after focus visits a menu portalled from the panel", async () => {
70 const panel = (open: boolean) => (
71 <Workspace open={open}>
72 <div role="region" aria-label="Open a surface" tabIndex={-1} />
73 {createPortal(<button type="button">Add panel surface</button>, document.body)}
74 </Workspace>
75 );
76 await using screen = await render(panel(false));
77 await userEvent.click(screen.getByRole("textbox", { name: "Message New thread" }));
78 await screen.rerender(panel(true));
79 await act(() => screen.getByRole("region", { name: "Open a surface" }).element().focus());
80 await act(() => page.getByRole("button", { name: "Add panel surface" }).element().focus());
81 await act(() => screen.getByRole("region", { name: "Open a surface" }).element().focus());
82​
83 await screen.rerender(panel(false));
84​
85 await expect.element(screen.getByRole("textbox", { name: "Message New thread" })).toHaveFocus();
86 });
87​
88 it("falls back to the toggle without scrolling when the origin has scrolled out of view", async () => {
89 const layout = (open: boolean) => (
90 <div>
91 <div
92 data-testid="timeline"
93 data-workspace-area="conversation"
94 style={{ height: 100, overflowY: "auto" }}
95 >
96 <button type="button">Copy message</button>
97 <div style={{ height: 2000 }} />
98 </div>
99 <button type="button" aria-label="Toggle right panel" data-right-panel-toggle />
100 <PreviewPanelShell mode="inline" open={open}>
101 <div role="region" aria-label="Open a surface" tabIndex={-1} />
102 </PreviewPanelShell>
103 </div>
104 );
105 await using screen = await render(layout(false));
106 await userEvent.click(screen.getByRole("button", { name: "Copy message" }));
107 await screen.rerender(layout(true));
108 await act(() => screen.getByRole("region", { name: "Open a surface" }).element().focus());
109 const timeline = screen.getByTestId("timeline").element();
110 timeline.scrollTop = 1500;
111​
112 await screen.rerender(layout(false));
113​
114 await expect.element(screen.getByRole("button", { name: "Toggle right panel" })).toHaveFocus();
115 expect(timeline.scrollTop).toBe(1500);
116 });
117​
118 it("falls back to the toggle when the origin's drawer is closing", async () => {
119 // A closing drawer stays mounted for its exit animation but is no longer a workspace area.
120 const layout = (open: boolean, drawerOpen: boolean) => (
121 <div>
122 <div data-workspace-area={drawerOpen ? "terminal" : undefined}>
123 <input aria-label="Terminal input" />
124 </div>
125 <button type="button" aria-label="Toggle right panel" data-right-panel-toggle />
126 <PreviewPanelShell mode="inline" open={open}>
127 <div role="region" aria-label="Open a surface" tabIndex={-1} />
128 </PreviewPanelShell>
129 </div>
130 );
131 await using screen = await render(layout(false, true));
132 await userEvent.click(screen.getByRole("textbox", { name: "Terminal input" }));
133 await screen.rerender(layout(true, true));
134 await act(() => screen.getByRole("region", { name: "Open a surface" }).element().focus());
135 await screen.rerender(layout(true, false));
136​
137 await screen.rerender(layout(false, false));
138​
139 await expect.element(screen.getByRole("button", { name: "Toggle right panel" })).toHaveFocus();
140 });
141​
142 it("does not return focus to the origin of an earlier open", async () => {
143 await using screen = await render(<Workspace open={false} />);
144 await openPanelFromComposer(screen);
145 await screen.rerender(<Workspace open={false} />);
146 const composer = screen.getByRole("textbox", { name: "Message New thread" });
147 await expect.element(composer).toHaveFocus();
148 await act(() => composer.element().blur());
149 await screen.rerender(<Workspace open />);
150 await act(() => screen.getByRole("region", { name: "Open a surface" }).element().focus());
151​
152 await screen.rerender(<Workspace open={false} />);
153​
154 await expect.element(screen.getByRole("button", { name: "Toggle right panel" })).toHaveFocus();
155 });
156​
157 it("returns focus to the toggle when a panel opened from it collapses", async () => {
158 await using screen = await render(<Workspace open={false} />);
159 await userEvent.click(screen.getByRole("textbox", { name: "Message New thread" }));
160 await userEvent.click(screen.getByRole("button", { name: "Toggle right panel" }));
161 await screen.rerender(<Workspace open />);
162 await act(() => screen.getByRole("region", { name: "Open a surface" }).element().focus());
163​
164 await screen.rerender(<Workspace open={false} />);
165​
166 await expect.element(screen.getByRole("button", { name: "Toggle right panel" })).toHaveFocus();
167 });
168});