2019-10-02 20:12:55 +00:00
|
|
|
import { getLocalStorage } from ".";
|
2019-11-07 21:33:23 +00:00
|
|
|
import { MemoryStorage, ScopedStorage } from "./basic";
|
2019-10-02 20:12:55 +00:00
|
|
|
import { BrowserLocalStorage } from "./browser";
|
2019-11-07 21:33:23 +00:00
|
|
|
import { NodeDirectoryStorage } from "./node";
|
|
|
|
import { forceNodeStoragesInTempDir } from "./node.test";
|
2019-10-02 20:12:55 +00:00
|
|
|
|
|
|
|
const localStorage = (window as any).localStorage;
|
|
|
|
|
|
|
|
describe(getLocalStorage, () => {
|
2019-11-07 21:33:23 +00:00
|
|
|
forceNodeStoragesInTempDir();
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
(window as any).localStorage = localStorage;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns the best storage available", async () => {
|
2019-10-02 20:12:55 +00:00
|
|
|
const mockWarn = jest.spyOn(console, "warn").mockImplementation();
|
|
|
|
|
2019-11-07 21:33:23 +00:00
|
|
|
let storage = getLocalStorage("app1");
|
|
|
|
expect(storage).toBeInstanceOf(ScopedStorage);
|
|
|
|
expect((storage as any).target).toBeInstanceOf(BrowserLocalStorage);
|
2019-10-02 20:12:55 +00:00
|
|
|
expect(mockWarn).not.toHaveBeenCalled();
|
|
|
|
|
2019-11-07 21:33:23 +00:00
|
|
|
await storage.set("testkey", "testvalue");
|
|
|
|
expect(await storage.get("testkey")).toBe("testvalue");
|
|
|
|
|
|
|
|
storage = getLocalStorage("app1");
|
|
|
|
expect(await storage.get("testkey")).toBe("testvalue");
|
|
|
|
|
|
|
|
storage = getLocalStorage("app2");
|
|
|
|
expect(await storage.get("testkey")).toBeNull();
|
|
|
|
|
2019-10-02 20:12:55 +00:00
|
|
|
delete (window as any)["localStorage"];
|
2019-11-07 21:33:23 +00:00
|
|
|
storage = getLocalStorage("app1");
|
|
|
|
expect(storage).toBeInstanceOf(NodeDirectoryStorage);
|
|
|
|
|
|
|
|
(NodeDirectoryStorage.findUserData as any).mockImplementation(() => { throw new Error() });
|
|
|
|
|
|
|
|
storage = getLocalStorage("app1");
|
2019-10-02 20:12:55 +00:00
|
|
|
expect(storage).toBeInstanceOf(MemoryStorage);
|
|
|
|
expect(mockWarn).toHaveBeenCalledWith("No persistent storage available, using in-memory volatile storage");
|
|
|
|
});
|
|
|
|
});
|