import { createPinia, setActivePinia } from "pinia"; import { assert, beforeEach, describe, it } from "vitest"; import { makeStore, useLocalStorageStore, useSessionStorageStore, } from "../../functions/localstorage/store.generic"; interface Foo { id: number; name: string; ts: number; } interface Foo2 extends Foo { id: number; middle_name: string; } interface Bar extends Foo { age: number; } interface Bar2 extends Foo2 { age: number; } describe("useLocalStorageStore", () => { beforeEach(() => { global.localStorage.clear(); global.sessionStorage.clear(); setActivePinia(createPinia()); }); it("can put and get values", () => { const storage = useLocalStorageStore(); const ts = Date.now(); storage.put("foo", { id: 1, name: "John Doe", ts }); assert.deepEqual(storage.get("foo"), { id: 1, name: "John Doe", ts }); }); it("can put and get values in session", () => { const storage = useSessionStorageStore(); const ts = Date.now(); storage.put("foo", { id: 1, name: "John Doe", ts }); assert.deepEqual(storage.get("foo"), { id: 1, name: "John Doe", ts }); }); it("can put and get values in localstorage for a type", () => { const useFoo = makeStore(localStorage, "foo", "foo"); const storage = useFoo(); const ts = Date.now(); storage.put("foo", { id: 1, name: "John Doe", ts }); assert.deepEqual(storage.get("foo"), { id: 1, name: "John Doe", ts }); }); it.skip("can put and get values in localstorage for T1, other than makeStore defined type", () => { const useFoo = makeStore(localStorage, "foo.dup", "foo"); const storage = useFoo(); const ts = Date.now(); storage.put("foo", { id: 1, name: "John Doe", ts, middle_name: "James" }); assert.deepEqual(storage.get("foo"), { id: 1, name: "John Doe", ts }); }); it("can put and get values in localstorage for another type", () => { const useFoo = makeStore(localStorage, "foo2", "foo"); const storage = useFoo(); const ts = Date.now(); storage.put("foo", { id: 1, name: "John Doe", ts, middle_name: "James" }); assert.deepEqual(storage.get("foo"), { id: 1, name: "John Doe", ts, middle_name: "James" }); }); it("can put and get values in session for a type", () => { const useFoo = makeStore(sessionStorage, "bar", "foo"); const storage = useFoo(); const ts = Date.now(); storage.put("foo", { id: 1, name: "John Doe", ts, age: 30 }); assert.deepEqual(storage.get("foo"), { id: 1, name: "John Doe", ts, age: 30 }); }); it("can put and get values in session for another type", () => { const useFoo = makeStore(sessionStorage, "bar2", "foo"); const storage = useFoo(); const ts = Date.now(); storage.put("foo", { id: 1, name: "John Doe", ts, middle_name: "James", age: 30 }); assert.deepEqual(storage.get("foo"), { id: 1, name: "John Doe", ts, middle_name: "James", age: 30 }); }); it.skip("can put and get values in session for T1, other than makeStore defined type", () => { const useFoo = makeStore(sessionStorage, "bar2.dup", "foo"); const storage = useFoo(); const ts = Date.now(); storage.put("foo", { id: 1, name: "John Doe", ts, middle_name: "James", age: 30 }); assert.equal(storage.get("foo"), { id: 1, name: "John Doe", ts, middle_name: "James" }); }); it("throws errors for a duplicate label type", () => { const labels = ["foo", "foo2", "bar", "bar2"]; labels.forEach(item => { assert.throws(() => { const useFoo = makeStore(localStorage, item, item); }) assert.throws(() => { const useFoo = makeStore(sessionStorage, item, item); }) }); }); });