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

PreviewPanelShell.a11y.browser.test.tsx

171 lines6.2 KB

1import { Dialog } from "@base-ui/react/dialog";
2import { act } from "react";
3import { createRoot, type Root } from "react-dom/client";
4import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
5import { userEvent } from "vite-plus/test/browser";
6​
7import { PanelLayoutControls } from "../chat/PanelLayoutControls";
8import { PreviewPanelShell, type PreviewPanelMode } from "./PreviewPanelShell";
9​
10let root: Root;
11let container: HTMLDivElement;
12​
13beforeEach(() => {
14 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
15 container = document.createElement("div");
16 document.body.append(container);
17 root = createRoot(container);
18});
19​
20afterEach(async () => {
21 await act(async () => root.unmount());
22 container.remove();
23 vi.unstubAllGlobals();
24});
25​
26async function renderPanel(
27 open: boolean | undefined,
28 mode: PreviewPanelMode = "inline",
29 present = true,
30) {
31 await act(async () => {
32 root.render(
33 <Dialog.Root open>
34 <Dialog.Portal>
35 <Dialog.Popup aria-label="Workspace" finalFocus={false}>
36 {present ? (
37 <PreviewPanelShell mode={mode} {...(open === undefined ? {} : { open })}>
38 <input aria-label="Preview URL" />
39 <button type="button">Reload preview</button>
40 </PreviewPanelShell>
41 ) : null}
42 <button type="button" data-testid="outside">
43 Send message
44 </button>
45 <div key={present ? "root-controls" : "header-controls"}>
46 <PanelLayoutControls
47 showTerminalControl={false}
48 showThreadPanelControl={false}
49 terminalAvailable={false}
50 terminalOpen={false}
51 terminalShortcutLabel={null}
52 threadPanelOpen={false}
53 threadPanelPresentation="inline"
54 threadPanelShortcutLabel={null}
55 rightPanelAvailable
56 rightPanelOpen={open ?? true}
57 rightPanelShortcutLabel={null}
58 onToggleTerminal={() => {}}
59 onToggleThreadPanel={() => {}}
60 onToggleRightPanel={() => {}}
61 />
62 </div>
63 </Dialog.Popup>
64 </Dialog.Portal>
65 </Dialog.Root>,
66 );
67 });
68 await act(nextFrame);
70​
71function nextFrame() {
72 return new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
74​
75// Tabbing past the popup's last control wraps through Base UI's focus guard
76// to the first tabbable descendant, which skips anything inert.
77async function enterFocusScope() {
78 const toggle = document.querySelector<HTMLElement>('[aria-label="Toggle right panel"]')!;
79 await act(async () => toggle.focus());
80 await act(() => userEvent.tab());
81 await act(nextFrame);
83​
84describe("right panel keyboard access", () => {
85 it("skips collapsed controls and restores access after reopening", async () => {
86 await renderPanel(false);
87 const outside = document.querySelector<HTMLElement>('[data-testid="outside"]')!;
88 expect(document.activeElement).toBe(outside);
89 await enterFocusScope();
90 expect(document.activeElement).toBe(outside);
91​
92 await renderPanel(true);
93 await enterFocusScope();
94 expect(document.activeElement).toBe(document.querySelector('[aria-label="Preview URL"]'));
95​
96 await renderPanel(false);
97 await enterFocusScope();
98 expect(document.activeElement).toBe(outside);
99 });
100​
101 it("returns focus to the right-panel toggle when a focused panel closes", async () => {
102 await renderPanel(true);
103 const input = document.querySelector<HTMLInputElement>('[aria-label="Preview URL"]')!;
104 await act(async () => input.focus());
105 await renderPanel(false);
106 expect(document.activeElement).toBe(
107 document.querySelector('[aria-label="Toggle right panel"]'),
108 );
109 await renderPanel(true);
110 await enterFocusScope();
111 expect(document.activeElement).toBe(input);
112 });
113​
114 it("retains focus after the closing animation relocates the toggle", async () => {
115 await renderPanel(true);
116 await act(async () => {
117 document.querySelector<HTMLInputElement>('[aria-label="Preview URL"]')!.focus();
118 });
119 await renderPanel(false);
120 const closingToggle = document.querySelector('[aria-label="Toggle right panel"]');
121 expect(document.activeElement).toBe(closingToggle);
122 await renderPanel(false, "inline", false);
123 const settledToggle = document.querySelector('[aria-label="Toggle right panel"]');
124 expect(settledToggle).not.toBe(closingToggle);
125 expect(document.activeElement).toBe(settledToggle);
126 });
127​
128 it("returns focus when the panel closes without an animation", async () => {
129 await renderPanel(true);
130 await act(async () => {
131 document.querySelector<HTMLInputElement>('[aria-label="Preview URL"]')!.focus();
132 });
133 await renderPanel(false, "inline", false);
134 expect(document.activeElement).toBe(
135 document.querySelector('[aria-label="Toggle right panel"]'),
136 );
137 });
138​
139 it("preserves focus moved outside while the closing animation settles", async () => {
140 await renderPanel(true);
141 await act(async () => {
142 document.querySelector<HTMLInputElement>('[aria-label="Preview URL"]')!.focus();
143 });
144 await renderPanel(false);
145 const outside = document.querySelector<HTMLButtonElement>('[data-testid="outside"]')!;
146 await act(async () => outside.focus());
147 await renderPanel(false, "inline", false);
148 expect(document.activeElement).toBe(outside);
149 });
150​
151 it("preserves focus outside the panel when it closes", async () => {
152 await renderPanel(true);
153 const outside = document.querySelector<HTMLButtonElement>('[data-testid="outside"]')!;
154 await act(async () => outside.focus());
155 await renderPanel(false);
156 expect(document.activeElement).toBe(outside);
157 });
158​
159 it.each(["sheet", "sidebar", "embedded"] as const)(
160 "keeps %s controls reachable when the inline open flag is false",
161 async (mode) => {
162 await renderPanel(false, mode);
163 expect(document.activeElement).toBe(document.querySelector('[aria-label="Preview URL"]'));
164 },
165 );
166​
167 it("keeps non-collapsible inline controls reachable", async () => {
168 await renderPanel(undefined);
169 expect(document.activeElement).toBe(document.querySelector('[aria-label="Preview URL"]'));
170 });
171});