|
| 1 | +import { LocalStorageEngine } from './local-storage-engine'; |
| 2 | +import { StorageFullUnableToWrite, LocalStorageUnknownFailure } from './string-valued.store'; |
| 3 | + |
| 4 | +describe('LocalStorageEngine', () => { |
| 5 | + let mockLocalStorage: Storage; |
| 6 | + let engine: LocalStorageEngine; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + mockLocalStorage = { |
| 10 | + get length() { |
| 11 | + return this._length || 0; |
| 12 | + }, |
| 13 | + _length: 0, |
| 14 | + clear: jest.fn(), |
| 15 | + getItem: jest.fn(), |
| 16 | + key: jest.fn(), |
| 17 | + removeItem: jest.fn(), |
| 18 | + setItem: jest.fn(), |
| 19 | + } as Storage & { _length: number }; |
| 20 | + engine = new LocalStorageEngine(mockLocalStorage, 'test'); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('setContentsJsonString', () => { |
| 24 | + it('should set item successfully when no error occurs', async () => { |
| 25 | + await engine.setContentsJsonString('test-config'); |
| 26 | + |
| 27 | + expect(mockLocalStorage.setItem).toHaveBeenCalledWith( |
| 28 | + 'eppo-configuration-test', |
| 29 | + 'test-config', |
| 30 | + ); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should clear eppo-configuration keys and retry on quota exceeded error', async () => { |
| 34 | + const quotaError = new DOMException('Quota exceeded', 'QuotaExceededError'); |
| 35 | + Object.defineProperty(quotaError, 'code', { value: DOMException.QUOTA_EXCEEDED_ERR }); |
| 36 | + |
| 37 | + // Mock localStorage to have some eppo-configuration keys |
| 38 | + (mockLocalStorage as Storage & { _length: number })._length = 3; |
| 39 | + (mockLocalStorage.key as jest.Mock) |
| 40 | + .mockReturnValueOnce('eppo-configuration-old') |
| 41 | + .mockReturnValueOnce('other-key') |
| 42 | + .mockReturnValueOnce('eppo-configuration-meta-old'); |
| 43 | + |
| 44 | + // First call fails with quota error, second call succeeds |
| 45 | + (mockLocalStorage.setItem as jest.Mock) |
| 46 | + .mockImplementationOnce(() => { |
| 47 | + throw quotaError; |
| 48 | + }) |
| 49 | + .mockImplementationOnce(jest.fn()); // Success on retry |
| 50 | + |
| 51 | + await engine.setContentsJsonString('test-config'); |
| 52 | + |
| 53 | + // Verify keys were collected and removed |
| 54 | + expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('eppo-configuration-old'); |
| 55 | + expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('eppo-configuration-meta-old'); |
| 56 | + expect(mockLocalStorage.removeItem).not.toHaveBeenCalledWith('other-key'); |
| 57 | + |
| 58 | + // Verify setItem was called twice (original + retry) |
| 59 | + expect(mockLocalStorage.setItem).toHaveBeenCalledTimes(2); |
| 60 | + expect(mockLocalStorage.setItem).toHaveBeenLastCalledWith( |
| 61 | + 'eppo-configuration-test', |
| 62 | + 'test-config', |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should throw StorageFullUnableToWrite when retry fails', async () => { |
| 67 | + const quotaError = new DOMException('Quota exceeded', 'QuotaExceededError'); |
| 68 | + Object.defineProperty(quotaError, 'code', { value: DOMException.QUOTA_EXCEEDED_ERR }); |
| 69 | + |
| 70 | + (mockLocalStorage as Storage & { _length: number })._length = 1; |
| 71 | + (mockLocalStorage.key as jest.Mock).mockReturnValue('eppo-configuration-old'); |
| 72 | + |
| 73 | + // Both calls fail with quota error |
| 74 | + (mockLocalStorage.setItem as jest.Mock).mockImplementation(() => { |
| 75 | + throw quotaError; |
| 76 | + }); |
| 77 | + |
| 78 | + // Should throw StorageFullUnableToWrite |
| 79 | + await expect(engine.setContentsJsonString('test-config')).rejects.toThrow( |
| 80 | + StorageFullUnableToWrite, |
| 81 | + ); |
| 82 | + |
| 83 | + expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('eppo-configuration-old'); |
| 84 | + expect(mockLocalStorage.setItem).toHaveBeenCalledTimes(2); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should handle quota exceeded error by name', async () => { |
| 88 | + const quotaError = new DOMException('Quota exceeded'); |
| 89 | + Object.defineProperty(quotaError, 'name', { value: 'QuotaExceededError' }); |
| 90 | + |
| 91 | + (mockLocalStorage as Storage & { _length: number })._length = 1; |
| 92 | + (mockLocalStorage.key as jest.Mock).mockReturnValue('eppo-configuration-test-key'); |
| 93 | + |
| 94 | + (mockLocalStorage.setItem as jest.Mock) |
| 95 | + .mockImplementationOnce(() => { |
| 96 | + throw quotaError; |
| 97 | + }) |
| 98 | + .mockImplementationOnce(jest.fn()); // Success on retry |
| 99 | + |
| 100 | + await engine.setContentsJsonString('test-config'); |
| 101 | + |
| 102 | + expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('eppo-configuration-test-key'); |
| 103 | + expect(mockLocalStorage.setItem).toHaveBeenCalledTimes(2); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should throw LocalStorageUnknownFailure for non-quota errors', async () => { |
| 107 | + const securityError = new DOMException('Security error', 'SecurityError'); |
| 108 | + |
| 109 | + (mockLocalStorage.setItem as jest.Mock).mockImplementation(() => { |
| 110 | + throw securityError; |
| 111 | + }); |
| 112 | + |
| 113 | + // Should throw LocalStorageUnknownFailure for non-quota errors with original error preserved |
| 114 | + const error = await engine.setContentsJsonString('test-config').catch((e) => e); |
| 115 | + expect(error).toBeInstanceOf(LocalStorageUnknownFailure); |
| 116 | + expect(error.message).toContain('Security error'); |
| 117 | + expect(error.originalError).toBeTruthy(); |
| 118 | + expect(error.originalError.message).toBe('Security error'); |
| 119 | + |
| 120 | + expect(mockLocalStorage.removeItem).not.toHaveBeenCalled(); |
| 121 | + expect(mockLocalStorage.setItem).toHaveBeenCalledTimes(1); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe('clearEppoConfigurationKeys', () => { |
| 126 | + it('should only remove keys starting with eppo-configuration', async () => { |
| 127 | + const quotaError = new DOMException('Quota exceeded', 'QuotaExceededError'); |
| 128 | + Object.defineProperty(quotaError, 'code', { value: DOMException.QUOTA_EXCEEDED_ERR }); |
| 129 | + |
| 130 | + (mockLocalStorage as Storage & { _length: number })._length = 5; |
| 131 | + (mockLocalStorage.key as jest.Mock) |
| 132 | + .mockReturnValueOnce('eppo-configuration') |
| 133 | + .mockReturnValueOnce('eppo-configuration-meta') |
| 134 | + .mockReturnValueOnce('eppo-overrides') |
| 135 | + .mockReturnValueOnce('other-app-data') |
| 136 | + .mockReturnValueOnce('eppo-configuration-custom'); |
| 137 | + |
| 138 | + (mockLocalStorage.setItem as jest.Mock) |
| 139 | + .mockImplementationOnce(() => { |
| 140 | + throw quotaError; |
| 141 | + }) |
| 142 | + .mockImplementationOnce(jest.fn()); // Success on retry |
| 143 | + |
| 144 | + await engine.setContentsJsonString('test-config'); |
| 145 | + |
| 146 | + // Should remove eppo-configuration keys |
| 147 | + expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('eppo-configuration'); |
| 148 | + expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('eppo-configuration-meta'); |
| 149 | + expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('eppo-configuration-custom'); |
| 150 | + |
| 151 | + // Should not remove other keys |
| 152 | + expect(mockLocalStorage.removeItem).not.toHaveBeenCalledWith('eppo-overrides'); |
| 153 | + expect(mockLocalStorage.removeItem).not.toHaveBeenCalledWith('other-app-data'); |
| 154 | + |
| 155 | + expect(mockLocalStorage.removeItem).toHaveBeenCalledTimes(3); |
| 156 | + }); |
| 157 | + }); |
| 158 | +}); |
0 commit comments