From d0344c4985f169b46ad105c229107e93959575d5 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 8 Aug 2022 15:12:36 -0600 Subject: [PATCH 1/4] add waitForCollectionCallback to keysChanged --- lib/Onyx.js | 6 ++++++ tests/unit/onyxTest.js | 32 ++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/Onyx.js b/lib/Onyx.js index 0e9ee4775..f5e105c0c 100644 --- a/lib/Onyx.js +++ b/lib/Onyx.js @@ -260,6 +260,12 @@ function keysChanged(collectionKey, collection) { if (isSubscribedToCollectionKey) { if (_.isFunction(subscriber.callback)) { const cachedCollection = getCachedCollection(collectionKey); + + if (subscriber.waitForCollectionCallback) { + subscriber.callback(cachedCollection); + return; + } + _.each(collection, (data, dataKey) => { subscriber.callback(cachedCollection[dataKey], dataKey); }); diff --git a/tests/unit/onyxTest.js b/tests/unit/onyxTest.js index 12b49b364..caa92ef8f 100644 --- a/tests/unit/onyxTest.js +++ b/tests/unit/onyxTest.js @@ -473,8 +473,8 @@ describe('Onyx', () => { const valuesReceived = {}; const mockCallback = jest.fn(data => valuesReceived[data.ID] = data.value); - // GIVEN some collection data - const collectionData = { + // GIVEN some initial collection data + const initialCollectionData = { test_connect_collection_1: { ID: 123, value: 'one', @@ -488,11 +488,18 @@ describe('Onyx', () => { value: 'three', }, }; - Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, collectionData); + + // AND an API response for that collection + const apiResponse = { + ...initialCollectionData, + test_connect_collection_4: {ID: 123, value: 'four'}, + }; + + Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, initialCollectionData); return waitForPromisesToResolve() .then(() => { - // WHEN we call Onyx.connect with the waitForCollectionCallback = true param - connectionID = Onyx.connect({ + // WHEN we connect to that collection with waitForCollectionCallback = true + Onyx.connect({ key: ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, waitForCollectionCallback: true, callback: mockCallback, @@ -500,11 +507,20 @@ describe('Onyx', () => { return waitForPromisesToResolve(); }) .then(() => { - // THEN the callback should be triggered only once + // THEN we expect the callback to be called only once and the initial stored value to be initialCollectionData expect(mockCallback.mock.calls.length).toBe(1); + expect(mockCallback.mock.calls[0][0]).toEqual(initialCollectionData); + + // WHEN we update the collection, e.g. the API returns a response + Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, apiResponse); + return waitForPromisesToResolve(); + }) + .then(() => { + // THEN we expect the callback to have called twice, once for the initial connect call and once for the API response + expect(mockCallback.mock.calls.length).toBe(2); - // AND all the collection data should be returned as a single object - expect(mockCallback.mock.calls[0][0]).toEqual(collectionData); + // AND all the updated collection data should be returned as a single object + expect(mockCallback.mock.calls[1][0]).toEqual(apiResponse); }); }); }); From b348a7d7b448c02d6994f31feccb208db4049070 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 8 Aug 2022 15:42:54 -0600 Subject: [PATCH 2/4] split tests --- tests/unit/onyxTest.js | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/tests/unit/onyxTest.js b/tests/unit/onyxTest.js index caa92ef8f..d76973ede 100644 --- a/tests/unit/onyxTest.js +++ b/tests/unit/onyxTest.js @@ -489,12 +489,6 @@ describe('Onyx', () => { }, }; - // AND an API response for that collection - const apiResponse = { - ...initialCollectionData, - test_connect_collection_4: {ID: 123, value: 'four'}, - }; - Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, initialCollectionData); return waitForPromisesToResolve() .then(() => { @@ -510,17 +504,34 @@ describe('Onyx', () => { // THEN we expect the callback to be called only once and the initial stored value to be initialCollectionData expect(mockCallback.mock.calls.length).toBe(1); expect(mockCallback.mock.calls[0][0]).toEqual(initialCollectionData); + }); + }); + it('should return all collection keys as a single object when updating a collection key with waitForCollectionCallback = true', () => { + const valuesReceived = {}; + const mockCallback = jest.fn(data => valuesReceived[data.ID] = data.value); + const collectionUpdate = { + test_connect_collection_3: {ID: 234, value: 'three'}, + test_connect_collection_4: {ID: 123, value: 'four'}, + }; + + // GIVEN an Onyx.connect call with waitForCollectionCallback=true + Onyx.connect({ + key: ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, + waitForCollectionCallback: true, + callback: mockCallback, + }); + return waitForPromisesToResolve() + .then(() => { // WHEN we update the collection, e.g. the API returns a response - Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, apiResponse); + Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, collectionUpdate); return waitForPromisesToResolve(); }) .then(() => { - // THEN we expect the callback to have called twice, once for the initial connect call and once for the API response + // THEN we expect the callback to have called twice (once for the initial connect call and once for the collection update) + // AND the value should be equal to collectionUpdate expect(mockCallback.mock.calls.length).toBe(2); - - // AND all the updated collection data should be returned as a single object - expect(mockCallback.mock.calls[1][0]).toEqual(apiResponse); + expect(mockCallback.mock.calls[1][0]).toEqual(collectionUpdate); }); }); }); From 1108a06a2753e3567df4d71029f1c44aba80bddd Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 8 Aug 2022 16:47:30 -0600 Subject: [PATCH 3/4] update tests --- tests/unit/onyxTest.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/tests/unit/onyxTest.js b/tests/unit/onyxTest.js index d76973ede..f90ca46a5 100644 --- a/tests/unit/onyxTest.js +++ b/tests/unit/onyxTest.js @@ -7,6 +7,7 @@ const ONYX_KEYS = { COLLECTION: { TEST_KEY: 'test_', TEST_CONNECT_COLLECTION: 'test_connect_collection_', + TEST_POLICY: 'test_policy_', }, }; @@ -470,8 +471,7 @@ describe('Onyx', () => { }); it('should return all collection keys as a single object when waitForCollectionCallback = true', () => { - const valuesReceived = {}; - const mockCallback = jest.fn(data => valuesReceived[data.ID] = data.value); + const mockCallback = jest.fn(); // GIVEN some initial collection data const initialCollectionData = { @@ -493,7 +493,7 @@ describe('Onyx', () => { return waitForPromisesToResolve() .then(() => { // WHEN we connect to that collection with waitForCollectionCallback = true - Onyx.connect({ + connectionID = Onyx.connect({ key: ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, waitForCollectionCallback: true, callback: mockCallback, @@ -508,29 +508,32 @@ describe('Onyx', () => { }); it('should return all collection keys as a single object when updating a collection key with waitForCollectionCallback = true', () => { - const valuesReceived = {}; - const mockCallback = jest.fn(data => valuesReceived[data.ID] = data.value); + const mockCallback = jest.fn(); const collectionUpdate = { - test_connect_collection_3: {ID: 234, value: 'three'}, - test_connect_collection_4: {ID: 123, value: 'four'}, + test_policy_1: {ID: 234, value: 'one'}, + test_policy_2: {ID: 123, value: 'two'}, }; // GIVEN an Onyx.connect call with waitForCollectionCallback=true - Onyx.connect({ - key: ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, + connectionID = Onyx.connect({ + key: ONYX_KEYS.COLLECTION.TEST_POLICY, waitForCollectionCallback: true, callback: mockCallback, }); return waitForPromisesToResolve() .then(() => { // WHEN we update the collection, e.g. the API returns a response - Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, collectionUpdate); + Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_POLICY, collectionUpdate); return waitForPromisesToResolve(); }) .then(() => { - // THEN we expect the callback to have called twice (once for the initial connect call and once for the collection update) - // AND the value should be equal to collectionUpdate + // THEN we expect the callback to have called twice, once for the initial connect call + once for the collection update expect(mockCallback.mock.calls.length).toBe(2); + + // AND the value for the first call should be null since the collection was not initialized at that point + expect(mockCallback.mock.calls[0][0]).toBe(null); + + // AND the value for the second call should be collectionUpdate since the collection was updated expect(mockCallback.mock.calls[1][0]).toEqual(collectionUpdate); }); }); From 2eb3d74e7b581007d7c4dcc332b5bbc027145ce2 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Tue, 9 Aug 2022 09:50:16 -0600 Subject: [PATCH 4/4] update comment --- tests/unit/onyxTest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/onyxTest.js b/tests/unit/onyxTest.js index f90ca46a5..661492ccf 100644 --- a/tests/unit/onyxTest.js +++ b/tests/unit/onyxTest.js @@ -522,7 +522,7 @@ describe('Onyx', () => { }); return waitForPromisesToResolve() .then(() => { - // WHEN we update the collection, e.g. the API returns a response + // WHEN mergeCollection is called with an updated collection Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_POLICY, collectionUpdate); return waitForPromisesToResolve(); })