UNPKG

@applicaster/zapp-react-dom-app

Version:

Zapp App Component for Applicaster's Quick Brick React Native App

176 lines (141 loc) • 5.05 kB
import * as R from "ramda"; import { getStorageModule, applyNamespaceToKeyName, STORAGE_TYPES, DEFAULT_NAMESPACE, } from "../"; import { StorageMock } from "./StorageMocks"; import * as functionUtils from "@applicaster/zapp-react-native-utils/functionUtils"; const noopSpy = jest.spyOn(functionUtils, "noop"); const consoleSpy = jest.spyOn(console, "warn").mockImplementation(() => {}); global.window = { sessionStorage: new StorageMock("sessionStorage"), localStorage: new StorageMock("localStorage"), }; function resetMocks() { noopSpy.mockClear(); consoleSpy.mockClear(); } function clearStorage() { R.forEach((storage) => global.window[storage].clear(), STORAGE_TYPES); } const storages = R.map((storage) => getStorageModule(storage), STORAGE_TYPES); describe("storageObject", () => { beforeEach(resetMocks); describe("when the requested storage exists", () => { const key = "foo"; const value = "bar"; const namespaceValue = "baz"; const namespace = "baz"; const keyName = applyNamespaceToKeyName(key, namespace); const failKey = "fail_key"; beforeEach(clearStorage); it.each(storages)("returns an object with 3 methods", (storage) => { expect(storage).toHaveProperty("setItem"); expect(storage).toHaveProperty("getItem"); expect(storage).toHaveProperty("getAllItems"); }); describe("setItem", () => { beforeEach(clearStorage); it.each(storages)("sets a property in the storage", async (storage) => { const result = await storage.setItem(key, value, null); const keyWithDefaultNameSpace = applyNamespaceToKeyName( key, DEFAULT_NAMESPACE ); expect(result).toBe(true); expect(storage.getItem(keyWithDefaultNameSpace)).resolves.toEqual( value ); }); it.each(storages)( "sets a property in the storage with a namespace", async (storage) => { const result = await storage.setItem(key, namespaceValue, namespace); expect(result).toBe(true); expect(storage.getItem(keyName)).resolves.toEqual(namespaceValue); } ); it.each(storages)( "will reject a promise if it the setting of the data fails", async (storage) => { await expect(storage.setItem(failKey, value)).rejects.toThrow( "Could not set item applicaster.v2_::_fail_key" ); } ); }); describe("getItem", () => { beforeEach(async () => { clearStorage(); storages.forEach((Storage) => { Storage.setItem(key, value, null); Storage.setItem(key, namespaceValue, namespace); }); }); it.each(storages)("gets properties from the storage", async (storage) => { const result = await storage.getItem(key); const resultWithNull = await storage.getItem(key, null); expect(result).toEqual(value); expect(resultWithNull).toEqual(value); }); it.each(storages)( "gets properties from the storage with a namespace", (storage) => { expect(storage.getItem(key, namespace)).resolves.toEqual( "namespaceValue" ); } ); it.each(storages)( "rejects if the property cannot be retrieved", async (storage) => { await expect(storage.getItem(failKey)).rejects.toThrow( "Could not retrieve item applicaster.v2_::_fail_key" ); } ); }); describe("getAllItems", () => { beforeEach(async () => { clearStorage(); storages.forEach((Storage) => { Storage.setItem(key, value); Storage.setItem(key, namespaceValue, namespace); Storage.setItem("otherKey", "otherValue"); Storage.setItem("otherKey", "otherValue", namespace); }); }); it.each(storages)("returns all keys", async (Storage) => { const res = await Storage.getAllItems(); expect(res).toMatchSnapshot(); }); it.each(storages)( "returns all keys in the namespace if provided", async (Storage) => { await expect( Storage.getAllItems(namespace) ).resolves.toMatchSnapshot(); } ); }); describe("removeItem", () => { it.each(storages)("removes an item", async (Storage) => { await expect(Storage.removeItem(key)).resolves.toBeTruthy(); await expect(Storage.removeItem(key, namespace)).resolves.toBeTruthy(); }); }); }); describe("when the request storage doesn't exist", () => { it("returns the noop function and displays a message in the console", () => { const storageType = "some_storage"; const Storage = getStorageModule(storageType); Storage.setItem(); expect(noopSpy).toHaveBeenCalled(); expect(consoleSpy).toHaveBeenCalledWith( `Storage type ${storageType} does not exist in this environment` ); }); }); });