Skip to content

Commit

Permalink
fix(core): fixed cache.clear() not working as expected (#13926)
Browse files Browse the repository at this point in the history
* fix: fixed cache.clear() not working as epected

* fix: removed mockResolve function in unit test for cache clear

* feat: update unit test to use real implementation instead of mockValues
  • Loading branch information
yuhengshs authored Oct 16, 2024
1 parent 952f64b commit 60de524
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
19 changes: 12 additions & 7 deletions packages/core/__tests__/Cache/StorageCacheCommon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defaultConfig } from '../../src/Cache/constants';
import { StorageCacheCommon } from '../../src/Cache/StorageCacheCommon';
import { KeyValueStorageInterface } from '../../src/types';
import { ConsoleLogger } from '../../src/Logger';
import { StorageCache } from '../../src/Cache/StorageCache';
import {
getByteLength,
getCurrentSizeKey,
Expand Down Expand Up @@ -584,16 +585,20 @@ describe('StorageCacheCommon', () => {
});

describe('clear()', () => {
const cache = getStorageCache(config);
const cache = new StorageCache(config);

it('clears the cache, including the currentSizeKey', async () => {
mockGetAllCacheKeys.mockReturnValue([
currentSizeKey,
`${keyPrefix}some-key`,
]);
await cache.setItem('key1', 'value1');
await cache.setItem('key2', 'value2');

expect(await cache.getItem('key1')).toBe('value1');
expect(await cache.getItem('key2')).toBe('value2');

await cache.clear();
expect(loggerSpy.debug).toHaveBeenCalledWith('Clear Cache');
expect(mockKeyValueStorageRemoveItem).toHaveBeenCalledTimes(2);

expect(await cache.getItem('key1')).toBeNull();
expect(await cache.getItem('key2')).toBeNull();
expect(await cache.getCurrentCacheSize()).toBe(0);
});
});

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/Cache/StorageCacheCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,8 @@ export abstract class StorageCacheCommon {
try {
const keys = await this.getAllKeys();
for (const key of keys) {
await this.getStorage().removeItem(key);
const prefixedKey = `${this.config.keyPrefix}${key}`;
await this.getStorage().removeItem(prefixedKey);
}
} catch (e) {
logger.warn(`clear failed! ${e}`);
Expand Down

0 comments on commit 60de524

Please sign in to comment.