|
| 1 | +import { MemoryOnlyConfigurationStore, HybridConfigurationStore } from '@eppo/js-client-sdk-common'; |
| 2 | + |
| 3 | +import { ChromeStorageAsyncStore } from './chrome.configuration-store'; |
| 4 | +import { configurationStorageFactory } from './configuration-factory'; |
| 5 | +import { LocalStorageBackedAsyncStore } from './local-storage'; |
| 6 | + |
| 7 | +describe('configurationStorageFactory', () => { |
| 8 | + afterEach(() => { |
| 9 | + jest.restoreAllMocks(); |
| 10 | + }); |
| 11 | + |
| 12 | + it('forces a MemoryOnlyConfigurationStore', () => { |
| 13 | + const result = configurationStorageFactory({ forceMemoryOnly: true }); |
| 14 | + expect(result).toBeInstanceOf(MemoryOnlyConfigurationStore); |
| 15 | + }); |
| 16 | + |
| 17 | + it('is a provided persistentStore', () => { |
| 18 | + const mockPersistentStore = { |
| 19 | + get: jest.fn(), |
| 20 | + set: jest.fn(), |
| 21 | + isInitialized: jest.fn().mockReturnValue(true), |
| 22 | + isExpired: jest.fn().mockReturnValue(false), |
| 23 | + getEntries: jest.fn().mockReturnValue({}), |
| 24 | + setEntries: jest.fn(), |
| 25 | + }; |
| 26 | + const result = configurationStorageFactory({ |
| 27 | + persistentStore: mockPersistentStore, |
| 28 | + }); |
| 29 | + expect(result).toBeInstanceOf(HybridConfigurationStore); |
| 30 | + expect(result.persistentStore).toBe(mockPersistentStore); |
| 31 | + }); |
| 32 | + |
| 33 | + it('is a HybridConfigurationStore with a ChromeStorageAsyncStore persistentStore when chrome storage is available', () => { |
| 34 | + const mockChromeStorageLocal = { |
| 35 | + get: jest.fn(), |
| 36 | + set: jest.fn(), |
| 37 | + remove: jest.fn(), |
| 38 | + clear: jest.fn(), |
| 39 | + getBytesInUse: jest.fn(), |
| 40 | + setAccessLevel: jest.fn(), |
| 41 | + onChanged: { |
| 42 | + addListener: jest.fn(), |
| 43 | + removeListener: jest.fn(), |
| 44 | + hasListener: jest.fn(), |
| 45 | + dispatch: jest.fn(), |
| 46 | + getRules: jest.fn(), |
| 47 | + removeRules: jest.fn(), |
| 48 | + addRules: jest.fn(), |
| 49 | + hasListeners: jest.fn(), |
| 50 | + }, |
| 51 | + }; |
| 52 | + |
| 53 | + const result = configurationStorageFactory( |
| 54 | + { hasChromeStorage: true }, |
| 55 | + { chromeStorage: mockChromeStorageLocal }, |
| 56 | + ); |
| 57 | + expect(result).toBeInstanceOf(HybridConfigurationStore); |
| 58 | + expect(result.persistentStore).toBeInstanceOf(ChromeStorageAsyncStore); |
| 59 | + }); |
| 60 | + |
| 61 | + it('is a HybridConfigurationStore with a LocalStorageBackedAsyncStore persistentStore when window local storage is available', () => { |
| 62 | + const mockLocalStorage = { |
| 63 | + clear: jest.fn(), |
| 64 | + getItem: jest.fn(), |
| 65 | + setItem: jest.fn(), |
| 66 | + removeItem: jest.fn(), |
| 67 | + key: jest.fn(), |
| 68 | + length: 0, |
| 69 | + }; |
| 70 | + |
| 71 | + const result = configurationStorageFactory( |
| 72 | + { hasWindowLocalStorage: true }, |
| 73 | + { windowLocalStorage: mockLocalStorage }, |
| 74 | + ); |
| 75 | + expect(result).toBeInstanceOf(HybridConfigurationStore); |
| 76 | + expect(result.persistentStore).toBeInstanceOf(LocalStorageBackedAsyncStore); |
| 77 | + }); |
| 78 | + |
| 79 | + it('falls back to MemoryOnlyConfigurationStore when no persistence options are available', () => { |
| 80 | + const result = configurationStorageFactory({}); |
| 81 | + expect(result).toBeInstanceOf(MemoryOnlyConfigurationStore); |
| 82 | + }); |
| 83 | +}); |
0 commit comments