1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-06 00:07:44 +01:00
unleash.unleash/frontend/src/utils/storage.test.ts
2024-01-29 12:33:01 +01:00

129 lines
3.7 KiB
TypeScript

import {
setLocalStorageItem,
getLocalStorageItem,
setSessionStorageItem,
getSessionStorageItem,
} from './storage';
import { vi } from 'vitest';
// Mocking the global localStorage
const localStorageMock = (() => {
let store: Record<string, string> = {};
return {
getItem(key: string) {
return store[key] || null;
},
setItem(key: string, value: string) {
store[key] = value.toString();
},
removeItem(key: string) {
delete store[key];
},
clear() {
store = {};
},
};
})();
const sessionStorageMock = (() => {
let store: Record<string, string> = {};
return {
getItem(key: string) {
return store[key] || null;
},
setItem(key: string, value: string) {
store[key] = value.toString();
},
removeItem(key: string) {
delete store[key];
},
clear() {
store = {};
},
};
})();
Object.defineProperty(window, 'localStorage', {
value: localStorageMock,
});
Object.defineProperty(window, 'sessionStorage', {
value: sessionStorageMock,
});
// Test suite
describe('localStorage with TTL', () => {
beforeEach(() => {
localStorage.clear();
vi.useFakeTimers();
});
test('item should be retrievable before TTL expires', () => {
setLocalStorageItem('testKey', 'testValue', 600000);
expect(getLocalStorageItem('testKey')).toBe('testValue');
});
test('item should not be retrievable after TTL expires', () => {
setLocalStorageItem('testKey', 'testValue', 500000);
vi.advanceTimersByTime(600000);
expect(getLocalStorageItem('testKey')).toBeUndefined();
});
test('object should be retrievable before TTL expires', () => {
const testObject = { name: 'Test', number: 123 };
setLocalStorageItem('testObjectKey', testObject, 600000);
const retrievedObject = getLocalStorageItem<{
name: string;
number: number;
}>('testObjectKey');
expect(retrievedObject).toEqual(testObject);
});
test('object should not be retrievable after TTL expires', () => {
const testObject = { name: 'Test', number: 123 };
setLocalStorageItem('testObjectKey', testObject, 500000);
vi.advanceTimersByTime(600000);
const retrievedObject = getLocalStorageItem<{
name: string;
number: number;
}>('testObjectKey');
expect(retrievedObject).toBeUndefined();
});
test('object should be retrievable before TTL expires in sessionStorage', () => {
const testObject = { name: 'TestSession', number: 456 };
setSessionStorageItem('testObjectKeySession', testObject, 500000);
const retrievedObject = getSessionStorageItem<typeof testObject>(
'testObjectKeySession',
);
expect(retrievedObject).toEqual(testObject);
});
test('object should not be retrievable after TTL expires in sessionStorage', () => {
const testObject = { name: 'TestSession', number: 456 };
setSessionStorageItem('testObjectKeySession', testObject, 500000); // 10 minutes TTL
vi.advanceTimersByTime(600000);
const retrievedObject = getSessionStorageItem<typeof testObject>(
'testObjectKeySession',
);
expect(retrievedObject).toBeUndefined();
});
test('should handle set with any level of nesting', () => {
setLocalStorageItem(
'testKey',
new Set([{ nestedSet: new Set([1, 2]) }]),
);
expect(getLocalStorageItem('testKey')).toEqual([{ nestedSet: [1, 2] }]);
});
});