UNPKG

@gleaner/tracker

Version:

A JavaScript tracking library with ecommerce support

77 lines (54 loc) 1.45 kB
import Cache from '../src/cache'; beforeEach(() => { localStorage.clear(); }); function storageSize() { return Object.keys(localStorage.__STORE__).length; } describe("Cache", () => { const CACHE_NAME = "test"; let c = new Cache(CACHE_NAME); describe("#add", () => { const event = { ts: 123 } const keyName = CACHE_NAME + event.ts; const expected = JSON.stringify(event); test("should set item in storage under keyName of concatenated cache name and key", () => { c.add(event.ts, event) expect(localStorage.setItem).toHaveBeenLastCalledWith(keyName, expected); expect(localStorage.__STORE__[keyName]).toBe(expected); expect(storageSize()).toBe(2); }) test("should register keyName as used", () => { expect(c.keys[keyName]).toBeDefined() }) }) describe("#delete", () => { const KEY = "123" test("should register keyName as used", () => { expect(c.keys[CACHE_NAME + KEY]).toBeDefined() }) test("should remove item", () => { c.delete(KEY) expect(c.keys[CACHE_NAME + KEY]).toBe(undefined) }) }) describe("#getAll", () => { test("should return array of values", () => { c.add("1", 1) c.add("2", 2) let arr = c.getAll() expect(arr).toEqual([1, 2]) }) }) describe("#deleteAll", () => { test("should cleanup the cache", () => { c.add("1", 1) c.add("2", 2) c.deleteAll() expect(storageSize()).toBe(0); expect(c.keys).toEqual({}) }) }) })