|
| 1 | +import { Suspense } from "react"; |
| 2 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 3 | +import { userEvent } from "vite-plus/test/browser"; |
| 4 | + |
| 5 | +import { render } from "../../../test/render"; |
| 6 | +import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog"; |
| 7 | +import { ThemeEditorPanel } from "./ThemeEditorPanel"; |
| 8 | + |
| 9 | +// The modal Settings dialog marks everything outside it inert in this harness, so the |
| 10 | +// test drives the editor through the DOM instead of pointer actions. |
| 11 | +async function editorElement<T extends HTMLElement>(selector: string): Promise<T> { |
| 12 | + await expect |
| 13 | + .poll(() => document.querySelector(`[data-theme-editor-panel] ${selector}`)) |
| 14 | + .toBeTruthy(); |
| 15 | + return document.querySelector<T>(`[data-theme-editor-panel] ${selector}`)!; |
| 16 | +} |
| 17 | + |
| 18 | +// The editor floats beside the Settings dialog, whose Esc handler sits on the document. |
| 19 | +function SettingsWithEditor(props: { |
| 20 | + onSettingsOpenChange: (open: boolean) => void; |
| 21 | + onEditorOpenChange: (open: boolean) => void; |
| 22 | +}) { |
| 23 | + return ( |
| 24 | + <> |
| 25 | + <Dialog onOpenChange={props.onSettingsOpenChange} open> |
| 26 | + <DialogPopup> |
| 27 | + <DialogTitle>Settings</DialogTitle> |
| 28 | + </DialogPopup> |
| 29 | + </Dialog> |
| 30 | + <Suspense fallback={null}> |
| 31 | + <ThemeEditorPanel |
| 32 | + editingTheme={null} |
| 33 | + initialAppearance="light" |
| 34 | + onOpenChange={props.onEditorOpenChange} |
| 35 | + onSaved={() => true} |
| 36 | + open |
| 37 | + restoreTheme={() => undefined} |
| 38 | + /> |
| 39 | + </Suspense> |
| 40 | + </> |
| 41 | + ); |
| 42 | +} |
| 43 | + |
| 44 | +describe("Esc in the theme editor", () => { |
| 45 | + it("closes the editor, not Settings", async () => { |
| 46 | + const onSettingsOpenChange = vi.fn(); |
| 47 | + const onEditorOpenChange = vi.fn(); |
| 48 | + await using _screen = await render( |
| 49 | + <SettingsWithEditor |
| 50 | + onEditorOpenChange={onEditorOpenChange} |
| 51 | + onSettingsOpenChange={onSettingsOpenChange} |
| 52 | + />, |
| 53 | + ); |
| 54 | + |
| 55 | + (await editorElement<HTMLInputElement>("input")).focus(); |
| 56 | + await userEvent.keyboard("{Escape}"); |
| 57 | + |
| 58 | + expect(onEditorOpenChange).toHaveBeenCalledWith(false); |
| 59 | + expect(onSettingsOpenChange).not.toHaveBeenCalled(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("cancels inspecting only", async () => { |
| 63 | + const onSettingsOpenChange = vi.fn(); |
| 64 | + const onEditorOpenChange = vi.fn(); |
| 65 | + await using _screen = await render( |
| 66 | + <SettingsWithEditor |
| 67 | + onEditorOpenChange={onEditorOpenChange} |
| 68 | + onSettingsOpenChange={onSettingsOpenChange} |
| 69 | + />, |
| 70 | + ); |
| 71 | + |
| 72 | + (await editorElement('[aria-label="Inspect app colors"]')).click(); |
| 73 | + await editorElement('[aria-label="Cancel inspecting app colors"]'); |
| 74 | + await userEvent.keyboard("{Escape}"); |
| 75 | + |
| 76 | + await editorElement('[aria-label="Inspect app colors"]'); |
| 77 | + expect(onEditorOpenChange).not.toHaveBeenCalled(); |
| 78 | + expect(onSettingsOpenChange).not.toHaveBeenCalled(); |
| 79 | + }); |
| 80 | +}); |
0 commit comments