|
3 | 3 | */
|
4 | 4 |
|
5 | 5 | import { LocalStorageEngine } from './local-storage-engine';
|
6 |
| -import { StringValuedAsyncStore } from './string-valued.store'; |
| 6 | +import { StringValuedAsyncStore, StorageFullUnableToWrite, LocalStorageUnknownFailure } from './string-valued.store'; |
7 | 7 |
|
8 | 8 | describe('LocalStorageStore', () => {
|
9 | 9 | // Note: window.localStorage is mocked for the node environment via the jsdom jest environment
|
@@ -80,4 +80,105 @@ describe('LocalStorageStore', () => {
|
80 | 80 | expect(await storeB.entries()).toEqual({ theKey: 'B' });
|
81 | 81 | expect(await storeB.isExpired()).toBe(false);
|
82 | 82 | });
|
| 83 | + |
| 84 | + describe('clear method', () => { |
| 85 | + it('should clear all eppo-configuration keys', () => { |
| 86 | + window.localStorage.setItem('eppo-configuration-test', 'value1'); |
| 87 | + window.localStorage.setItem('eppo-configuration-other', 'value2'); |
| 88 | + window.localStorage.setItem('other-key', 'value3'); |
| 89 | + |
| 90 | + localStorageEngine.clear(); |
| 91 | + |
| 92 | + expect(window.localStorage.getItem('eppo-configuration-test')).toBeNull(); |
| 93 | + expect(window.localStorage.getItem('eppo-configuration-other')).toBeNull(); |
| 94 | + expect(window.localStorage.getItem('other-key')).toBe('value3'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should handle empty storage', () => { |
| 98 | + window.localStorage.clear(); |
| 99 | + expect(() => localStorageEngine.clear()).not.toThrow(); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('StorageFullUnableToWrite exception handling', () => { |
| 104 | + let mockLocalStorage: Storage; |
| 105 | + let localStorageEngineWithMock: LocalStorageEngine; |
| 106 | + |
| 107 | + beforeEach(() => { |
| 108 | + mockLocalStorage = { |
| 109 | + getItem: jest.fn(), |
| 110 | + setItem: jest.fn(), |
| 111 | + removeItem: jest.fn(), |
| 112 | + clear: jest.fn(), |
| 113 | + length: 0, |
| 114 | + key: jest.fn(), |
| 115 | + }; |
| 116 | + localStorageEngineWithMock = new LocalStorageEngine(mockLocalStorage, 'test'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should throw StorageFullUnableToWrite when setContentsJsonString fails after clear and retry', async () => { |
| 120 | + const quotaError = new DOMException('QuotaExceededError', 'QuotaExceededError'); |
| 121 | + Object.defineProperty(quotaError, 'code', { value: DOMException.QUOTA_EXCEEDED_ERR }); |
| 122 | + |
| 123 | + (mockLocalStorage.setItem as jest.Mock).mockImplementation(() => { |
| 124 | + throw quotaError; |
| 125 | + }); |
| 126 | + (mockLocalStorage.key as jest.Mock).mockReturnValue(null); |
| 127 | + (mockLocalStorage.length as any) = 0; |
| 128 | + |
| 129 | + await expect( |
| 130 | + localStorageEngineWithMock.setContentsJsonString('test-config') |
| 131 | + ).rejects.toThrow(StorageFullUnableToWrite); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should throw StorageFullUnableToWrite when setMetaJsonString fails after clear and retry', async () => { |
| 135 | + const quotaError = new DOMException('QuotaExceededError', 'QuotaExceededError'); |
| 136 | + Object.defineProperty(quotaError, 'code', { value: DOMException.QUOTA_EXCEEDED_ERR }); |
| 137 | + |
| 138 | + (mockLocalStorage.setItem as jest.Mock).mockImplementation(() => { |
| 139 | + throw quotaError; |
| 140 | + }); |
| 141 | + (mockLocalStorage.key as jest.Mock).mockReturnValue(null); |
| 142 | + (mockLocalStorage.length as any) = 0; |
| 143 | + |
| 144 | + await expect( |
| 145 | + localStorageEngineWithMock.setMetaJsonString('test-meta') |
| 146 | + ).rejects.toThrow(StorageFullUnableToWrite); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should succeed after clearing when retry works', async () => { |
| 150 | + const quotaError = new DOMException('QuotaExceededError', 'QuotaExceededError'); |
| 151 | + Object.defineProperty(quotaError, 'code', { value: DOMException.QUOTA_EXCEEDED_ERR }); |
| 152 | + |
| 153 | + let callCount = 0; |
| 154 | + (mockLocalStorage.setItem as jest.Mock).mockImplementation(() => { |
| 155 | + callCount++; |
| 156 | + if (callCount === 1) { |
| 157 | + throw quotaError; |
| 158 | + } |
| 159 | + // Second call succeeds |
| 160 | + }); |
| 161 | + (mockLocalStorage.key as jest.Mock).mockReturnValue('eppo-configuration-old'); |
| 162 | + (mockLocalStorage.length as any) = 1; |
| 163 | + |
| 164 | + await expect( |
| 165 | + localStorageEngineWithMock.setContentsJsonString('test-config') |
| 166 | + ).resolves.not.toThrow(); |
| 167 | + |
| 168 | + expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('eppo-configuration-old'); |
| 169 | + expect(mockLocalStorage.setItem).toHaveBeenCalledTimes(2); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should throw LocalStorageUnknownFailure for non-quota errors', async () => { |
| 173 | + const otherError = new Error('Some other error'); |
| 174 | + (mockLocalStorage.setItem as jest.Mock).mockImplementation(() => { |
| 175 | + throw otherError; |
| 176 | + }); |
| 177 | + |
| 178 | + const error = await localStorageEngineWithMock.setContentsJsonString('test-config').catch(e => e); |
| 179 | + expect(error).toBeInstanceOf(LocalStorageUnknownFailure); |
| 180 | + expect(error.originalError).toBe(otherError); |
| 181 | + expect(error.message).toContain('Some other error'); |
| 182 | + }); |
| 183 | + }); |
83 | 184 | });
|
0 commit comments