|
| 1 | +// @vitest-environment jsdom |
| 2 | + |
| 3 | +import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 4 | +import { act, useRef, useState } 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 { useRightPanelStore } from "../../rightPanelStore"; |
| 9 | +import { PopoverCreateHandle } from "../ui/popover"; |
| 10 | +import { PanelLayoutControls } from "./PanelLayoutControls"; |
| 11 | +import { ThreadDetailsCard } from "./ThreadDetailsCard"; |
| 12 | + |
| 13 | +const threadRef = { |
| 14 | + environmentId: EnvironmentId.make("banner-dismissal"), |
| 15 | + threadId: ThreadId.make("thread"), |
| 16 | +}; |
| 17 | +const noop = () => {}; |
| 18 | +let root: Root; |
| 19 | +let container: HTMLDivElement; |
| 20 | + |
| 21 | +function Fixture() { |
| 22 | + const anchor = useRef<HTMLDivElement>(null); |
| 23 | + const errorBannersRef = useRef<HTMLDivElement>(null); |
| 24 | + const [handle] = useState(PopoverCreateHandle); |
| 25 | + const [error, setError] = useState(true); |
| 26 | + return ( |
| 27 | + <> |
| 28 | + <div ref={anchor}> |
| 29 | + <PanelLayoutControls |
| 30 | + terminalAvailable={false} |
| 31 | + terminalOpen={false} |
| 32 | + terminalShortcutLabel={null} |
| 33 | + threadPanelOpen={true} |
| 34 | + threadPanelPresentation="popover" |
| 35 | + threadPanelPopoverHandle={handle} |
| 36 | + errorBannersRef={errorBannersRef} |
| 37 | + threadPanelShortcutLabel={null} |
| 38 | + threadPanelHasAttention={false} |
| 39 | + rightPanelAvailable={false} |
| 40 | + rightPanelOpen={false} |
| 41 | + rightPanelShortcutLabel={null} |
| 42 | + onToggleTerminal={noop} |
| 43 | + onToggleThreadPanel={noop} |
| 44 | + onToggleRightPanel={noop} |
| 45 | + showTerminalControl={false} |
| 46 | + showRightPanelControl={false} |
| 47 | + /> |
| 48 | + </div> |
| 49 | + <div ref={errorBannersRef}> |
| 50 | + {error && ( |
| 51 | + <div role="alert"> |
| 52 | + <span>Provider unavailable</span> |
| 53 | + <button onClick={() => setError(false)} aria-label="Dismiss error"> |
| 54 | + <svg> |
| 55 | + <path /> |
| 56 | + </svg> |
| 57 | + </button> |
| 58 | + </div> |
| 59 | + )} |
| 60 | + </div> |
| 61 | + <button>Outside</button> |
| 62 | + <ThreadDetailsCard |
| 63 | + threadRef={threadRef} |
| 64 | + anchor={anchor} |
| 65 | + errorBannersRef={errorBannersRef} |
| 66 | + handle={handle} |
| 67 | + onPresentationChange={noop} |
| 68 | + > |
| 69 | + {() => <button>Project actions</button>} |
| 70 | + </ThreadDetailsCard> |
| 71 | + </> |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +beforeEach(async () => { |
| 76 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 77 | + Object.defineProperty(Element.prototype, "getAnimations", { |
| 78 | + configurable: true, |
| 79 | + value: () => [], |
| 80 | + }); |
| 81 | + vi.stubGlobal( |
| 82 | + "ResizeObserver", |
| 83 | + class { |
| 84 | + observe() {} |
| 85 | + unobserve() {} |
| 86 | + disconnect() {} |
| 87 | + }, |
| 88 | + ); |
| 89 | + useRightPanelStore.setState({ threadPanelVisibilityByThreadKey: {} }); |
| 90 | + container = document.createElement("div"); |
| 91 | + document.body.append(container); |
| 92 | + root = createRoot(container); |
| 93 | + await act(() => root.render(<Fixture />)); |
| 94 | + await click(button("Toggle thread details panel")); |
| 95 | + expect(panel()).not.toBeNull(); |
| 96 | +}); |
| 97 | + |
| 98 | +afterEach(async () => { |
| 99 | + await act(() => root.unmount()); |
| 100 | + container.remove(); |
| 101 | + Reflect.deleteProperty(Element.prototype, "getAnimations"); |
| 102 | + vi.unstubAllGlobals(); |
| 103 | +}); |
| 104 | + |
| 105 | +function panel() { |
| 106 | + return document.querySelector( |
| 107 | + '[data-slot="popover-popup"][data-open] [data-thread-details-panel="popover"]', |
| 108 | + ); |
| 109 | +} |
| 110 | + |
| 111 | +function button(label: string) { |
| 112 | + return Array.from(document.querySelectorAll("button")).find( |
| 113 | + (element) => (element.getAttribute("aria-label") ?? element.textContent) === label, |
| 114 | + )!; |
| 115 | +} |
| 116 | + |
| 117 | +async function click(element: Element) { |
| 118 | + await act(() => { |
| 119 | + const pointerDown = new MouseEvent("pointerdown", { bubbles: true }); |
| 120 | + Object.defineProperty(pointerDown, "pointerType", { value: "mouse" }); |
| 121 | + element.dispatchEvent(pointerDown); |
| 122 | + element.dispatchEvent(new MouseEvent("mousedown", { bubbles: true })); |
| 123 | + element.closest("button")?.focus(); |
| 124 | + element.dispatchEvent(new MouseEvent("mouseup", { bubbles: true })); |
| 125 | + element.dispatchEvent(new MouseEvent("pointerup", { bubbles: true })); |
| 126 | + element.dispatchEvent(new MouseEvent("click", { bubbles: true })); |
| 127 | + }); |
| 128 | +} |
| 129 | + |
| 130 | +describe("workspace panel and foreground errors", () => { |
| 131 | + it("dismisses the error through its icon without closing the panel", async () => { |
| 132 | + await click(button("Dismiss error").querySelector("path")!); |
| 133 | + expect(document.querySelector('[role="alert"]')).toBeNull(); |
| 134 | + expect(panel()).not.toBeNull(); |
| 135 | + }); |
| 136 | + |
| 137 | + it("keeps the panel open for banner text, but closes for other outside clicks", async () => { |
| 138 | + await click(document.querySelector('[role="alert"] span')!); |
| 139 | + expect(panel()).not.toBeNull(); |
| 140 | + await click(button("Outside")); |
| 141 | + expect(panel()).toBeNull(); |
| 142 | + }); |
| 143 | + |
| 144 | + it("allows focus to move to the error control without closing the panel", async () => { |
| 145 | + await act(() => button("Project actions").focus()); |
| 146 | + await act(() => button("Dismiss error").focus()); |
| 147 | + expect(document.activeElement).toBe(button("Dismiss error")); |
| 148 | + expect(panel()).not.toBeNull(); |
| 149 | + await act(() => button("Dismiss error").click()); |
| 150 | + expect(document.querySelector('[role="alert"]')).toBeNull(); |
| 151 | + expect(panel()).not.toBeNull(); |
| 152 | + }); |
| 153 | + |
| 154 | + it("still closes on Escape while an error control has focus", async () => { |
| 155 | + await act(() => button("Dismiss error").focus()); |
| 156 | + await act(() => |
| 157 | + document.activeElement!.dispatchEvent( |
| 158 | + new KeyboardEvent("keydown", { key: "Escape", bubbles: true }), |
| 159 | + ), |
| 160 | + ); |
| 161 | + expect(panel()).toBeNull(); |
| 162 | + expect(document.querySelector('[role="alert"]')).not.toBeNull(); |
| 163 | + }); |
| 164 | + |
| 165 | + it("returns focus to the workspace trigger on Escape after dismissing an error", async () => { |
| 166 | + await act(() => button("Project actions").focus()); |
| 167 | + await click(button("Dismiss error")); |
| 168 | + expect(panel()).not.toBeNull(); |
| 169 | + await act(() => button("Project actions").focus()); |
| 170 | + await act(() => |
| 171 | + document.activeElement!.dispatchEvent( |
| 172 | + new KeyboardEvent("keydown", { key: "Escape", bubbles: true }), |
| 173 | + ), |
| 174 | + ); |
| 175 | + await act(() => new Promise<void>((resolve) => requestAnimationFrame(() => resolve()))); |
| 176 | + expect(panel()).toBeNull(); |
| 177 | + expect(document.activeElement).toBe(button("Toggle thread details panel")); |
| 178 | + }); |
| 179 | + |
| 180 | + it("keeps the workspace open when the error is dismissed from trigger focus", async () => { |
| 181 | + await act(() => button("Toggle thread details panel").focus()); |
| 182 | + await click(button("Dismiss error")); |
| 183 | + expect(document.querySelector('[role="alert"]')).toBeNull(); |
| 184 | + expect(panel()).not.toBeNull(); |
| 185 | + }); |
| 186 | +}); |
0 commit comments