Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add waitForCollectionCallback to keysChanged #167

Merged
merged 4 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/Onyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
43 changes: 35 additions & 8 deletions tests/unit/onyxTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -488,23 +488,50 @@ describe('Onyx', () => {
value: 'three',
},
};
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, collectionData);

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,
});
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);
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
});
});

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);
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
const collectionUpdate = {
test_connect_collection_3: {ID: 234, value: 'three'},
test_connect_collection_4: {ID: 123, value: 'four'},
};

// AND all the collection data should be returned as a single object
expect(mockCallback.mock.calls[0][0]).toEqual(collectionData);
// 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
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
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 collection update)
// AND the value should be equal to collectionUpdate
expect(mockCallback.mock.calls.length).toBe(2);
expect(mockCallback.mock.calls[1][0]).toEqual(collectionUpdate);
});
});
});