Skip to content

Commit

Permalink
Add multiSet to update
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwenmemon committed Aug 18, 2023
1 parent 79bfb5e commit db902e3
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 6 deletions.
24 changes: 20 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,28 @@ 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) {
if (!_.isObject(value) || _.isArray(value) || _.isFunction(value)) {
throw new Error(`Invalid value provided in Onyx multiSet. Onyx multiSet value must be of type object.`);

Check failure on line 1283 in lib/Onyx.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
}

_.each(_.keys(value), (multiSetKey) => {
if (_.isString(multiSetKey)) {
return;
}

throw new Error(`Invalid ${typeof key} key provided in Onyx multiSet. Onyx key must be of type string.`);
});
} 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 +1309,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
93 changes: 91 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.');
}

try {
Expand All @@ -517,6 +517,95 @@ 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.');
}

try {
Onyx.update([
{
onyxMethod: 'multiset',
value: {
0: {
ID: 123,
value: 'one',
},
1: {
ID: 234,
value: 'two',
},
},
},
]);
} catch (error) {
expect(error.message)
.toEqual('Invalid Number key provided in Onyx multiSet. Onyx key must be of type string.`');
}
});

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

Expand Down

0 comments on commit db902e3

Please sign in to comment.