Skip to content

Create a test case for mergecollection value check - #60

Closed
tugbadogan wants to merge 2 commits into
Expensify:masterfrom
tugbadogan:tugbadogan-onyx-mergecollection-bug
Closed

tugbadogan wants to merge 2 commits into
Expensify:masterfrom
tugbadogan:tugbadogan-onyx-mergecollection-bug

Conversation

@tugbadogan

Copy link
Copy Markdown

Related issue: Expensify/App#2225

Created a new test case for mergeCollection function to see if it merges values as expected. This test will fail.

@tugbadogan
tugbadogan requested a review from a team as a code owner April 6, 2021 00:24
@github-actions

github-actions Bot commented Apr 6, 2021 •

Copy link
Copy Markdown
Contributor

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@MelvinBot
MelvinBot requested review from yuwenmemon and removed request for a team April 6, 2021 00:25
@tugbadogan

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

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.

@marcaaron

Copy link
Copy Markdown
Contributor

Hey @tugbadogan we're on somewhat of a tight deadline with this so I'm going to create the PR we will get you a bonus for catching this + helping write the test (which I'll include in the PR I'm drafting now). Thanks again!

@marcaaron marcaaron closed this Apr 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants