fileManagerNavigationPreference.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import type { IBroadcastService } from "@zcode/services";
  2. import { beforeEach, describe, expect, it } from "vitest";
  3. import {
  4. createZCodeStore,
  5. FILE_MANAGER_NAVIGATION_PREFERENCES_STORAGE_KEY,
  6. } from "@/store/index.js";
  7. const localStorageState = new Map<string, string>();
  8. Object.defineProperty(globalThis, "localStorage", {
  9. configurable: true,
  10. value: {
  11. getItem: (key: string) => localStorageState.get(key) ?? null,
  12. setItem: (key: string, value: string) => {
  13. localStorageState.set(key, value);
  14. },
  15. removeItem: (key: string) => {
  16. localStorageState.delete(key);
  17. },
  18. clear: () => {
  19. localStorageState.clear();
  20. },
  21. },
  22. });
  23. Object.defineProperty(globalThis, "document", {
  24. configurable: true,
  25. value: {
  26. documentElement: {
  27. classList: {
  28. contains: () => false,
  29. toggle: () => {},
  30. },
  31. },
  32. },
  33. });
  34. const mockBroadcastService: IBroadcastService = {
  35. send: async () => {},
  36. onMessage: () => ({ dispose: () => {} }),
  37. };
  38. function isNavigationOpen(
  39. store: ReturnType<typeof createZCodeStore>,
  40. workspaceKey: string,
  41. ): boolean {
  42. return store.getState().fileManagerNavigationOpenByWorkspace[workspaceKey] ?? true;
  43. }
  44. describe("file manager navigation preference", () => {
  45. beforeEach(() => {
  46. localStorageState.clear();
  47. });
  48. it("同一 workspace 跨 store 重建恢复最后显隐状态", () => {
  49. const firstStore = createZCodeStore(mockBroadcastService);
  50. firstStore.getState().setFileManagerNavigationOpen("workspace-a", false);
  51. expect(isNavigationOpen(firstStore, "workspace-a")).toBe(false);
  52. expect(isNavigationOpen(createZCodeStore(mockBroadcastService), "workspace-a")).toBe(false);
  53. });
  54. it("不同 workspace 各自保存显隐状态", () => {
  55. const store = createZCodeStore(mockBroadcastService);
  56. store.getState().setFileManagerNavigationOpen("workspace-a", false);
  57. expect(isNavigationOpen(store, "workspace-a")).toBe(false);
  58. expect(isNavigationOpen(store, "workspace-b")).toBe(true);
  59. store.getState().setFileManagerNavigationOpen("workspace-b", false);
  60. store.getState().setFileManagerNavigationOpen("workspace-a", true);
  61. expect(isNavigationOpen(store, "workspace-a")).toBe(true);
  62. expect(isNavigationOpen(store, "workspace-b")).toBe(false);
  63. });
  64. it("损坏或非法持久化数据回退为默认展开", () => {
  65. localStorageState.set(FILE_MANAGER_NAVIGATION_PREFERENCES_STORAGE_KEY, "not-json");
  66. expect(isNavigationOpen(createZCodeStore(mockBroadcastService), "workspace-a")).toBe(true);
  67. localStorageState.set(
  68. FILE_MANAGER_NAVIGATION_PREFERENCES_STORAGE_KEY,
  69. JSON.stringify({ "workspace-a": "false", "workspace-b": false }),
  70. );
  71. const store = createZCodeStore(mockBroadcastService);
  72. expect(isNavigationOpen(store, "workspace-a")).toBe(true);
  73. expect(isNavigationOpen(store, "workspace-b")).toBe(false);
  74. });
  75. });