Skip to content

Commit 57951b6

Browse files
committed
fix tests after merge
1 parent 09b66a6 commit 57951b6

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

src/local-storage.store.spec.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
*/
44

55
import { LocalStorageEngine } from './local-storage-engine';
6-
import { StringValuedAsyncStore, StorageFullUnableToWrite, LocalStorageUnknownFailure } from './string-valued.store';
6+
import {
7+
StringValuedAsyncStore,
8+
StorageFullUnableToWrite,
9+
LocalStorageUnknownFailure,
10+
} from './string-valued.store';
711

812
describe('LocalStorageStore', () => {
913
// Note: window.localStorage is mocked for the node environment via the jsdom jest environment
@@ -101,55 +105,63 @@ describe('LocalStorageStore', () => {
101105
});
102106

103107
describe('StorageFullUnableToWrite exception handling', () => {
104-
let mockLocalStorage: Storage;
108+
// We need to mock localStorage with a controllable length property for testing
109+
// The Storage interface has a read-only 'length' property, so we extend it with
110+
// a private '_length' property that we can modify in tests
111+
let mockLocalStorage: Storage & { _length: number };
105112
let localStorageEngineWithMock: LocalStorageEngine;
106113

107114
beforeEach(() => {
108115
mockLocalStorage = {
116+
get length() {
117+
return this._length || 0;
118+
},
119+
_length: 0,
109120
getItem: jest.fn(),
110121
setItem: jest.fn(),
111122
removeItem: jest.fn(),
112123
clear: jest.fn(),
113-
length: 0,
114124
key: jest.fn(),
115-
};
125+
} as Storage & { _length: number };
116126
localStorageEngineWithMock = new LocalStorageEngine(mockLocalStorage, 'test');
117127
});
118128

119129
it('should throw StorageFullUnableToWrite when setContentsJsonString fails after clear and retry', async () => {
120130
const quotaError = new DOMException('QuotaExceededError', 'QuotaExceededError');
121131
Object.defineProperty(quotaError, 'code', { value: DOMException.QUOTA_EXCEEDED_ERR });
122-
132+
123133
(mockLocalStorage.setItem as jest.Mock).mockImplementation(() => {
124134
throw quotaError;
125135
});
126136
(mockLocalStorage.key as jest.Mock).mockReturnValue(null);
127-
(mockLocalStorage.length as any) = 0;
137+
// Simulate empty storage by setting length to 0
138+
mockLocalStorage._length = 0;
128139

129-
await expect(
130-
localStorageEngineWithMock.setContentsJsonString('test-config')
131-
).rejects.toThrow(StorageFullUnableToWrite);
140+
await expect(localStorageEngineWithMock.setContentsJsonString('test-config')).rejects.toThrow(
141+
StorageFullUnableToWrite,
142+
);
132143
});
133144

134145
it('should throw StorageFullUnableToWrite when setMetaJsonString fails after clear and retry', async () => {
135146
const quotaError = new DOMException('QuotaExceededError', 'QuotaExceededError');
136147
Object.defineProperty(quotaError, 'code', { value: DOMException.QUOTA_EXCEEDED_ERR });
137-
148+
138149
(mockLocalStorage.setItem as jest.Mock).mockImplementation(() => {
139150
throw quotaError;
140151
});
141152
(mockLocalStorage.key as jest.Mock).mockReturnValue(null);
142-
(mockLocalStorage.length as any) = 0;
153+
// Simulate empty storage by setting length to 0
154+
mockLocalStorage._length = 0;
143155

144-
await expect(
145-
localStorageEngineWithMock.setMetaJsonString('test-meta')
146-
).rejects.toThrow(StorageFullUnableToWrite);
156+
await expect(localStorageEngineWithMock.setMetaJsonString('test-meta')).rejects.toThrow(
157+
StorageFullUnableToWrite,
158+
);
147159
});
148160

149161
it('should succeed after clearing when retry works', async () => {
150162
const quotaError = new DOMException('QuotaExceededError', 'QuotaExceededError');
151163
Object.defineProperty(quotaError, 'code', { value: DOMException.QUOTA_EXCEEDED_ERR });
152-
164+
153165
let callCount = 0;
154166
(mockLocalStorage.setItem as jest.Mock).mockImplementation(() => {
155167
callCount++;
@@ -159,14 +171,16 @@ describe('LocalStorageStore', () => {
159171
// Second call succeeds
160172
});
161173
(mockLocalStorage.key as jest.Mock).mockReturnValue('eppo-configuration-old');
162-
(mockLocalStorage.length as any) = 1;
174+
// Simulate storage with one item by setting length to 1
175+
mockLocalStorage._length = 1;
163176

164177
await expect(
165-
localStorageEngineWithMock.setContentsJsonString('test-config')
178+
localStorageEngineWithMock.setContentsJsonString('test-config'),
166179
).resolves.not.toThrow();
167-
180+
168181
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('eppo-configuration-old');
169-
expect(mockLocalStorage.setItem).toHaveBeenCalledTimes(2);
182+
// setItem is called 3 times: 1) migration during construction, 2) first attempt (fails), 3) retry after clearing (succeeds)
183+
expect(mockLocalStorage.setItem).toHaveBeenCalledTimes(3);
170184
});
171185

172186
it('should throw LocalStorageUnknownFailure for non-quota errors', async () => {
@@ -175,7 +189,9 @@ describe('LocalStorageStore', () => {
175189
throw otherError;
176190
});
177191

178-
const error = await localStorageEngineWithMock.setContentsJsonString('test-config').catch(e => e);
192+
const error = await localStorageEngineWithMock
193+
.setContentsJsonString('test-config')
194+
.catch((e) => e);
179195
expect(error).toBeInstanceOf(LocalStorageUnknownFailure);
180196
expect(error.originalError).toBe(otherError);
181197
expect(error.message).toContain('Some other error');

0 commit comments

Comments
 (0)