Skip to content

Commit c09f14d

Browse files
Add unit test
1 parent ba83154 commit c09f14d

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

CHANGES.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
2.8.1 (November 21, 2025)
1+
2.8.1 (November 25, 2025)
22
- Updated the order of storage operations to prevent inconsistent states when using the `LOCALSTORAGE` storage type and the browser’s `localStorage` fails due to quota limits.
33

44
2.8.0 (October 30, 2025)

src/storages/inLocalStorage/MySegmentsCacheInLocal.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { AbstractMySegmentsCacheSync } from '../AbstractMySegmentsCacheSync';
44
import type { MySegmentsKeyBuilder } from '../KeyBuilderCS';
55
import { LOG_PREFIX, DEFINED } from './constants';
66
import { StorageAdapter } from '../types';
7+
import { MySegmentsData } from '../../sync/polling/types';
8+
import { IMySegmentsResponse } from '../../dtos/types';
79

810
export class MySegmentsCacheInLocal extends AbstractMySegmentsCacheSync {
911

@@ -16,7 +18,6 @@ export class MySegmentsCacheInLocal extends AbstractMySegmentsCacheSync {
1618
this.log = log;
1719
this.keys = keys;
1820
this.storage = storage;
19-
// There is not need to flush segments cache like splits cache, since resetSegments receives the up-to-date list of active segments
2021
}
2122

2223
protected addSegment(name: string): boolean {
@@ -70,9 +71,9 @@ export class MySegmentsCacheInLocal extends AbstractMySegmentsCacheSync {
7071
return n;
7172
}
7273

73-
registerSegments() {
74+
resetSegments(segmentsData: MySegmentsData | IMySegmentsResponse) {
7475
try {
75-
return super.registerSegments();
76+
return super.resetSegments(segmentsData);
7677
} catch (e) {
7778
this.log.error(LOG_PREFIX + e);
7879
return false;

src/storages/inLocalStorage/__tests__/MySegmentsCacheInLocal.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,16 @@ test.each(storages)('SEGMENT CACHE / in LocalStorage', (storage) => {
3939
expect(storage.getItem(PREFIX + '.user.largeSegment.mocked-segment-2')).toBe('1');
4040
expect(storage.getItem(PREFIX + '.user.largeSegment.mocked-segment')).toBe(null);
4141
});
42+
43+
test('SEGMENT CACHE / Special case: localStorage failure should not throw an exception', () => {
44+
const cache = new MySegmentsCacheInLocal(loggerMock, new KeyBuilderCS(PREFIX, 'user2'), localStorage);
45+
46+
// mock localStorage failure
47+
const setItemSpy = jest.spyOn(localStorage, 'setItem').mockImplementation(() => { throw new Error('localStorage failure'); });
48+
setItemSpy.mockClear();
49+
50+
expect(cache.resetSegments({ k: [{ n: 'mocked-segment' }, { n: 'mocked-segment-2' }], cn: 123 })).toBe(false);
51+
expect(setItemSpy).toHaveBeenCalledTimes(1);
52+
53+
setItemSpy.mockRestore();
54+
});

0 commit comments

Comments
 (0)