sortableWorkspaceSidebarRenderStability.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { act, createElement } from "react";
  2. import { createRoot, type Root } from "react-dom/client";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import type { WorkspaceTabState } from "@/store/tabStore.js";
  5. const { childCalls, sortableRefs } = vi.hoisted(() => ({
  6. childCalls: [] as Array<Record<string, unknown>>,
  7. sortableRefs: {
  8. attributes: { role: "button" },
  9. listeners: { onPointerDown: () => {} },
  10. setNodeRef: () => {},
  11. },
  12. }));
  13. vi.mock("@dnd-kit/sortable", () => ({
  14. useSortable: () => ({
  15. attributes: sortableRefs.attributes,
  16. isDragging: false,
  17. listeners: sortableRefs.listeners,
  18. setNodeRef: sortableRefs.setNodeRef,
  19. transform: {
  20. scaleX: 0.5,
  21. scaleY: 0.75,
  22. x: 10,
  23. y: 20,
  24. },
  25. transition: "transform 120ms ease",
  26. }),
  27. }));
  28. vi.mock("@dnd-kit/utilities", () => ({
  29. CSS: {
  30. Transform: {
  31. toString: (
  32. transform: {
  33. scaleX?: number;
  34. scaleY?: number;
  35. x: number;
  36. y: number;
  37. } | null,
  38. ) =>
  39. transform
  40. ? `${transform.x},${transform.y},${transform.scaleX},${transform.scaleY}`
  41. : undefined,
  42. },
  43. },
  44. }));
  45. vi.mock("@/WorkspaceSidebarItem.js", () => ({
  46. WorkspaceSidebarItem: (props: Record<string, unknown>) => {
  47. childCalls.push(props);
  48. return null;
  49. },
  50. }));
  51. function createMinimalElement(ownerDocument: Document, tagName = "div") {
  52. const element = {
  53. addEventListener: () => {},
  54. appendChild: (child: { parentNode?: unknown }) => {
  55. child.parentNode = element;
  56. return child;
  57. },
  58. childNodes: [] as unknown[],
  59. getAttribute: () => null,
  60. insertBefore: (child: { parentNode?: unknown }) => {
  61. child.parentNode = element;
  62. return child;
  63. },
  64. nodeName: tagName.toUpperCase(),
  65. nodeType: 1,
  66. ownerDocument,
  67. parentNode: null as unknown,
  68. removeAttribute: () => {},
  69. removeChild: (child: { parentNode?: unknown }) => {
  70. child.parentNode = null;
  71. return child;
  72. },
  73. removeEventListener: () => {},
  74. setAttribute: () => {},
  75. style: {},
  76. tagName: tagName.toUpperCase(),
  77. };
  78. return element as unknown as Element;
  79. }
  80. function installMinimalDom() {
  81. const documentMock = {
  82. addEventListener: () => {},
  83. createElement: (tagName: string) =>
  84. createMinimalElement(documentMock as unknown as Document, tagName),
  85. createTextNode: (nodeValue: string) => ({
  86. nodeType: 3,
  87. nodeValue,
  88. ownerDocument: documentMock,
  89. parentNode: null,
  90. }),
  91. nodeType: 9,
  92. removeEventListener: () => {},
  93. } as unknown as Document;
  94. const windowMock = {
  95. addEventListener: () => {},
  96. document: documentMock,
  97. HTMLIFrameElement: function HTMLIFrameElement() {},
  98. HTMLElement: function HTMLElement() {},
  99. Node: function Node() {},
  100. removeEventListener: () => {},
  101. };
  102. Object.defineProperty(globalThis, "document", {
  103. configurable: true,
  104. value: documentMock,
  105. });
  106. Object.defineProperty(globalThis, "window", {
  107. configurable: true,
  108. value: windowMock,
  109. });
  110. Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
  111. configurable: true,
  112. value: true,
  113. });
  114. return createMinimalElement(documentMock);
  115. }
  116. function createWorkspaceTab(): WorkspaceTabState {
  117. return {
  118. id: "workspace-1",
  119. kind: "workspace",
  120. label: "workspace",
  121. workspacePath: "/workspace",
  122. };
  123. }
  124. describe("SortableWorkspaceSidebarItem render stability", () => {
  125. afterEach(() => {
  126. childCalls.length = 0;
  127. delete (globalThis as { document?: unknown }).document;
  128. delete (globalThis as { window?: unknown }).window;
  129. delete (globalThis as { IS_REACT_ACT_ENVIRONMENT?: unknown }).IS_REACT_ACT_ENVIRONMENT;
  130. });
  131. it("keeps child style, sortable bindings, and show-more handler stable across equivalent drag state", async () => {
  132. const { SortableWorkspaceSidebarItem } = await import("@/SortableWorkspaceSidebar.js");
  133. const root: Root = createRoot(installMinimalDom());
  134. const tab = createWorkspaceTab();
  135. const onShowMoreWorkspaceTasks = vi.fn();
  136. function renderWithLoading(taskListLoading: boolean) {
  137. act(() => {
  138. root.render(
  139. createElement(SortableWorkspaceSidebarItem, {
  140. activateTab: () => {},
  141. closeTab: () => {},
  142. isActiveWorkspace: true,
  143. isExpanded: true,
  144. mobileActiveTaskKey: null,
  145. onOpenFileTree: () => {},
  146. onReconnectRemoteWorkspace: async () => {},
  147. onSelectTask: () => {},
  148. onShowMoreWorkspaceTasks,
  149. reconnectingRemoteWorkspaceKeys: [],
  150. reconnectingRemoteWorkspaceLogsByWorkspaceKey: {},
  151. remoteWorkspaceErrorByWorkspaceKey: {},
  152. tab,
  153. taskItems: [],
  154. taskListHasMore: true,
  155. taskListLoading,
  156. toggleWorkspaceExpanded: () => {},
  157. workspaceKey: "workspace-key",
  158. }),
  159. );
  160. });
  161. }
  162. renderWithLoading(false);
  163. renderWithLoading(true);
  164. expect(childCalls).toHaveLength(2);
  165. const [firstProps, secondProps] = childCalls;
  166. expect(secondProps.itemStyle).toBe(firstProps.itemStyle);
  167. expect(secondProps.sortableBindings).toBe(firstProps.sortableBindings);
  168. expect(secondProps.onShowMoreTasks).toBe(firstProps.onShowMoreTasks);
  169. expect(typeof firstProps.onShowMoreTasks).toBe("function");
  170. (firstProps.onShowMoreTasks as () => void)();
  171. expect(onShowMoreWorkspaceTasks).toHaveBeenCalledWith("workspace-key");
  172. act(() => {
  173. root.unmount();
  174. });
  175. });
  176. });