projectSidebarExpansion.test.ts 1.3 KB

12345678910111213141516171819202122232425
  1. // @vitest-environment jsdom
  2. import { act, cleanup, renderHook } from "@testing-library/react";
  3. import { afterEach, expect, it } from "vitest";
  4. import { useProjectSidebarPreferences } from "@/project-sidebar/useProjectSidebarPreferences.js";
  5. import type { VirtualProject } from "@/project-sidebar/contract.js";
  6. const projects = [
  7. { id: "same", hostId: "local", roots: [], pinned: true },
  8. { id: "same", hostId: "remote:server:one", roots: [] },
  9. ] as VirtualProject[];
  10. afterEach(() => {
  11. cleanup();
  12. localStorage.clear();
  13. });
  14. it("bulk expansion shares the per-project preference, preserves other hosts and survives remount", () => {
  15. const first = renderHook(() => useProjectSidebarPreferences(projects, false));
  16. act(() => first.result.current.setAllExpanded(projects, false));
  17. expect(projects.map(first.result.current.isExpanded)).toEqual([false, false]);
  18. act(() => first.result.current.setAllExpanded([projects[0]!], true));
  19. expect(projects.map(first.result.current.isExpanded)).toEqual([true, false]);
  20. first.unmount();
  21. const restored = renderHook(() => useProjectSidebarPreferences(projects, false));
  22. expect(projects.map(restored.result.current.isExpanded)).toEqual([true, false]);
  23. act(() => restored.result.current.setAllExpanded(projects, true));
  24. expect(projects.map(restored.result.current.isExpanded)).toEqual([true, true]);
  25. });