Skip to content
Closed
Changes from all 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
20 changes: 20 additions & 0 deletions tests/unit/withOnyxTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,23 @@ describe('withOnyx', () => {
});
});
});

describe('withOnyx', () => {
it('should update withOnyx subscribing to individual key with merged value if mergeCollection is used', () => {
const collectionItemID = 4;
const TestComponentWithOnyx = withOnyx({
text: {
key: `${ONYX_KEYS.COLLECTION.TEST_KEY}${collectionItemID}`,
},
})(ViewWithCollections);
const onRender = jest.fn();
render(<TestComponentWithOnyx onRender={onRender} />);
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {test_4: {ID: 456}, test_5: {ID: 567}});
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {test_4: {Name: 'Test4'}, test_5: {Name: 'Test5'}, test_6: {ID: 678, Name: 'Test6'}});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see these two calls happening here and here.

This makes sense to me since mergeCollection() does not work the same way as merge so the assumption to treat the data the same was incorrect on my part. However, I'm unsure what would be the best way forward since Onyx.merge() does not require that we use prevState and the mechanism is a bit different.

See here:

/**
* Given an Onyx key and value this method will combine all queued
* value updates and return a single value. Merge attempts are
* batched. They must occur after a single call to get() so we
* can avoid race conditions.
*
* @param {String} key
* @param {*} data
*
* @returns {*}
*/
function applyMerge(key, data) {
const mergeValues = mergeQueue[key];
if (_.isArray(data) || _.every(mergeValues, _.isArray)) {
// Array values will always just concatenate
// more items onto the end of the array
return _.reduce(mergeValues, (modifiedData, mergeValue) => [
...modifiedData,
...mergeValue,
], data || []);
}
if (_.isObject(data) || _.every(mergeValues, _.isObject)) {
// Object values are merged one after the other
return _.reduce(mergeValues, (modifiedData, mergeValue) => {
const newData = lodashMerge({}, modifiedData, mergeValue);
// We will also delete any object keys that are undefined or null.
// Deleting keys is not supported by AsyncStorage so we do it this way.
// Remove all first level keys that are explicitly set to null.
return _.omit(newData, (value, finalObjectKey) => _.isNull(mergeValue[finalObjectKey]));
}, data || {});
}
// If we have anything else we can't merge it so we'll
// simply return the last value that was queued
return _.last(mergeValues);
}
/**
* Merge a new value into an existing value at a key
*
* @param {string} key
* @param {*} val
*/
function merge(key, val) {
if (mergeQueue[key]) {
mergeQueue[key].push(val);
return;
}
mergeQueue[key] = [val];
get(key)
.then((data) => {
const modifiedData = applyMerge(key, data);
// Clean up the write queue so we
// don't apply these changes again
delete mergeQueue[key];
set(key, modifiedData);
});
}

The merge is batched and uses the data in localStorage as the source of truth so that when we call updateIndividualSubscriber() we will merge them together and then set the whole thing to state.

Probably the answer is to treat mergeCollection() similar to merge()? Another idea is that we can only use prevState when using mergeCollection(). Open to suggestions on the best way forward.

return waitForPromisesToResolve()
.then(() => {
expect(onRender.mock.calls.length).toBe(3);
expect(onRender.mock.instances[2].text).toEqual({ID: 456, Name: 'Test4'});
});
});
});