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