Skip to content

Commit 60de524

Browse files
authored
fix(core): fixed cache.clear() not working as expected (#13926)
* 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
1 parent 952f64b commit 60de524

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

packages/core/__tests__/Cache/StorageCacheCommon.test.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defaultConfig } from '../../src/Cache/constants';
33
import { StorageCacheCommon } from '../../src/Cache/StorageCacheCommon';
44
import { KeyValueStorageInterface } from '../../src/types';
55
import { ConsoleLogger } from '../../src/Logger';
6+
import { StorageCache } from '../../src/Cache/StorageCache';
67
import {
78
getByteLength,
89
getCurrentSizeKey,
@@ -584,16 +585,20 @@ describe('StorageCacheCommon', () => {
584585
});
585586

586587
describe('clear()', () => {
587-
const cache = getStorageCache(config);
588+
const cache = new StorageCache(config);
588589

589590
it('clears the cache, including the currentSizeKey', async () => {
590-
mockGetAllCacheKeys.mockReturnValue([
591-
currentSizeKey,
592-
`${keyPrefix}some-key`,
593-
]);
591+
await cache.setItem('key1', 'value1');
592+
await cache.setItem('key2', 'value2');
593+
594+
expect(await cache.getItem('key1')).toBe('value1');
595+
expect(await cache.getItem('key2')).toBe('value2');
596+
594597
await cache.clear();
595-
expect(loggerSpy.debug).toHaveBeenCalledWith('Clear Cache');
596-
expect(mockKeyValueStorageRemoveItem).toHaveBeenCalledTimes(2);
598+
599+
expect(await cache.getItem('key1')).toBeNull();
600+
expect(await cache.getItem('key2')).toBeNull();
601+
expect(await cache.getCurrentCacheSize()).toBe(0);
597602
});
598603
});
599604

packages/core/src/Cache/StorageCacheCommon.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,8 @@ export abstract class StorageCacheCommon {
588588
try {
589589
const keys = await this.getAllKeys();
590590
for (const key of keys) {
591-
await this.getStorage().removeItem(key);
591+
const prefixedKey = `${this.config.keyPrefix}${key}`;
592+
await this.getStorage().removeItem(prefixedKey);
592593
}
593594
} catch (e) {
594595
logger.warn(`clear failed! ${e}`);

0 commit comments

Comments
 (0)