Skip to content

Commit e151a91

Browse files
authored
Merge pull request #167 from Expensify/cmartins-fixWaitForCollectionCallack
Add waitForCollectionCallback to keysChanged
2 parents a674178 + 2eb3d74 commit e151a91

2 files changed

Lines changed: 45 additions & 9 deletions

File tree

lib/Onyx.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,12 @@ function keysChanged(collectionKey, collection) {
260260
if (isSubscribedToCollectionKey) {
261261
if (_.isFunction(subscriber.callback)) {
262262
const cachedCollection = getCachedCollection(collectionKey);
263+
264+
if (subscriber.waitForCollectionCallback) {
265+
subscriber.callback(cachedCollection);
266+
return;
267+
}
268+
263269
_.each(collection, (data, dataKey) => {
264270
subscriber.callback(cachedCollection[dataKey], dataKey);
265271
});

tests/unit/onyxTest.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const ONYX_KEYS = {
77
COLLECTION: {
88
TEST_KEY: 'test_',
99
TEST_CONNECT_COLLECTION: 'test_connect_collection_',
10+
TEST_POLICY: 'test_policy_',
1011
},
1112
};
1213

@@ -470,11 +471,10 @@ describe('Onyx', () => {
470471
});
471472

472473
it('should return all collection keys as a single object when waitForCollectionCallback = true', () => {
473-
const valuesReceived = {};
474-
const mockCallback = jest.fn(data => valuesReceived[data.ID] = data.value);
474+
const mockCallback = jest.fn();
475475

476-
// GIVEN some collection data
477-
const collectionData = {
476+
// GIVEN some initial collection data
477+
const initialCollectionData = {
478478
test_connect_collection_1: {
479479
ID: 123,
480480
value: 'one',
@@ -488,10 +488,11 @@ describe('Onyx', () => {
488488
value: 'three',
489489
},
490490
};
491-
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, collectionData);
491+
492+
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, initialCollectionData);
492493
return waitForPromisesToResolve()
493494
.then(() => {
494-
// WHEN we call Onyx.connect with the waitForCollectionCallback = true param
495+
// WHEN we connect to that collection with waitForCollectionCallback = true
495496
connectionID = Onyx.connect({
496497
key: ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION,
497498
waitForCollectionCallback: true,
@@ -500,11 +501,40 @@ describe('Onyx', () => {
500501
return waitForPromisesToResolve();
501502
})
502503
.then(() => {
503-
// THEN the callback should be triggered only once
504+
// THEN we expect the callback to be called only once and the initial stored value to be initialCollectionData
504505
expect(mockCallback.mock.calls.length).toBe(1);
506+
expect(mockCallback.mock.calls[0][0]).toEqual(initialCollectionData);
507+
});
508+
});
509+
510+
it('should return all collection keys as a single object when updating a collection key with waitForCollectionCallback = true', () => {
511+
const mockCallback = jest.fn();
512+
const collectionUpdate = {
513+
test_policy_1: {ID: 234, value: 'one'},
514+
test_policy_2: {ID: 123, value: 'two'},
515+
};
516+
517+
// GIVEN an Onyx.connect call with waitForCollectionCallback=true
518+
connectionID = Onyx.connect({
519+
key: ONYX_KEYS.COLLECTION.TEST_POLICY,
520+
waitForCollectionCallback: true,
521+
callback: mockCallback,
522+
});
523+
return waitForPromisesToResolve()
524+
.then(() => {
525+
// WHEN mergeCollection is called with an updated collection
526+
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_POLICY, collectionUpdate);
527+
return waitForPromisesToResolve();
528+
})
529+
.then(() => {
530+
// THEN we expect the callback to have called twice, once for the initial connect call + once for the collection update
531+
expect(mockCallback.mock.calls.length).toBe(2);
532+
533+
// AND the value for the first call should be null since the collection was not initialized at that point
534+
expect(mockCallback.mock.calls[0][0]).toBe(null);
505535

506-
// AND all the collection data should be returned as a single object
507-
expect(mockCallback.mock.calls[0][0]).toEqual(collectionData);
536+
// AND the value for the second call should be collectionUpdate since the collection was updated
537+
expect(mockCallback.mock.calls[1][0]).toEqual(collectionUpdate);
508538
});
509539
});
510540
});

0 commit comments

Comments
 (0)