File size: 3,387 Bytes
ff7a361
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { beforeEach, describe, expect, it, vi } from "vitest";
import { addLogEntry } from "./logEntries";

vi.mock("./logEntries", () => ({
  addLogEntry: vi.fn(),
}));

beforeEach(() => {
  const storage: Record<string, string> = {};
  vi.stubGlobal("localStorage", {
    getItem: (key: string) => (key in storage ? storage[key] : null),
    setItem: (key: string, value: string) => {
      storage[key] = value;
    },
    removeItem: (key: string) => {
      delete storage[key];
    },
    clear: () => {
      for (const k in storage) delete storage[k];
    },
  });
  vi.clearAllMocks();
  vi.resetModules();
});

describe("PubSub localStorage persistence", () => {
  it("updates query suggestions and persists to localStorage", async () => {
    const { updateQuerySuggestions, getQuerySuggestions } = await import(
      "./pubSub"
    );
    expect(getQuerySuggestions()).toEqual([]);
    const newSuggestions = ["apple", "banana"];
    updateQuerySuggestions(newSuggestions);
    expect(getQuerySuggestions()).toEqual(newSuggestions);
    const stored = JSON.parse(
      localStorage.getItem("querySuggestions") as string,
    );
    expect(stored).toEqual(newSuggestions);
  });

  it("updates settings and persists to localStorage", async () => {
    const { settingsPubSub } = await import("./pubSub");
    const [, , getCurrentSettings] = settingsPubSub;
    const defaultSettings = getCurrentSettings();
    expect(defaultSettings).toBeDefined();
    const [updateSettings] = settingsPubSub;
    const modified = { ...defaultSettings, enterToSubmit: false };
    updateSettings(modified);
    expect(getCurrentSettings()).toEqual(modified);
    const stored = JSON.parse(localStorage.getItem("settings") as string);
    expect(stored).toEqual(modified);
  });

  it("falls back to the default and drops the value when the stored JSON is corrupted", async () => {
    localStorage.setItem("menuExpandedAccordions", "{not valid json");
    const { menuExpandedAccordionsPubSub } = await import("./pubSub");
    const [, , getMenuExpandedAccordions] = menuExpandedAccordionsPubSub;
    expect(getMenuExpandedAccordions()).toEqual([]);
    expect(localStorage.getItem("menuExpandedAccordions")).toBeNull();
    expect(addLogEntry).toHaveBeenCalledWith(
      "Discarded an unusable stored value for 'menuExpandedAccordions'",
    );
  });

  it("falls back to the default and drops the value when the stored JSON has the wrong type", async () => {
    localStorage.setItem("lastSearchTokenHash", "123");
    const { getLastSearchTokenHash } = await import("./pubSub");
    expect(getLastSearchTokenHash()).toBe("");
    expect(localStorage.getItem("lastSearchTokenHash")).toBeNull();
    expect(addLogEntry).toHaveBeenCalledWith(
      "Discarded an unusable stored value for 'lastSearchTokenHash'",
    );
  });

  it("falls back to the default and drops the value when the stored value is empty", async () => {
    localStorage.setItem("menuExpandedAccordions", "");
    const { menuExpandedAccordionsPubSub } = await import("./pubSub");
    const [, , getMenuExpandedAccordions] = menuExpandedAccordionsPubSub;
    expect(getMenuExpandedAccordions()).toEqual([]);
    expect(localStorage.getItem("menuExpandedAccordions")).toBeNull();
    expect(addLogEntry).toHaveBeenCalledWith(
      "Discarded an unusable stored value for 'menuExpandedAccordions'",
    );
  });
});