Skip to content

Commit 8bdc36a

Browse files
committed
test: cover non-empty to empty collection transition
1 parent 90c8957 commit 8bdc36a

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

tests/unit/onyxCacheTest.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,27 @@ describe('Onyx', () => {
625625
});
626626
});
627627

628+
it('should return the stable empty reference after all members are removed', async () => {
629+
await initOnyx();
630+
// Unrelated key keeps storageKeys non-empty so an empty collection is treated as "loaded and empty"
631+
await Onyx.set(ONYX_KEYS.TEST_KEY, 'value');
632+
await Onyx.set(`${ONYX_KEYS.COLLECTION.MOCK_COLLECTION}1`, {id: 1});
633+
634+
const populated = cache.getCollectionData(ONYX_KEYS.COLLECTION.MOCK_COLLECTION);
635+
expect(Object.keys(populated!)).toHaveLength(1);
636+
637+
// Remove the last member — the collection becomes empty
638+
await Onyx.set(`${ONYX_KEYS.COLLECTION.MOCK_COLLECTION}1`, null);
639+
640+
const first = cache.getCollectionData(ONYX_KEYS.COLLECTION.MOCK_COLLECTION);
641+
const second = cache.getCollectionData(ONYX_KEYS.COLLECTION.MOCK_COLLECTION);
642+
643+
expect(first).toBeDefined();
644+
expect(Object.keys(first!)).toHaveLength(0);
645+
// Reads must return the same empty reference so useSyncExternalStore doesn't re-render
646+
expect(first).toBe(second);
647+
});
648+
628649
it('should preserve unchanged member references when a sibling is updated', async () => {
629650
await initOnyx();
630651
const member1Value = {id: 1, name: 'unchanged'};

0 commit comments

Comments
 (0)