Open History to see the last commit
1import { act } from "react";
2import { createRoot, type Root } from "react-dom/client";
3import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
4import { userEvent } from "vite-plus/test/browser";
5
6import { deriveProjectIdentity } from "../../projectIdentity";
7import { ProjectIconPickerDialog } from "./ProjectIconPickerDialog";
8
9let root: Root;
10let container: HTMLDivElement;
11
12beforeEach(() => {
13 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
14 container = document.createElement("div");
15 document.body.append(container);
16 root = createRoot(container);
17});
18
19afterEach(async () => {
20 await act(async () => root.unmount());
21 container.remove();
22 vi.unstubAllGlobals();
23});
24
25function button(name: string) {
26 return vi.waitFor(() => {
27 const match = [...document.querySelectorAll<HTMLElement>("button, [role='radio']")].find(
28 (element) => element.getAttribute("aria-label") === name || element.textContent === name,
29 );
30 if (!match) throw new Error(`No button ${name}`);
31 return match;
32 });
33}
34
35// The modal's inert backdrop covers the unstyled popup, so Playwright pointer clicks land on it.
36async function click(name: string) {
37 const target = await button(name);
38 await act(async () => target.click());
39}
40
41async function openPicker() {
42 const onSelect = vi.fn();
43 const onOpenChange = vi.fn();
44 await act(async () =>
45 root.render(
46 <ProjectIconPickerDialog
47 current={null}
48 projectName="Test"
49 open
50 onOpenChange={onOpenChange}
51 onSelect={onSelect}
52 />,
53 ),
54 );
55 return { onSelect, onOpenChange };
56}
57
58describe("ProjectIconPickerDialog", () => {
59 it("starts an automatic project on icons in its derived color and saves the picked icon", async () => {
60 const { onSelect, onOpenChange } = await openPicker();
61 const { color } = deriveProjectIdentity("Test");
62 expect((await button("Icons")).getAttribute("aria-pressed")).toBe("true");
63 expect(
64 document.querySelector('[aria-label="Icon color"] [aria-pressed="true"]'),
65 ).not.toBeNull();
66
67 await userEvent.fill(searchBox(), "rocket");
68 await click("Rocket");
69 await click("Save icon");
70
71 expect(onSelect).toHaveBeenCalledExactlyOnceWith({ kind: "lucide", name: "rocket", color });
72 expect(onOpenChange).toHaveBeenCalledWith(false);
73 });
74
75 it("saves a monogram only once the letters are letters or numbers", async () => {
76 const { onSelect } = await openPicker();
77 await click("Monogram");
78 const letters = await vi.waitFor(() => {
79 const input = document.querySelector<HTMLInputElement>("#project-monogram");
80 if (!input) throw new Error("No monogram input");
81 return input;
82 });
83
84 await userEvent.fill(letters, "t-3");
85 await click("Save icon");
86 expect(onSelect).not.toHaveBeenCalled();
87
88 await userEvent.fill(letters, "t3");
89 await click("Save icon");
90 expect(onSelect).toHaveBeenCalledExactlyOnceWith({
91 kind: "monogram",
92 text: "T3",
93 color: deriveProjectIdentity("Test").color,
94 });
95 });
96});
97
98function searchBox() {
99 return document.querySelector<HTMLInputElement>('input[aria-label="Search Lucide icons"]')!;
100}