Skip to content

Commit 5342c78

Browse files
authored
Merge pull request Expensify#720 from margelo/@chrispader/fix-collection-key-expected-matched-on-collection-callback
fix: Send collection key as `matchedKey` in collection callbacks
2 parents 2ae6062 + 70006e2 commit 5342c78

4 files changed

Lines changed: 12 additions & 10 deletions

File tree

lib/OnyxUtils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ function addKeyToRecentlyAccessedIfNeeded<TKey extends OnyxKey>(key: TKey): void
847847
function getCollectionDataAndSendAsObject<TKey extends OnyxKey>(matchingKeys: CollectionKeyBase[], mapping: CallbackToStateMapping<TKey>): void {
848848
multiGet(matchingKeys).then((dataMap) => {
849849
const data = Object.fromEntries(dataMap.entries()) as OnyxValue<TKey>;
850-
sendDataToConnection(mapping, data, undefined);
850+
sendDataToConnection(mapping, data, mapping.key);
851851
});
852852
}
853853

@@ -1173,9 +1173,11 @@ function subscribeToKey<TKey extends OnyxKey>(connectOptions: ConnectOptions<TKe
11731173
cache.addNullishStorageKey(mapping.key);
11741174
}
11751175

1176+
const matchedKey = isCollectionKey(mapping.key) && mapping.waitForCollectionCallback ? mapping.key : undefined;
1177+
11761178
// Here we cannot use batching because the nullish value is expected to be set immediately for default props
11771179
// or they will be undefined.
1178-
sendDataToConnection(mapping, null, undefined);
1180+
sendDataToConnection(mapping, null, matchedKey);
11791181
return;
11801182
}
11811183

tests/unit/OnyxConnectionManagerTest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ describe('OnyxConnectionManager', () => {
144144
expect(callback1).toHaveBeenNthCalledWith(2, obj2, `${ONYXKEYS.COLLECTION.TEST_KEY}entry2`);
145145

146146
expect(callback2).toHaveBeenCalledTimes(1);
147-
expect(callback2).toHaveBeenCalledWith(collection, undefined, undefined);
147+
expect(callback2).toHaveBeenCalledWith(collection, ONYXKEYS.COLLECTION.TEST_KEY, undefined);
148148

149149
connectionManager.disconnect(connection1);
150150
connectionManager.disconnect(connection2);
@@ -483,7 +483,7 @@ describe('OnyxConnectionManager', () => {
483483

484484
// Initial callback with undefined values
485485
expect(callback).toHaveBeenCalledTimes(1);
486-
expect(callback).toHaveBeenCalledWith(undefined, undefined, undefined);
486+
expect(callback).toHaveBeenCalledWith(undefined, ONYXKEYS.COLLECTION.TEST_KEY, undefined);
487487

488488
// Reset mock to test the next update
489489
callback.mockReset();

tests/unit/onyxClearWebStorageTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ describe('Set data while storage is clearing', () => {
161161
expect(collectionCallback).toHaveBeenCalledTimes(3);
162162

163163
// And it should be called with the expected parameters each time
164-
expect(collectionCallback).toHaveBeenNthCalledWith(1, undefined, undefined, undefined);
164+
expect(collectionCallback).toHaveBeenNthCalledWith(1, undefined, ONYX_KEYS.COLLECTION.TEST, undefined);
165165
expect(collectionCallback).toHaveBeenNthCalledWith(
166166
2,
167167
{

tests/unit/onyxTest.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ describe('Onyx', () => {
10711071
.then(() => {
10721072
// Then we expect the callback to be called only once and the initial stored value to be initialCollectionData
10731073
expect(mockCallback).toHaveBeenCalledTimes(1);
1074-
expect(mockCallback).toHaveBeenCalledWith(initialCollectionData, undefined, undefined);
1074+
expect(mockCallback).toHaveBeenCalledWith(initialCollectionData, ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, undefined);
10751075
});
10761076
});
10771077

@@ -1097,7 +1097,7 @@ describe('Onyx', () => {
10971097
expect(mockCallback).toHaveBeenCalledTimes(2);
10981098

10991099
// AND the value for the first call should be null since the collection was not initialized at that point
1100-
expect(mockCallback).toHaveBeenNthCalledWith(1, undefined, undefined, undefined);
1100+
expect(mockCallback).toHaveBeenNthCalledWith(1, undefined, ONYX_KEYS.COLLECTION.TEST_POLICY, undefined);
11011101

11021102
// AND the value for the second call should be collectionUpdate since the collection was updated
11031103
expect(mockCallback).toHaveBeenNthCalledWith(2, collectionUpdate, ONYX_KEYS.COLLECTION.TEST_POLICY, collectionUpdate);
@@ -1155,7 +1155,7 @@ describe('Onyx', () => {
11551155
expect(mockCallback).toHaveBeenCalledTimes(2);
11561156

11571157
// AND the value for the second call should be collectionUpdate
1158-
expect(mockCallback).toHaveBeenNthCalledWith(1, undefined, undefined, undefined);
1158+
expect(mockCallback).toHaveBeenNthCalledWith(1, undefined, ONYX_KEYS.COLLECTION.TEST_POLICY, undefined);
11591159
expect(mockCallback).toHaveBeenNthCalledWith(2, collectionUpdate, ONYX_KEYS.COLLECTION.TEST_POLICY, {
11601160
[`${ONYX_KEYS.COLLECTION.TEST_POLICY}1`]: collectionUpdate.testPolicy_1,
11611161
});
@@ -1271,7 +1271,7 @@ describe('Onyx', () => {
12711271
{onyxMethod: Onyx.METHOD.MERGE_COLLECTION, key: ONYX_KEYS.COLLECTION.TEST_UPDATE, value: {[itemKey]: {a: 'a'}} as GenericCollection},
12721272
]).then(() => {
12731273
expect(collectionCallback).toHaveBeenCalledTimes(2);
1274-
expect(collectionCallback).toHaveBeenNthCalledWith(1, undefined, undefined, undefined);
1274+
expect(collectionCallback).toHaveBeenNthCalledWith(1, undefined, ONYX_KEYS.COLLECTION.TEST_UPDATE, undefined);
12751275
expect(collectionCallback).toHaveBeenNthCalledWith(2, {[itemKey]: {a: 'a'}}, ONYX_KEYS.COLLECTION.TEST_UPDATE, {[itemKey]: {a: 'a'}});
12761276

12771277
expect(testCallback).toHaveBeenCalledTimes(2);
@@ -1538,7 +1538,7 @@ describe('Onyx', () => {
15381538
.then(() => {
15391539
expect(collectionCallback).toHaveBeenCalledTimes(3);
15401540
expect(collectionCallback).toHaveBeenNthCalledWith(1, {[cat]: initialValue}, ONYX_KEYS.COLLECTION.ANIMALS, {[cat]: initialValue});
1541-
expect(collectionCallback).toHaveBeenNthCalledWith(2, {[cat]: initialValue}, undefined, undefined);
1541+
expect(collectionCallback).toHaveBeenNthCalledWith(2, {[cat]: initialValue}, ONYX_KEYS.COLLECTION.ANIMALS, undefined);
15421542
expect(collectionCallback).toHaveBeenNthCalledWith(3, collectionDiff, ONYX_KEYS.COLLECTION.ANIMALS, {[cat]: initialValue, [dog]: {name: 'Rex'}});
15431543

15441544
// Cat hasn't changed from its original value, expect only the initial connect callback

0 commit comments

Comments
 (0)