storage/src/basic.test.ts

44 lines
1.4 KiB
TypeScript
Raw Normal View History

import { KeyValueStorage, MemoryStorage, ScopedStorage } from "./basic";
export async function basicCheck(storage: KeyValueStorage): Promise<void> {
expect(await storage.get("test")).toBe(null);
await storage.set("test", "val");
expect(await storage.get("test")).toBe("val");
expect(await storage.get("notest")).toBe(null);
await storage.set("test", null);
expect(await storage.get("test")).toBe(null);
}
describe(MemoryStorage, () => {
it("stores values in memory", async () => {
const storage = new MemoryStorage();
await basicCheck(storage);
});
});
describe(ScopedStorage, () => {
it("uses a target storage, scoping its keys", async () => {
const reference1 = new MemoryStorage();
const reference2 = new MemoryStorage();
const target = new MemoryStorage();
let storage = new ScopedStorage(reference1, () => target);
await basicCheck(storage);
await storage.set("persist", "42");
expect(await storage.get("persist")).toBe("42");
storage = new ScopedStorage(() => reference1, target);
expect(await storage.get("persist")).toBe("42");
storage = new ScopedStorage(reference2, target);
await storage.set("other", "thing");
expect(await storage.get("persist")).toBe(null);
expect(await storage.get("other")).toBe("thing");
storage = new ScopedStorage(reference1, target);
expect(await storage.get("persist")).toBe("42");
expect(await storage.get("other")).toBe(null);
});
});