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 multiSet to update #305

Merged
merged 2 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
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
17 changes: 13 additions & 4 deletions lib/Onyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const METHOD = {
SET: 'set',
MERGE: 'merge',
MERGE_COLLECTION: 'mergecollection',
MULTI_SET: 'multiset',
CLEAR: 'clear',
};

Expand Down Expand Up @@ -1268,16 +1269,21 @@ function mergeCollection(collectionKey, collection) {
/**
* Insert API responses and lifecycle data into Onyx
*
* @param {Array} data An array of objects with shape {onyxMethod: oneOf('set', 'merge', 'mergeCollection'), key: string, value: *}
* @param {Array} data An array of objects with shape {onyxMethod: oneOf('set', 'merge', 'mergeCollection', 'multiSet', 'clear'), key: string, value: *}
* @returns {Promise} resolves when all operations are complete
*/
function update(data) {
// First, validate the Onyx object is in the format we expect
_.each(data, ({onyxMethod, key}) => {
if (!_.contains([METHOD.CLEAR, METHOD.SET, METHOD.MERGE, METHOD.MERGE_COLLECTION], onyxMethod)) {
_.each(data, ({onyxMethod, key, value}) => {
if (!_.contains([METHOD.CLEAR, METHOD.SET, METHOD.MERGE, METHOD.MERGE_COLLECTION, METHOD.MULTI_SET], onyxMethod)) {
throw new Error(`Invalid onyxMethod ${onyxMethod} in Onyx update.`);
}
if (onyxMethod !== METHOD.CLEAR && !_.isString(key)) {
if (onyxMethod === METHOD.MULTI_SET) {
// For multiset, we just expect the value to be an object
if (!_.isObject(value) || _.isArray(value) || _.isFunction(value)) {
throw new Error('Invalid value provided in Onyx multiSet. Onyx multiSet value must be of type object.');
}
} else if (onyxMethod !== METHOD.CLEAR && !_.isString(key)) {
throw new Error(`Invalid ${typeof key} key provided in Onyx update. Onyx key must be of type string.`);
}
});
Expand All @@ -1296,6 +1302,9 @@ function update(data) {
case METHOD.MERGE_COLLECTION:
promises.push(() => mergeCollection(key, value));
break;
case METHOD.MULTI_SET:
promises.push(() => multiSet(value));
break;
case METHOD.CLEAR:
clearPromise = clear();
break;
Expand Down
72 changes: 70 additions & 2 deletions tests/unit/onyxTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,15 +494,15 @@ describe('Onyx', () => {
// Given the invalid data object with onyxMethod='multiSet'
const data = [
{onyxMethod: 'set', key: ONYX_KEYS.TEST_KEY, value: 'four'},
{onyxMethod: 'multiSet', key: ONYX_KEYS.OTHER_TEST, value: {test2: 'test2'}},
{onyxMethod: 'murge', key: ONYX_KEYS.OTHER_TEST, value: {test2: 'test2'}},
];

try {
// When we pass it to Onyx.update
Onyx.update(data);
} catch (error) {
// Then we should expect the error message below
expect(error.message).toEqual('Invalid onyxMethod multiSet in Onyx update.');
expect(error.message).toEqual('Invalid onyxMethod murge in Onyx update.');
Copy link
Contributor

Choose a reason for hiding this comment

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

murge?! 😂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha - yeah open to ideas for better names for a "wrong" onyxMethod.

}

try {
Expand All @@ -517,6 +517,74 @@ describe('Onyx', () => {
}
});

it('should properly set all keys provided in a multiSet called via update', () => {
const valuesReceived = {};
connectionID = Onyx.connect({
key: ONYX_KEYS.COLLECTION.TEST_KEY,
initWithStoredValues: false,
callback: (data, key) => valuesReceived[key] = data,
});

return Onyx.multiSet({
test_1: {
existingData: 'test',
},
test_2: {
existingData: 'test',
},
})
.then(() => Onyx.update([
{
onyxMethod: 'multiset',
value: {
test_1: {
ID: 123,
value: 'one',
},
test_2: {
ID: 234,
value: 'two',
},
},
},
]))
.then(() => {
expect(valuesReceived).toEqual({
test_1: {
ID: 123,
value: 'one',
},
test_2: {
ID: 234,
value: 'two',
},
});
});
});

it('should reject an improperly formatted multiset operation called via update', () => {
try {
Onyx.update([
{
onyxMethod: 'multiset',
value: [
{
ID: 123,
value: 'one',
},
{
ID: 234,
value: 'two',
},
],
},
]);
} catch (error) {
expect(error.message)
.toEqual('Invalid value provided in Onyx multiSet. Onyx multiSet value must be of type object.');
}
});

it('should return all collection keys as a single object when waitForCollectionCallback = true', () => {
const mockCallback = jest.fn();

Expand Down
Loading