|
1 | 1 | // @vitest-environment happy-dom |
2 | 2 |
|
3 | | -// happy-dom has no CSS engine, so this pins the structure that makes the gap |
4 | | -// work (children are direct flex items of the gap-bearing button), not the |
5 | | -// computed pixels. The measured gap is verified in a real browser. |
| 3 | +/** |
| 4 | + * Button/IconButton: renders every variant and size, then compiles Studio's |
| 5 | + * real stylesheet against the emitted classes so an unstyled class fails. |
| 6 | + */ |
| 7 | +import { createRequire } from "node:module"; |
| 8 | +import { readFileSync } from "node:fs"; |
| 9 | +import path from "node:path"; |
| 10 | +import React, { act } from "react"; |
| 11 | +import { compile } from "tailwindcss"; |
| 12 | +import { afterEach, beforeAll, describe, expect, it } from "vitest"; |
| 13 | +import { Button, type ButtonSize, type ButtonVariant } from "./Button"; |
| 14 | +import { IconButton } from "./IconButton"; |
| 15 | +import { Tab, Tabs, TabsList } from "./Tabs"; |
| 16 | +import { cleanupMounted, mountHost } from "./mountHost.testHelpers"; |
| 17 | +import { isTypingTarget } from "../../utils/typingTarget"; |
| 18 | +import { shouldIgnorePlaybackShortcutTarget } from "../../player/lib/playbackShortcuts"; |
6 | 19 |
|
7 | | -import { act } from "react"; |
8 | | -import { createRoot, type Root } from "react-dom/client"; |
9 | | -import { afterEach, describe, expect, it } from "vitest"; |
10 | | -import { Button } from "./Button"; |
| 20 | +const VARIANTS: ButtonVariant[] = ["primary", "secondary", "danger", "ghost"]; |
| 21 | +const SIZES: ButtonSize[] = ["sm", "md", "lg"]; |
11 | 22 |
|
12 | | -Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true }); |
| 23 | +afterEach(cleanupMounted); |
13 | 24 |
|
14 | | -let root: Root | null = null; |
| 25 | +function classesOf(host: HTMLElement, selector = "button"): string[] { |
| 26 | + const el = host.querySelector(selector); |
| 27 | + if (!el) throw new Error(`nothing matched ${selector}`); |
| 28 | + return [...el.classList]; |
| 29 | +} |
| 30 | + |
| 31 | +// -- the compiled stylesheet, built once for the whole file -- |
15 | 32 |
|
16 | | -afterEach(() => { |
17 | | - if (root) act(() => root?.unmount()); |
18 | | - root = null; |
19 | | - document.body.innerHTML = ""; |
| 33 | +const STYLES_DIR = path.resolve(__dirname, "../../styles"); |
| 34 | +const require = createRequire(import.meta.url); |
| 35 | +const TAILWIND_DIR = path.dirname(require.resolve("tailwindcss/package.json")); |
| 36 | + |
| 37 | +async function loadStylesheet(id: string, base: string) { |
| 38 | + const file = id === "tailwindcss" ? path.join(TAILWIND_DIR, "index.css") : path.resolve(base, id); |
| 39 | + return { path: file, base: path.dirname(file), content: readFileSync(file, "utf8") }; |
| 40 | +} |
| 41 | + |
| 42 | +let compileStudioCss: (candidates: string[]) => string; |
| 43 | + |
| 44 | +beforeAll(async () => { |
| 45 | + const compiled = await compile(readFileSync(path.join(STYLES_DIR, "studio.css"), "utf8"), { |
| 46 | + base: STYLES_DIR, |
| 47 | + loadStylesheet, |
| 48 | + }); |
| 49 | + compileStudioCss = (candidates) => compiled.build(candidates); |
20 | 50 | }); |
21 | 51 |
|
22 | | -function mount(element: React.ReactNode): HTMLButtonElement { |
23 | | - const host = document.createElement("div"); |
24 | | - document.body.append(host); |
25 | | - root = createRoot(host); |
26 | | - act(() => root?.render(element)); |
27 | | - const button = host.querySelector("button"); |
28 | | - if (!button) throw new Error("button did not render"); |
29 | | - return button; |
| 52 | +function unresolved(candidates: string[]): string[] { |
| 53 | + const css = compileStudioCss(candidates); |
| 54 | + return candidates.filter((candidate) => !css.includes(CSS.escape(candidate))); |
30 | 55 | } |
31 | 56 |
|
32 | | -describe("Button children layout", () => { |
33 | | - it("lays label and kbd out as sibling flex items of the gap-bearing button", () => { |
34 | | - const button = mount( |
| 57 | +describe("Button classes", () => { |
| 58 | + it("emits only classes Studio's stylesheet defines", () => { |
| 59 | + const emitted = new Set<string>(); |
| 60 | + for (const variant of VARIANTS) { |
| 61 | + for (const size of SIZES) { |
| 62 | + const host = mountHost( |
| 63 | + <> |
| 64 | + <Button variant={variant} size={size}> |
| 65 | + Export |
| 66 | + </Button> |
| 67 | + <IconButton variant={variant} size={size} icon={null} aria-label="Zoom out" /> |
| 68 | + </>, |
| 69 | + ); |
| 70 | + for (const el of host.querySelectorAll("button")) { |
| 71 | + for (const cls of el.classList) emitted.add(cls); |
| 72 | + } |
| 73 | + cleanupMounted(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + expect(emitted.size).toBeGreaterThan(20); |
| 78 | + expect(unresolved([...emitted])).toEqual([]); |
| 79 | + }); |
| 80 | + |
| 81 | + it("fails an undefined class, so the check above is not vacuous", () => { |
| 82 | + // Names in a real namespace with no token behind them; the theme file now defines the |
| 83 | + // `rounded-button` pair this used to name. |
| 84 | + expect(unresolved(["bg-not-a-token", "text-step-999"])).toEqual([ |
| 85 | + "bg-not-a-token", |
| 86 | + "text-step-999", |
| 87 | + ]); |
| 88 | + }); |
| 89 | + |
| 90 | + it("lets a caller's type size beat the size's own", () => { |
| 91 | + // The Renders Export case: `size="md"` brings `text-step-12`, the caller |
| 92 | + // wants 11px, and with plain concatenation both survived into the class |
| 93 | + // list and the stylesheet's order decided the winner. |
| 94 | + const classes = classesOf(mountHost(<Button size="md" className="text-step-11" />)); |
| 95 | + |
| 96 | + expect(classes).toContain("text-step-11"); |
| 97 | + expect(classes).not.toContain("text-step-12"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("keeps every interactive look reachable without a pointer", () => { |
| 101 | + // `data-preview-state` exists so a gallery can show the hover, active and |
| 102 | + // focus looks. It is worth nothing if it drifts from the real state, so |
| 103 | + // each pair is matched here in both directions. |
| 104 | + const realPrefix = { |
| 105 | + hover: ["enabled:hover:", "hover:"], |
| 106 | + active: ["enabled:active:", "active:"], |
| 107 | + focus: ["focus-visible:"], |
| 108 | + }; |
| 109 | + |
| 110 | + for (const variant of VARIANTS) { |
| 111 | + const classes = classesOf(mountHost(<Button variant={variant}>Export</Button>)); |
| 112 | + cleanupMounted(); |
| 113 | + |
| 114 | + for (const [state, prefixes] of Object.entries(realPrefix)) { |
| 115 | + const previewPrefix = `data-[preview-state=${state}]:`; |
| 116 | + const preview = classes |
| 117 | + .filter((cls) => cls.startsWith(previewPrefix)) |
| 118 | + .map((cls) => cls.slice(previewPrefix.length)); |
| 119 | + const real = classes |
| 120 | + .filter((cls) => prefixes.some((prefix) => cls.startsWith(prefix))) |
| 121 | + .map((cls) => cls.slice(cls.lastIndexOf(":") + 1)); |
| 122 | + |
| 123 | + expect([...preview].sort(), `${variant} ${state}`).toEqual([...real].sort()); |
| 124 | + expect(preview.length, `${variant} ${state}`).toBeGreaterThan(0); |
| 125 | + } |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + it("uses a motion token that zeroes itself under reduced motion", () => { |
| 130 | + // AE4. The zero-duration case lives in the `duration-press` utility itself |
| 131 | + // (theme.css), not in a `motion-reduce:` class beside it, so a caller |
| 132 | + // cannot use the token and forget the reduced-motion half. The button's |
| 133 | + // job is to name the token; the stylesheet's job is the media query. |
| 134 | + expect(classesOf(mountHost(<Button>Export</Button>))).toContain("duration-press"); |
| 135 | + |
| 136 | + expect(compileStudioCss(["duration-press"])).toMatch( |
| 137 | + /\.duration-press \{[\s\S]*?prefers-reduced-motion: reduce[\s\S]*?transition-duration: 0ms/, |
| 138 | + ); |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
| 142 | +describe("Button behaviour", () => { |
| 143 | + it("does not fire a disabled click, and says it is disabled", () => { |
| 144 | + let clicks = 0; |
| 145 | + const host = mountHost( |
| 146 | + <Button disabled onClick={() => (clicks += 1)}> |
| 147 | + Export |
| 148 | + </Button>, |
| 149 | + ); |
| 150 | + const button = host.querySelector("button")!; |
| 151 | + |
| 152 | + act(() => button.click()); |
| 153 | + |
| 154 | + expect(clicks).toBe(0); |
| 155 | + expect(button.getAttribute("aria-disabled")).toBe("true"); |
| 156 | + }); |
| 157 | + |
| 158 | + it("still fires a click while a preview state is forced", () => { |
| 159 | + let clicks = 0; |
| 160 | + const host = mountHost( |
| 161 | + <Button data-preview-state="hover" onClick={() => (clicks += 1)}> |
| 162 | + Export |
| 163 | + </Button>, |
| 164 | + ); |
| 165 | + const button = host.querySelector("button")!; |
| 166 | + |
| 167 | + act(() => button.click()); |
| 168 | + |
| 169 | + expect(clicks).toBe(1); |
| 170 | + expect(button.getAttribute("data-preview-state")).toBe("hover"); |
| 171 | + }); |
| 172 | + |
| 173 | + it("is classified by the hotkey selectors the way a plain button is", () => { |
| 174 | + // KTD13. Both selector lists gate on roles and element names. A primitive |
| 175 | + // that landed on a different role would leak or swallow global hotkeys with |
| 176 | + // nothing to notice it. |
| 177 | + const host = mountHost( |
| 178 | + <> |
| 179 | + <button type="button" data-testid="plain"> |
| 180 | + Export |
| 181 | + </button> |
| 182 | + <Button data-testid="primitive">Export</Button> |
| 183 | + <Tabs defaultValue="code"> |
| 184 | + <TabsList aria-label="Sidebar panels"> |
| 185 | + <Tab value="code">Code</Tab> |
| 186 | + </TabsList> |
| 187 | + </Tabs> |
| 188 | + </>, |
| 189 | + ); |
| 190 | + const plain = host.querySelector('[data-testid="plain"]')!; |
| 191 | + const button = host.querySelector('[data-testid="primitive"]')!; |
| 192 | + const tab = host.querySelector('[role="tab"]')!; |
| 193 | + |
| 194 | + for (const el of [plain, button, tab]) { |
| 195 | + expect(isTypingTarget(el)).toBe(false); |
| 196 | + expect(shouldIgnorePlaybackShortcutTarget(el)).toBe(true); |
| 197 | + } |
| 198 | + }); |
| 199 | +}); |
| 200 | + |
| 201 | +describe("disabled states", () => { |
| 202 | + it("disables a loading Button", () => { |
| 203 | + const host = mountHost(<Button loading>Save</Button>); |
| 204 | + const button = host.querySelector("button"); |
| 205 | + |
| 206 | + expect(button?.disabled).toBe(true); |
| 207 | + expect(button?.getAttribute("aria-disabled")).toBe("true"); |
| 208 | + }); |
| 209 | + |
| 210 | + it("marks a disabled IconButton for assistive tech", () => { |
| 211 | + const host = mountHost(<IconButton aria-label="Undo" icon={<svg />} disabled />); |
| 212 | + const button = host.querySelector("button"); |
| 213 | + |
| 214 | + expect(button?.disabled).toBe(true); |
| 215 | + expect(button?.getAttribute("aria-disabled")).toBe("true"); |
| 216 | + }); |
| 217 | +}); |
| 218 | + |
| 219 | +describe("children layout", () => { |
| 220 | + it("keeps label and shortcut direct flex items of the gap-bearing button without an icon", () => { |
| 221 | + const host = mountHost( |
35 | 222 | <Button size="md"> |
36 | 223 | <span>Export</span> |
37 | 224 | <kbd>⌘E</kbd> |
38 | 225 | </Button>, |
39 | 226 | ); |
| 227 | + const button = host.querySelector("button"); |
40 | 228 |
|
41 | | - expect(button.classList.contains("inline-flex")).toBe(true); |
42 | | - expect(button.classList.contains("gap-1.5")).toBe(true); |
43 | | - expect(Array.from(button.children).map((child) => child.tagName)).toEqual(["SPAN", "KBD"]); |
| 229 | + expect(button?.classList.contains("inline-flex")).toBe(true); |
| 230 | + expect(button?.classList.contains("gap-1.5")).toBe(true); |
| 231 | + expect(Array.from(button?.children ?? []).map((child) => child.tagName)).toEqual([ |
| 232 | + "SPAN", |
| 233 | + "KBD", |
| 234 | + ]); |
44 | 235 | }); |
45 | 236 |
|
46 | | - it("puts children next to the icon as direct flex items too", () => { |
47 | | - const button = mount( |
| 237 | + it("makes label and shortcut direct flex items so the button gap applies between them", () => { |
| 238 | + const host = mountHost( |
48 | 239 | <Button icon={<svg />}> |
49 | 240 | <span>Export</span> |
50 | 241 | <kbd>⌘E</kbd> |
51 | 242 | </Button>, |
52 | 243 | ); |
| 244 | + const button = host.querySelector("button"); |
53 | 245 |
|
54 | | - expect(Array.from(button.children).map((child) => child.tagName)).toEqual([ |
| 246 | + expect(Array.from(button?.children ?? []).map((child) => child.tagName)).toEqual([ |
55 | 247 | "SPAN", |
56 | 248 | "SPAN", |
57 | 249 | "KBD", |
|
0 commit comments