|
| 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 { PreviewPanelShell, type PreviewPanelMode } from "./PreviewPanelShell"; |
| 9 | + |
| 10 | +let root: Root; |
| 11 | +let container: HTMLDivElement; |
| 12 | + |
| 13 | +beforeEach(() => { |
| 14 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 15 | + vi.useFakeTimers({ toFake: ["requestAnimationFrame", "cancelAnimationFrame"] }); |
| 16 | + vi.stubGlobal( |
| 17 | + "ResizeObserver", |
| 18 | + class { |
| 19 | + observe() {} |
| 20 | + unobserve() {} |
| 21 | + disconnect() {} |
| 22 | + }, |
| 23 | + ); |
| 24 | + container = document.createElement("div"); |
| 25 | + document.body.append(container); |
| 26 | + root = createRoot(container); |
| 27 | +}); |
| 28 | + |
| 29 | +afterEach(async () => { |
| 30 | + await act(async () => root.unmount()); |
| 31 | + container.remove(); |
| 32 | + vi.unstubAllGlobals(); |
| 33 | + vi.useRealTimers(); |
| 34 | +}); |
| 35 | + |
| 36 | +async function renderPanel(open: boolean | undefined, mode: PreviewPanelMode = "inline") { |
| 37 | + await act(async () => { |
| 38 | + root.render( |
| 39 | + <Dialog.Root open> |
| 40 | + <Dialog.Portal> |
| 41 | + <Dialog.Popup aria-label="Workspace" finalFocus={false}> |
| 42 | + <PreviewPanelShell mode={mode} {...(open === undefined ? {} : { open })}> |
| 43 | + <input aria-label="Preview URL" /> |
| 44 | + <button type="button">Reload preview</button> |
| 45 | + </PreviewPanelShell> |
| 46 | + <button type="button" data-testid="outside"> |
| 47 | + Send message |
| 48 | + </button> |
| 49 | + </Dialog.Popup> |
| 50 | + </Dialog.Portal> |
| 51 | + </Dialog.Root>, |
| 52 | + ); |
| 53 | + }); |
| 54 | + await act(async () => vi.advanceTimersToNextFrame()); |
| 55 | +} |
| 56 | + |
| 57 | +async function enterFocusScope() { |
| 58 | + // Base UI's focus guard models Tab wrapping into the scope. Its production |
| 59 | + // focus manager chooses the first tabbable descendant, including inert rules |
| 60 | + // that jsdom's native focus() does not implement. |
| 61 | + const guards = document.querySelectorAll<HTMLElement>("[data-base-ui-focus-guard]"); |
| 62 | + expect(guards.length).toBe(2); |
| 63 | + await act(async () => guards[1]!.focus()); |
| 64 | + await act(async () => vi.advanceTimersToNextFrame()); |
| 65 | +} |
| 66 | + |
| 67 | +describe("right panel keyboard access", () => { |
| 68 | + it("skips collapsed controls and restores access after reopening", async () => { |
| 69 | + await renderPanel(false); |
| 70 | + const outside = document.querySelector<HTMLElement>('[data-testid="outside"]')!; |
| 71 | + expect(document.activeElement).toBe(outside); |
| 72 | + await enterFocusScope(); |
| 73 | + expect(document.activeElement).toBe(outside); |
| 74 | + |
| 75 | + await renderPanel(true); |
| 76 | + await enterFocusScope(); |
| 77 | + expect(document.activeElement).toBe(document.querySelector('[aria-label="Preview URL"]')); |
| 78 | + |
| 79 | + await renderPanel(false); |
| 80 | + await enterFocusScope(); |
| 81 | + expect(document.activeElement).toBe(outside); |
| 82 | + }); |
| 83 | + |
| 84 | + it.each(["sheet", "sidebar", "embedded"] as const)( |
| 85 | + "keeps %s controls reachable when the inline open flag is false", |
| 86 | + async (mode) => { |
| 87 | + await renderPanel(false, mode); |
| 88 | + expect(document.activeElement).toBe(document.querySelector('[aria-label="Preview URL"]')); |
| 89 | + }, |
| 90 | + ); |
| 91 | + |
| 92 | + it("keeps non-collapsible inline controls reachable", async () => { |
| 93 | + await renderPanel(undefined); |
| 94 | + expect(document.activeElement).toBe(document.querySelector('[aria-label="Preview URL"]')); |
| 95 | + }); |
| 96 | +}); |
0 commit comments