2019-11-07 21:33:23 +00:00
|
|
|
import { KeyValueStorage, MemoryStorage, RefScopedStorage } from "./basic";
|
2019-10-02 20:12:55 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-11-07 21:33:23 +00:00
|
|
|
describe(RefScopedStorage, () => {
|
2019-10-02 20:12:55 +00:00
|
|
|
it("uses a target storage, scoping its keys", async () => {
|
|
|
|
const reference1 = new MemoryStorage();
|
|
|
|
const reference2 = new MemoryStorage();
|
|
|
|
const target = new MemoryStorage();
|
|
|
|
|
2019-11-07 21:33:23 +00:00
|
|
|
let storage = new RefScopedStorage(reference1, () => target);
|
2019-10-02 20:12:55 +00:00
|
|
|
await basicCheck(storage);
|
|
|
|
|
|
|
|
await storage.set("persist", "42");
|
|
|
|
expect(await storage.get("persist")).toBe("42");
|
|
|
|
|
2019-11-07 21:33:23 +00:00
|
|
|
storage = new RefScopedStorage(() => reference1, target);
|
2019-10-02 20:12:55 +00:00
|
|
|
expect(await storage.get("persist")).toBe("42");
|
|
|
|
|
2019-11-07 21:33:23 +00:00
|
|
|
storage = new RefScopedStorage(reference2, target);
|
2019-10-02 20:12:55 +00:00
|
|
|
await storage.set("other", "thing");
|
|
|
|
expect(await storage.get("persist")).toBe(null);
|
|
|
|
expect(await storage.get("other")).toBe("thing");
|
|
|
|
|
2019-11-07 21:33:23 +00:00
|
|
|
storage = new RefScopedStorage(reference1, target);
|
2019-10-02 20:12:55 +00:00
|
|
|
expect(await storage.get("persist")).toBe("42");
|
|
|
|
expect(await storage.get("other")).toBe(null);
|
|
|
|
});
|
|
|
|
});
|