Skip to content

Commit 62557ea

Browse files
fix cacheEvictionTest
1 parent cc6fcbf commit 62557ea

2 files changed

Lines changed: 11 additions & 11 deletions

File tree

lib/OnyxCache.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import bindAll from 'lodash/bindAll';
33
import type {ValueOf} from 'type-fest';
44
import utils from './utils';
55
import type {OnyxKey, OnyxValue} from './types';
6+
import Storage from './storage';
67

78
// Task constants
89
const TASK = {
@@ -356,12 +357,13 @@ class OnyxCache {
356357
* removed.
357358
*/
358359
addAllSafeEvictionKeysToRecentlyAccessedList(isCollectionKeyFn: (key: OnyxKey) => boolean): Promise<void> {
359-
return Promise.resolve().then(() => {
360+
return Storage.getAllKeys().then((keys: string[]) => {
360361
this.evictionAllowList.forEach((safeEvictionKey) => {
361-
this.storageKeys.forEach((key) => {
362+
keys.forEach((key: string) => {
362363
if (!this.isKeyMatch(safeEvictionKey, key)) {
363364
return;
364365
}
366+
365367
this.addLastAccessedKey(key, isCollectionKeyFn(key));
366368
});
367369
});

tests/unit/cacheEvictionTest.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,12 @@ test('Cache eviction', () => {
5050
);
5151
StorageMock.setItem = setItemMock;
5252

53-
return Onyx.set(`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_ADD}`, {test: 'add'}).then(() => {
54-
// When storage is full, eviction will typically remove test_add first, then add it back,
55-
// which avoids removing test_evict. For this test, we'll manually handle the eviction.
56-
delete collection[`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_EVICT}`];
57-
58-
// Then our collection should no longer contain the evictable key
59-
expect(collection[`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_EVICT}`]).toBe(undefined);
60-
expect(collection[`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_ADD}`]).toStrictEqual({test: 'add'});
61-
});
53+
return Onyx.set(`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_ADD}`, {test: 'add'})
54+
.then(() => waitForPromisesToResolve())
55+
.then(() => {
56+
// Then our collection should no longer contain the evictable key
57+
expect(collection[`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_EVICT}`]).toBe(undefined);
58+
expect(collection[`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_ADD}`]).toStrictEqual({test: 'add'});
59+
});
6260
});
6361
});

0 commit comments

Comments
 (0)