Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProjectScriptsControl.test.tsx

99 lines3.2 KB

1import type { ProjectScript, ResolvedKeybindingsConfig } from "@t3tools/contracts";
2import { renderToStaticMarkup } from "react-dom/server";
3import { describe, expect, it } from "vite-plus/test";
4​
5import ProjectScriptsControl from "./ProjectScriptsControl";
6​
7const EMPTY_KEYBINDINGS: ResolvedKeybindingsConfig = [];
8const PRIMARY_SCRIPT: ProjectScript = {
9 id: "dev",
10 name: "Dev",
11 command: "vp dev",
12 icon: "play",
13 runOnWorktreeCreate: false,
14};
15​
16function renderControl(scripts: ReadonlyArray<ProjectScript>) {
17 return renderToStaticMarkup(
18 <ProjectScriptsControl
19 scripts={scripts}
20 keybindings={EMPTY_KEYBINDINGS}
21 onRunScript={() => {}}
22 onAddScript={async () => undefined as never}
23 onUpdateScript={async () => undefined as never}
24 onDeleteScript={async () => undefined as never}
25 />,
26 );
28​
29function renderPanelControlWithImportedAction() {
30 return renderToStaticMarkup(
31 <ProjectScriptsControl
32 displayMode="panel"
33 scripts={[]}
34 fileScripts={[
35 {
36 name: "Setup Worktree",
37 command: "vp i",
38 icon: "configure",
39 runOnWorktreeCreate: true,
40 },
41 ]}
42 keybindings={EMPTY_KEYBINDINGS}
43 onRunScript={() => {}}
44 onAddScript={async () => undefined as never}
45 onUpdateScript={async () => undefined as never}
46 onDeleteScript={async () => undefined as never}
47 />,
48 );
50​
51function buttonTag(html: string, ariaLabel: string) {
52 return html.match(new RegExp(`<button[^>]*aria-label="${ariaLabel}"[^>]*>`))?.[0];
54​
55function expectResponsiveXsControl(markup: string | undefined) {
56 expect(markup).toBeDefined();
57 expect(markup).toContain("h-7");
58 expect(markup).toContain("gap-1");
59 expect(markup).toContain("text-sm");
60 expect(markup).toContain("sm:h-6");
61 expect(markup).toContain("sm:text-xs");
62 expect(markup).toContain("w-7");
63 expect(markup).toContain("px-0");
64 expect(markup).toContain("sm:w-6");
65 expect(markup).toContain("@3xl/header-actions:w-auto!");
66 expect(markup).toContain("@3xl/header-actions:px-[calc(--spacing(2)-1px)]");
68​
69describe("ProjectScriptsControl compact controls", () => {
70 it("keeps the primary Run control compact and expands it with its label", () => {
71 const html = renderControl([PRIMARY_SCRIPT]);
72​
73 expectResponsiveXsControl(buttonTag(html, "Run Dev"));
74 expect(html).toContain(
75 'class="sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5"',
76 );
77 });
78​
79 it("keeps the standalone Add control compact and expands it with its label", () => {
80 const html = renderControl([]);
81​
82 expectResponsiveXsControl(buttonTag(html, "Add action"));
83 expect(html).toContain(
84 'class="sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5"',
85 );
86 });
87​
88 it("renders available project actions as a full panel row", () => {
89 const html = renderPanelControlWithImportedAction();
90 const primary = buttonTag(html, "Project actions");
91 const menuTrigger = buttonTag(html, "Choose project action");
92​
93 expect(primary).toContain("flex-1");
94 expect(primary).toContain("justify-start");
95 expect(menuTrigger).toContain("w-8");
96 expect(html).toContain("h-4 w-px");
97 expect(html).toContain(">Actions</span>");
98 });
99});