Skip to content

Commit f3f3f16

Browse files
authored
Merge pull request #163 from Expensify/cmartins-addConnectToCollection
Add waitForCollectionCallback param to Onyx.connect
2 parents 00dff90 + 4f1d050 commit f3f3f16

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

lib/Onyx.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ function sendDataToConnection(config, val, key) {
391391
* This is used by any non-React code to connect to Onyx
392392
* @param {Boolean} [mapping.initWithStoredValues] If set to false, then no data will be prefilled into the
393393
* component
394+
* @param {Boolean} [mapping.waitForCollectionCallback] If set to true, it will trigger the callback once and return all data as a single object
394395
* @returns {Number} an ID to use when calling disconnect
395396
*/
396397
function connect(mapping) {
@@ -441,7 +442,7 @@ function connect(mapping) {
441442
// to expect a single key or multiple keys in the case of a collection.
442443
// React components are an exception since we'll want to send their
443444
// initial data as a single object when using collection keys.
444-
if (mapping.withOnyxInstance && isCollectionKey(mapping.key)) {
445+
if ((mapping.withOnyxInstance && isCollectionKey(mapping.key)) || mapping.waitForCollectionCallback) {
445446
Promise.all(_.map(matchingKeys, key => get(key)))
446447
.then(values => _.reduce(values, (finalObject, value, i) => ({
447448
...finalObject,

tests/unit/onyxTest.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const ONYX_KEYS = {
66
OTHER_TEST: 'otherTest',
77
COLLECTION: {
88
TEST_KEY: 'test_',
9+
TEST_CONNECT_COLLECTION: 'test_connect_collection_',
910
},
1011
};
1112

@@ -467,4 +468,43 @@ describe('Onyx', () => {
467468
expect(error.message).toEqual('Invalid boolean key provided in Onyx update. Onyx key must be of type string.');
468469
}
469470
});
471+
472+
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);
475+
476+
// GIVEN some collection data
477+
const collectionData = {
478+
test_connect_collection_1: {
479+
ID: 123,
480+
value: 'one',
481+
},
482+
test_connect_collection_2: {
483+
ID: 234,
484+
value: 'two',
485+
},
486+
test_connect_collection_3: {
487+
ID: 345,
488+
value: 'three',
489+
},
490+
};
491+
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, collectionData);
492+
return waitForPromisesToResolve()
493+
.then(() => {
494+
// WHEN we call Onyx.connect with the waitForCollectionCallback = true param
495+
connectionID = Onyx.connect({
496+
key: ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION,
497+
waitForCollectionCallback: true,
498+
callback: mockCallback,
499+
});
500+
return waitForPromisesToResolve();
501+
})
502+
.then(() => {
503+
// THEN the callback should be triggered only once
504+
expect(mockCallback.mock.calls.length).toBe(1);
505+
506+
// AND all the collection data should be returned as a single object
507+
expect(mockCallback.mock.calls[0][0]).toEqual(collectionData);
508+
});
509+
});
470510
});

0 commit comments

Comments
 (0)