archiveManagementUi.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { cleanup, fireEvent, render, screen } from "@testing-library/react";
  4. import { afterEach, expect, it, vi } from "vitest";
  5. import { ArchiveGroupCard } from "@/project-sidebar/ArchiveGroupCard.js";
  6. import { buildArchiveGroups, buildArchiveProjectOptions } from "@/project-sidebar/archive-view.js";
  7. import type { ProjectTree } from "@/project-sidebar/contract.js";
  8. import zhCN from "@/i18n/locales/zh-CN.js";
  9. import enUS from "@/i18n/locales/en-US.js";
  10. const mock = vi.hoisted(() => ({ locale: "zh-CN" }));
  11. vi.mock("@/i18n/IntlProvider.js", () => ({
  12. useZCodeIntl: () => ({
  13. locale: mock.locale,
  14. intl: {
  15. formatMessage: ({ id }: { id: string }, values: Record<string, number | string> = {}) =>
  16. Object.entries(values).reduce(
  17. (s, [k, v]) => s.replaceAll(`{${k}}`, String(v)),
  18. (mock.locale === "zh-CN" ? zhCN : enUS)[id] ?? id,
  19. ),
  20. },
  21. }),
  22. }));
  23. afterEach(cleanup);
  24. it.each(["zh-CN", "en-US"])(
  25. "mounts archived rows only while expanded and preserves pagination: %s",
  26. (locale) => {
  27. mock.locale = locale;
  28. const tree: ProjectTree = {
  29. projects: [],
  30. tasks: Array.from({ length: 51 }, (_, i) => ({
  31. taskId: `t${i}`,
  32. title: `Long title ${i} ` + "x".repeat(100),
  33. traceId: `t${i}`,
  34. workspacePath: "/root",
  35. projectId: null,
  36. hostId: "local",
  37. archived: true,
  38. pinned: false,
  39. mode: "default",
  40. createdAt: 1,
  41. updatedAt: 2,
  42. })),
  43. };
  44. const onAction = vi.fn(async () => {});
  45. const group = buildArchiveGroups(tree)[0]!;
  46. render(
  47. createElement(ArchiveGroupCard, {
  48. group,
  49. name: "Virtual title",
  50. sort: "updated",
  51. disabled: false,
  52. onAction,
  53. }),
  54. );
  55. expect(screen.queryAllByTestId("archive-task")).toHaveLength(0);
  56. expect(screen.queryByTestId("archive-show-more")).toBeNull();
  57. const toggle = screen.getByTestId("archive-group-toggle");
  58. expect(toggle.getAttribute("aria-expanded")).toBe("false");
  59. expect(screen.getByTestId("archive-group-menu")).toBeTruthy();
  60. expect(screen.getByTestId("archive-group").textContent).toContain("51");
  61. fireEvent.click(toggle);
  62. expect(toggle.getAttribute("aria-expanded")).toBe("true");
  63. expect(screen.getAllByTestId("archive-task")).toHaveLength(50);
  64. expect(screen.getAllByTestId("archive-unarchive")).toHaveLength(50);
  65. fireEvent.click(screen.getByTestId("archive-show-more"));
  66. expect(screen.getAllByTestId("archive-task")).toHaveLength(51);
  67. fireEvent.click(screen.getAllByTestId("archive-unarchive")[0]!);
  68. expect(onAction).toHaveBeenCalledWith([group.tasks[0]], "unarchive");
  69. expect(screen.getAllByTitle(/^Long title/)[0]!.textContent).toBe(group.tasks[0]!.title);
  70. fireEvent.click(toggle);
  71. expect(toggle.getAttribute("aria-expanded")).toBe("false");
  72. expect(screen.queryAllByTestId("archive-task")).toHaveLength(0);
  73. expect(screen.queryAllByTestId("archive-unarchive")).toHaveLength(0);
  74. expect(screen.queryByTestId("archive-show-more")).toBeNull();
  75. fireEvent.click(toggle);
  76. expect(screen.getAllByTestId("archive-task")).toHaveLength(51);
  77. },
  78. );
  79. it("keeps project options with zero archived rows, including same roots under different IDs", () => {
  80. const project = {
  81. hostId: "local",
  82. name: "virtual",
  83. roots: [{ rootPath: "/shared", isPrimary: true, position: 0 }],
  84. deleted: false,
  85. pinned: false,
  86. position: 0,
  87. createdAt: 1,
  88. updatedAt: 1,
  89. nameOverridden: true,
  90. };
  91. const options = buildArchiveProjectOptions({
  92. projects: [
  93. { ...project, id: "p1" },
  94. { ...project, id: "p2" },
  95. ],
  96. tasks: [],
  97. });
  98. expect(options.map((p) => p.project?.id)).toEqual(["p1", "p2"]);
  99. });