forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsessionStore.test.ts
More file actions
39 lines (30 loc) · 1.09 KB
/
Copy pathsessionStore.test.ts
File metadata and controls
39 lines (30 loc) · 1.09 KB
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
import { describe, it, expect, beforeEach, vi } from "vitest";
import { sessionStore } from "./sessionStore";
describe("sessionStore", () => {
beforeEach(() => {
localStorage.clear();
vi.clearAllMocks();
});
it("sets and gets items", () => {
sessionStore.setItem("test-key", { foo: "bar" });
const item = sessionStore.getItem<{ foo: string }>("test-key");
expect(item).toEqual({ foo: "bar" });
});
it("handles expiry", () => {
vi.useFakeTimers();
sessionStore.setItem("expiring-key", "val", 1000);
expect(sessionStore.getItem("expiring-key")).toBe("val");
vi.advanceTimersByTime(1001);
expect(sessionStore.getItem("expiring-key")).toBeNull();
vi.useRealTimers();
});
it("subscribes to changes", () => {
const listener = vi.fn();
const unsubscribe = sessionStore.subscribe(listener);
sessionStore.setItem("sub-key", "new-val");
expect(listener).toHaveBeenCalledWith("sub-key", "new-val");
unsubscribe();
sessionStore.setItem("sub-key", "another-val");
expect(listener).toHaveBeenCalledTimes(1);
});
});