|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import ChromeStorageAssignmentCache from './chrome-storage-assignment-cache'; |
| 6 | +import HybridAssignmentCache from './hybrid-assignment-cache'; |
| 7 | +import { LocalStorageAssignmentCache } from './local-storage-assignment-cache'; |
| 8 | + |
| 9 | +import StorageArea = chrome.storage.StorageArea; |
| 10 | + |
| 11 | +describe('HybridStorageAssignmentCache', () => { |
| 12 | + const fakeStore: Record<string, string> = {}; |
| 13 | + |
| 14 | + const get = jest.fn((key?: string) => { |
| 15 | + return new Promise((resolve) => { |
| 16 | + if (!key) { |
| 17 | + resolve(fakeStore); |
| 18 | + } else { |
| 19 | + resolve({ [key]: fakeStore[key] }); |
| 20 | + } |
| 21 | + }); |
| 22 | + }) as jest.Mock; |
| 23 | + |
| 24 | + const set = jest.fn((items: { [key: string]: string }) => { |
| 25 | + return new Promise((resolve) => { |
| 26 | + Object.assign(fakeStore, items); |
| 27 | + resolve(undefined); |
| 28 | + }); |
| 29 | + }) as jest.Mock; |
| 30 | + |
| 31 | + const mockChromeStorage = { get, set } as unknown as StorageArea; |
| 32 | + const chromeStorageCache = new ChromeStorageAssignmentCache(mockChromeStorage); |
| 33 | + const localStorageCache = new LocalStorageAssignmentCache('test'); |
| 34 | + const hybridCache = new HybridAssignmentCache(localStorageCache, chromeStorageCache); |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + window.localStorage.clear(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('has should return false if cache is empty', async () => { |
| 41 | + const cacheKey = { |
| 42 | + subjectKey: 'subject-1', |
| 43 | + flagKey: 'flag-1', |
| 44 | + allocationKey: 'allocation-1', |
| 45 | + variationKey: 'control', |
| 46 | + }; |
| 47 | + await hybridCache.init(); |
| 48 | + expect(hybridCache.has(cacheKey)).toBeFalsy(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('has should return true if cache key is present', async () => { |
| 52 | + const cacheKey = { |
| 53 | + subjectKey: 'subject-1', |
| 54 | + flagKey: 'flag-1', |
| 55 | + allocationKey: 'allocation-1', |
| 56 | + variationKey: 'control', |
| 57 | + }; |
| 58 | + await hybridCache.init(); |
| 59 | + expect(hybridCache.has(cacheKey)).toBeFalsy(); |
| 60 | + expect(localStorageCache.has(cacheKey)).toBeFalsy(); |
| 61 | + hybridCache.set(cacheKey); |
| 62 | + expect(hybridCache.has(cacheKey)).toBeTruthy(); |
| 63 | + expect(localStorageCache.has(cacheKey)).toBeTruthy(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should populate localStorageCache from chromeStorageCache', async () => { |
| 67 | + const key1 = { |
| 68 | + subjectKey: 'subject-1', |
| 69 | + flagKey: 'flag-1', |
| 70 | + allocationKey: 'allocation-1', |
| 71 | + variationKey: 'control', |
| 72 | + }; |
| 73 | + const key2 = { |
| 74 | + subjectKey: 'subject-2', |
| 75 | + flagKey: 'flag-2', |
| 76 | + allocationKey: 'allocation-2', |
| 77 | + variationKey: 'control', |
| 78 | + }; |
| 79 | + expect(localStorageCache.has(key1)).toBeFalsy(); |
| 80 | + chromeStorageCache.set(key1); |
| 81 | + chromeStorageCache.set(key2); |
| 82 | + await hybridCache.init(); |
| 83 | + expect(localStorageCache.has(key1)).toBeTruthy(); |
| 84 | + expect(localStorageCache.has(key2)).toBeTruthy(); |
| 85 | + expect(localStorageCache.has({ ...key1, allocationKey: 'foo' })).toBeFalsy(); |
| 86 | + }); |
| 87 | +}); |
0 commit comments