webRemoteControlTaskIndexRowHoverActions.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import { act, createElement, type ReactNode } from "react";
  2. import { createRoot, type Root } from "react-dom/client";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import type { WebRemoteControlTaskTarget } from "@zcode/shared";
  5. type TestEvent = {
  6. bubbles?: boolean;
  7. currentTarget?: TestElement;
  8. relatedTarget?: unknown;
  9. target?: TestElement;
  10. type: string;
  11. };
  12. type TestEventHandler = (event: TestEvent) => void;
  13. type TestElement = Element & {
  14. __attrs: Map<string, string>;
  15. __listeners: Map<string, TestEventHandler[]>;
  16. childNodes: TestElement[];
  17. dispatchEvent: (event: TestEvent) => boolean;
  18. parentNode: TestElement | null;
  19. };
  20. vi.mock("lucide-react", () => {
  21. const createIcon = (name: string) => (props: Record<string, unknown>) =>
  22. createElement("svg", { "data-icon": name, ...props });
  23. return {
  24. Archive: createIcon("archive"),
  25. ArchiveX: createIcon("archive-x"),
  26. CloudDownload: createIcon("cloud-download"),
  27. LoaderIcon: createIcon("loader"),
  28. Pin: createIcon("pin"),
  29. Trash2: createIcon("trash"),
  30. };
  31. });
  32. vi.mock("@/components/ui/button.js", () => ({
  33. Button: ({ children, ...props }: { children?: ReactNode }) =>
  34. createElement("button", props, children),
  35. }));
  36. vi.mock("@/ControlHintTooltip.js", () => ({
  37. ControlHintTooltip: ({ children }: { children?: ReactNode }) =>
  38. createElement("span", null, children),
  39. }));
  40. vi.mock("@/lib/taskListItemPresentation.js", () => ({
  41. formatTaskRelativeTime: () => "now",
  42. }));
  43. const stableIntl = {
  44. formatMessage: ({ id }: { id: string }) => id,
  45. };
  46. function createTask(overrides: Partial<WebRemoteControlTaskTarget> = {}): WebRemoteControlTaskTarget {
  47. return {
  48. createdAt: 1,
  49. taskId: "task-1",
  50. title: "Task 1",
  51. updatedAt: 2,
  52. workspaceKind: "local",
  53. workspaceLabel: "Project",
  54. workspacePath: "/workspace",
  55. ...overrides,
  56. };
  57. }
  58. function createMinimalElement(ownerDocument: Document, tagName = "div") {
  59. const element = {
  60. __attrs: new Map<string, string>(),
  61. __listeners: new Map<string, TestEventHandler[]>(),
  62. addEventListener: (type: string, handler: TestEventHandler) => {
  63. const handlers = element.__listeners.get(type) ?? [];
  64. handlers.push(handler);
  65. element.__listeners.set(type, handlers);
  66. },
  67. appendChild: (child: TestElement) => {
  68. child.parentNode = element as unknown as TestElement;
  69. element.childNodes.push(child);
  70. return child;
  71. },
  72. childNodes: [] as TestElement[],
  73. dispatchEvent: (event: TestEvent) => {
  74. event.target ??= element as unknown as TestElement;
  75. let current: TestElement | null = element as unknown as TestElement;
  76. while (current) {
  77. event.currentTarget = current;
  78. for (const handler of current.__listeners.get(event.type) ?? []) {
  79. handler(event);
  80. }
  81. if (!event.bubbles) {
  82. break;
  83. }
  84. current = current.parentNode;
  85. }
  86. return true;
  87. },
  88. getAttribute: (name: string) => element.__attrs.get(name) ?? null,
  89. insertBefore: (child: TestElement, beforeChild?: TestElement | null) => {
  90. child.parentNode = element as unknown as TestElement;
  91. const beforeIndex = beforeChild ? element.childNodes.indexOf(beforeChild) : -1;
  92. if (beforeIndex >= 0) {
  93. element.childNodes.splice(beforeIndex, 0, child);
  94. } else {
  95. element.childNodes.push(child);
  96. }
  97. return child;
  98. },
  99. nodeName: tagName.toUpperCase(),
  100. nodeType: 1,
  101. ownerDocument,
  102. parentNode: null as TestElement | null,
  103. querySelector: (selector: string) => {
  104. const attributeMatch = /^\[([^=\]]+)(?:=[^\]]+)?\]$/u.exec(selector);
  105. if (!attributeMatch) {
  106. return null;
  107. }
  108. return findByAttribute(element as unknown as TestElement, attributeMatch[1]);
  109. },
  110. removeAttribute: (name: string) => {
  111. element.__attrs.delete(name);
  112. },
  113. removeChild: (child: TestElement) => {
  114. element.childNodes = element.childNodes.filter((item) => item !== child);
  115. child.parentNode = null;
  116. return child;
  117. },
  118. removeEventListener: (type: string, handler: TestEventHandler) => {
  119. element.__listeners.set(
  120. type,
  121. (element.__listeners.get(type) ?? []).filter((item) => item !== handler),
  122. );
  123. },
  124. setAttribute: (name: string, value: string) => {
  125. element.__attrs.set(name, String(value));
  126. },
  127. style: {},
  128. tagName: tagName.toUpperCase(),
  129. };
  130. return element as unknown as TestElement;
  131. }
  132. function findByAttribute(node: TestElement, attributeName: string): TestElement | null {
  133. if (node.__attrs?.has(attributeName)) {
  134. return node;
  135. }
  136. for (const child of node.childNodes ?? []) {
  137. const match = findByAttribute(child, attributeName);
  138. if (match) {
  139. return match;
  140. }
  141. }
  142. return null;
  143. }
  144. function findByAttributeValue(
  145. node: TestElement,
  146. attributeName: string,
  147. attributeValue: string,
  148. ): TestElement | null {
  149. if (node.__attrs?.get(attributeName) === attributeValue) {
  150. return node;
  151. }
  152. for (const child of node.childNodes ?? []) {
  153. const match = findByAttributeValue(child, attributeName, attributeValue);
  154. if (match) {
  155. return match;
  156. }
  157. }
  158. return null;
  159. }
  160. function installMinimalDom() {
  161. const documentMock = {
  162. addEventListener: () => {},
  163. createElement: (tagName: string) =>
  164. createMinimalElement(documentMock as unknown as Document, tagName),
  165. createElementNS: (_namespace: string, tagName: string) =>
  166. createMinimalElement(documentMock as unknown as Document, tagName),
  167. createTextNode: (nodeValue: string) => ({
  168. nodeType: 3,
  169. nodeValue,
  170. ownerDocument: documentMock,
  171. parentNode: null,
  172. }),
  173. nodeType: 9,
  174. removeEventListener: () => {},
  175. } as unknown as Document;
  176. const windowMock = {
  177. addEventListener: () => {},
  178. document: documentMock,
  179. HTMLIFrameElement: function HTMLIFrameElement() {},
  180. HTMLElement: function HTMLElement() {},
  181. Node: function Node() {},
  182. removeEventListener: () => {},
  183. };
  184. Object.defineProperty(globalThis, "document", {
  185. configurable: true,
  186. value: documentMock,
  187. });
  188. Object.defineProperty(globalThis, "window", {
  189. configurable: true,
  190. value: windowMock,
  191. });
  192. Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
  193. configurable: true,
  194. value: true,
  195. });
  196. return createMinimalElement(documentMock);
  197. }
  198. describe("WebRemoteControlTaskIndexRow hover actions", () => {
  199. afterEach(() => {
  200. delete (globalThis as { document?: unknown }).document;
  201. delete (globalThis as { window?: unknown }).window;
  202. delete (globalThis as { IS_REACT_ACT_ENVIRONMENT?: unknown })
  203. .IS_REACT_ACT_ENVIRONMENT;
  204. });
  205. it("renders pin and archive actions from row hover state", async () => {
  206. const { WebRemoteControlTaskIndexRow } = await import("@/WebRemoteControlTaskIndexRow.js");
  207. const container = installMinimalDom();
  208. const root: Root = createRoot(container);
  209. act(() => {
  210. root.render(
  211. createElement(WebRemoteControlTaskIndexRow, {
  212. activeTaskId: null,
  213. activeWorkspaceKey: "/workspace",
  214. canMutateTask: true,
  215. intl: stableIntl,
  216. mutatingTaskKey: null,
  217. onArchiveTask: vi.fn(),
  218. onDeleteArchivedTask: vi.fn(),
  219. onOpenTask: vi.fn(),
  220. onToggleTaskPin: vi.fn(),
  221. onUnarchiveTask: vi.fn(),
  222. pendingArchiveTaskKey: null,
  223. switchingTaskKey: null,
  224. task: createTask(),
  225. taskSortBy: "updated",
  226. }),
  227. );
  228. });
  229. expect(findByAttributeValue(container, "aria-label", "taskList.pin")).toBeNull();
  230. expect(findByAttributeValue(container, "aria-label", "taskList.archive")).toBeNull();
  231. const row = findByAttribute(container, "data-web-remote-task-row");
  232. expect(row).not.toBeNull();
  233. act(() => {
  234. row?.dispatchEvent({
  235. bubbles: true,
  236. relatedTarget: null,
  237. type: "mouseover",
  238. });
  239. });
  240. const pinButton = findByAttributeValue(container, "aria-label", "taskList.pin");
  241. expect(pinButton).not.toBeNull();
  242. expect(pinButton?.__attrs.get("class")).toContain("z-10");
  243. expect(pinButton?.__attrs.get("class")).toContain("inline-flex");
  244. expect(findByAttributeValue(container, "aria-label", "taskList.archive")).not.toBeNull();
  245. act(() => {
  246. row?.dispatchEvent({
  247. bubbles: true,
  248. relatedTarget: null,
  249. type: "mouseout",
  250. });
  251. });
  252. expect(findByAttributeValue(container, "aria-label", "taskList.pin")).toBeNull();
  253. expect(findByAttributeValue(container, "aria-label", "taskList.archive")).toBeNull();
  254. act(() => {
  255. root.unmount();
  256. });
  257. });
  258. it("renders unarchive and delete actions from archived row hover state", async () => {
  259. const { WebRemoteControlTaskIndexRow } = await import("@/WebRemoteControlTaskIndexRow.js");
  260. const container = installMinimalDom();
  261. const root: Root = createRoot(container);
  262. act(() => {
  263. root.render(
  264. createElement(WebRemoteControlTaskIndexRow, {
  265. activeTaskId: null,
  266. activeWorkspaceKey: "/workspace",
  267. canMutateTask: true,
  268. intl: stableIntl,
  269. mutatingTaskKey: null,
  270. onArchiveTask: vi.fn(),
  271. onDeleteArchivedTask: vi.fn(),
  272. onOpenTask: vi.fn(),
  273. onToggleTaskPin: vi.fn(),
  274. onUnarchiveTask: vi.fn(),
  275. pendingArchiveTaskKey: null,
  276. switchingTaskKey: null,
  277. task: createTask({ archived: true, pinned: true }),
  278. taskSortBy: "updated",
  279. }),
  280. );
  281. });
  282. const row = findByAttribute(container, "data-web-remote-task-row");
  283. expect(row).not.toBeNull();
  284. act(() => {
  285. row?.dispatchEvent({
  286. bubbles: true,
  287. relatedTarget: null,
  288. type: "mouseover",
  289. });
  290. });
  291. expect(findByAttributeValue(container, "aria-label", "taskList.unarchive")).not.toBeNull();
  292. expect(findByAttributeValue(container, "aria-label", "taskList.delete")).not.toBeNull();
  293. expect(findByAttributeValue(container, "aria-label", "taskList.pin")).toBeNull();
  294. expect(findByAttributeValue(container, "aria-label", "taskList.unpin")).toBeNull();
  295. expect(findByAttributeValue(container, "aria-label", "taskList.archive")).toBeNull();
  296. act(() => {
  297. root.unmount();
  298. });
  299. });
  300. });