| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- import { act, createElement, type ReactNode } from "react";
- import { createRoot, type Root } from "react-dom/client";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import type { WebRemoteControlTaskTarget } from "@zcode/shared";
- type TestEvent = {
- bubbles?: boolean;
- currentTarget?: TestElement;
- relatedTarget?: unknown;
- target?: TestElement;
- type: string;
- };
- type TestEventHandler = (event: TestEvent) => void;
- type TestElement = Element & {
- __attrs: Map<string, string>;
- __listeners: Map<string, TestEventHandler[]>;
- childNodes: TestElement[];
- dispatchEvent: (event: TestEvent) => boolean;
- parentNode: TestElement | null;
- };
- vi.mock("lucide-react", () => {
- const createIcon = (name: string) => (props: Record<string, unknown>) =>
- createElement("svg", { "data-icon": name, ...props });
- return {
- Archive: createIcon("archive"),
- ArchiveX: createIcon("archive-x"),
- CloudDownload: createIcon("cloud-download"),
- LoaderIcon: createIcon("loader"),
- Pin: createIcon("pin"),
- Trash2: createIcon("trash"),
- };
- });
- vi.mock("@/components/ui/button.js", () => ({
- Button: ({ children, ...props }: { children?: ReactNode }) =>
- createElement("button", props, children),
- }));
- vi.mock("@/ControlHintTooltip.js", () => ({
- ControlHintTooltip: ({ children }: { children?: ReactNode }) =>
- createElement("span", null, children),
- }));
- vi.mock("@/lib/taskListItemPresentation.js", () => ({
- formatTaskRelativeTime: () => "now",
- }));
- const stableIntl = {
- formatMessage: ({ id }: { id: string }) => id,
- };
- function createTask(overrides: Partial<WebRemoteControlTaskTarget> = {}): WebRemoteControlTaskTarget {
- return {
- createdAt: 1,
- taskId: "task-1",
- title: "Task 1",
- updatedAt: 2,
- workspaceKind: "local",
- workspaceLabel: "Project",
- workspacePath: "/workspace",
- ...overrides,
- };
- }
- function createMinimalElement(ownerDocument: Document, tagName = "div") {
- const element = {
- __attrs: new Map<string, string>(),
- __listeners: new Map<string, TestEventHandler[]>(),
- addEventListener: (type: string, handler: TestEventHandler) => {
- const handlers = element.__listeners.get(type) ?? [];
- handlers.push(handler);
- element.__listeners.set(type, handlers);
- },
- appendChild: (child: TestElement) => {
- child.parentNode = element as unknown as TestElement;
- element.childNodes.push(child);
- return child;
- },
- childNodes: [] as TestElement[],
- dispatchEvent: (event: TestEvent) => {
- event.target ??= element as unknown as TestElement;
- let current: TestElement | null = element as unknown as TestElement;
- while (current) {
- event.currentTarget = current;
- for (const handler of current.__listeners.get(event.type) ?? []) {
- handler(event);
- }
- if (!event.bubbles) {
- break;
- }
- current = current.parentNode;
- }
- return true;
- },
- getAttribute: (name: string) => element.__attrs.get(name) ?? null,
- insertBefore: (child: TestElement, beforeChild?: TestElement | null) => {
- child.parentNode = element as unknown as TestElement;
- const beforeIndex = beforeChild ? element.childNodes.indexOf(beforeChild) : -1;
- if (beforeIndex >= 0) {
- element.childNodes.splice(beforeIndex, 0, child);
- } else {
- element.childNodes.push(child);
- }
- return child;
- },
- nodeName: tagName.toUpperCase(),
- nodeType: 1,
- ownerDocument,
- parentNode: null as TestElement | null,
- querySelector: (selector: string) => {
- const attributeMatch = /^\[([^=\]]+)(?:=[^\]]+)?\]$/u.exec(selector);
- if (!attributeMatch) {
- return null;
- }
- return findByAttribute(element as unknown as TestElement, attributeMatch[1]);
- },
- removeAttribute: (name: string) => {
- element.__attrs.delete(name);
- },
- removeChild: (child: TestElement) => {
- element.childNodes = element.childNodes.filter((item) => item !== child);
- child.parentNode = null;
- return child;
- },
- removeEventListener: (type: string, handler: TestEventHandler) => {
- element.__listeners.set(
- type,
- (element.__listeners.get(type) ?? []).filter((item) => item !== handler),
- );
- },
- setAttribute: (name: string, value: string) => {
- element.__attrs.set(name, String(value));
- },
- style: {},
- tagName: tagName.toUpperCase(),
- };
- return element as unknown as TestElement;
- }
- function findByAttribute(node: TestElement, attributeName: string): TestElement | null {
- if (node.__attrs?.has(attributeName)) {
- return node;
- }
- for (const child of node.childNodes ?? []) {
- const match = findByAttribute(child, attributeName);
- if (match) {
- return match;
- }
- }
- return null;
- }
- function findByAttributeValue(
- node: TestElement,
- attributeName: string,
- attributeValue: string,
- ): TestElement | null {
- if (node.__attrs?.get(attributeName) === attributeValue) {
- return node;
- }
- for (const child of node.childNodes ?? []) {
- const match = findByAttributeValue(child, attributeName, attributeValue);
- if (match) {
- return match;
- }
- }
- return null;
- }
- function installMinimalDom() {
- const documentMock = {
- addEventListener: () => {},
- createElement: (tagName: string) =>
- createMinimalElement(documentMock as unknown as Document, tagName),
- createElementNS: (_namespace: string, tagName: string) =>
- createMinimalElement(documentMock as unknown as Document, tagName),
- createTextNode: (nodeValue: string) => ({
- nodeType: 3,
- nodeValue,
- ownerDocument: documentMock,
- parentNode: null,
- }),
- nodeType: 9,
- removeEventListener: () => {},
- } as unknown as Document;
- const windowMock = {
- addEventListener: () => {},
- document: documentMock,
- HTMLIFrameElement: function HTMLIFrameElement() {},
- HTMLElement: function HTMLElement() {},
- Node: function Node() {},
- removeEventListener: () => {},
- };
- Object.defineProperty(globalThis, "document", {
- configurable: true,
- value: documentMock,
- });
- Object.defineProperty(globalThis, "window", {
- configurable: true,
- value: windowMock,
- });
- Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
- configurable: true,
- value: true,
- });
- return createMinimalElement(documentMock);
- }
- describe("WebRemoteControlTaskIndexRow hover actions", () => {
- afterEach(() => {
- delete (globalThis as { document?: unknown }).document;
- delete (globalThis as { window?: unknown }).window;
- delete (globalThis as { IS_REACT_ACT_ENVIRONMENT?: unknown })
- .IS_REACT_ACT_ENVIRONMENT;
- });
- it("renders pin and archive actions from row hover state", async () => {
- const { WebRemoteControlTaskIndexRow } = await import("@/WebRemoteControlTaskIndexRow.js");
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- act(() => {
- root.render(
- createElement(WebRemoteControlTaskIndexRow, {
- activeTaskId: null,
- activeWorkspaceKey: "/workspace",
- canMutateTask: true,
- intl: stableIntl,
- mutatingTaskKey: null,
- onArchiveTask: vi.fn(),
- onDeleteArchivedTask: vi.fn(),
- onOpenTask: vi.fn(),
- onToggleTaskPin: vi.fn(),
- onUnarchiveTask: vi.fn(),
- pendingArchiveTaskKey: null,
- switchingTaskKey: null,
- task: createTask(),
- taskSortBy: "updated",
- }),
- );
- });
- expect(findByAttributeValue(container, "aria-label", "taskList.pin")).toBeNull();
- expect(findByAttributeValue(container, "aria-label", "taskList.archive")).toBeNull();
- const row = findByAttribute(container, "data-web-remote-task-row");
- expect(row).not.toBeNull();
- act(() => {
- row?.dispatchEvent({
- bubbles: true,
- relatedTarget: null,
- type: "mouseover",
- });
- });
- const pinButton = findByAttributeValue(container, "aria-label", "taskList.pin");
- expect(pinButton).not.toBeNull();
- expect(pinButton?.__attrs.get("class")).toContain("z-10");
- expect(pinButton?.__attrs.get("class")).toContain("inline-flex");
- expect(findByAttributeValue(container, "aria-label", "taskList.archive")).not.toBeNull();
- act(() => {
- row?.dispatchEvent({
- bubbles: true,
- relatedTarget: null,
- type: "mouseout",
- });
- });
- expect(findByAttributeValue(container, "aria-label", "taskList.pin")).toBeNull();
- expect(findByAttributeValue(container, "aria-label", "taskList.archive")).toBeNull();
- act(() => {
- root.unmount();
- });
- });
- it("renders unarchive and delete actions from archived row hover state", async () => {
- const { WebRemoteControlTaskIndexRow } = await import("@/WebRemoteControlTaskIndexRow.js");
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- act(() => {
- root.render(
- createElement(WebRemoteControlTaskIndexRow, {
- activeTaskId: null,
- activeWorkspaceKey: "/workspace",
- canMutateTask: true,
- intl: stableIntl,
- mutatingTaskKey: null,
- onArchiveTask: vi.fn(),
- onDeleteArchivedTask: vi.fn(),
- onOpenTask: vi.fn(),
- onToggleTaskPin: vi.fn(),
- onUnarchiveTask: vi.fn(),
- pendingArchiveTaskKey: null,
- switchingTaskKey: null,
- task: createTask({ archived: true, pinned: true }),
- taskSortBy: "updated",
- }),
- );
- });
- const row = findByAttribute(container, "data-web-remote-task-row");
- expect(row).not.toBeNull();
- act(() => {
- row?.dispatchEvent({
- bubbles: true,
- relatedTarget: null,
- type: "mouseover",
- });
- });
- expect(findByAttributeValue(container, "aria-label", "taskList.unarchive")).not.toBeNull();
- expect(findByAttributeValue(container, "aria-label", "taskList.delete")).not.toBeNull();
- expect(findByAttributeValue(container, "aria-label", "taskList.pin")).toBeNull();
- expect(findByAttributeValue(container, "aria-label", "taskList.unpin")).toBeNull();
- expect(findByAttributeValue(container, "aria-label", "taskList.archive")).toBeNull();
- act(() => {
- root.unmount();
- });
- });
- });
|