1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-10-13 11:17:26 +02:00
unleash.unleash/frontend/src/hooks/useClearSWRCache.test.ts
Gastón Fournier abe160eb7d
feat: Unleash v7 ESM migration (#9877)
We're migrating to ESM, which will allow us to import the latest
versions of our dependencies.

Co-Authored-By: Christopher Kolstad <chriswk@getunleash.io>
2025-05-14 09:47:12 +02:00

54 lines
1.9 KiB
TypeScript

import { clearCacheEntries } from './useClearSWRCache.js';
describe('manageCacheEntries', () => {
it('should clear old cache entries and keep the current one when SWR_CACHE_SIZE is not provided', () => {
const cacheMock = new Map();
cacheMock.set('prefix-1', {});
cacheMock.set('prefix-2', {});
cacheMock.set('prefix-3', {});
clearCacheEntries(cacheMock, 'prefix-3', 'prefix-');
expect(cacheMock.has('prefix-1')).toBe(false);
expect(cacheMock.has('prefix-2')).toBe(false);
expect(cacheMock.has('prefix-3')).toBe(true);
});
it('should keep the SWR_CACHE_SIZE entries and delete the rest', () => {
const cacheMock = new Map();
cacheMock.set('prefix-1', {});
cacheMock.set('prefix-2', {});
cacheMock.set('prefix-3', {});
cacheMock.set('prefix-4', {});
clearCacheEntries(cacheMock, 'prefix-4', 'prefix-', 2);
expect(cacheMock.has('prefix-4')).toBe(true);
expect([...cacheMock.keys()].length).toBe(2);
});
it('should handle case when SWR_CACHE_SIZE is larger than number of entries', () => {
const cacheMock = new Map();
cacheMock.set('prefix-1', {});
cacheMock.set('prefix-2', {});
clearCacheEntries(cacheMock, 'prefix-2', 'prefix-', 5);
expect(cacheMock.has('prefix-1')).toBe(true);
expect(cacheMock.has('prefix-2')).toBe(true);
});
it('should not delete entries that do not match the prefix', () => {
const cacheMock = new Map();
cacheMock.set('prefix-1', {});
cacheMock.set('other-2', {});
cacheMock.set('prefix-3', {});
clearCacheEntries(cacheMock, 'prefix-3', 'prefix-', 2);
expect(cacheMock.has('prefix-1')).toBe(true);
expect(cacheMock.has('other-2')).toBe(true);
expect(cacheMock.has('prefix-3')).toBe(true);
});
});