diff --git a/lib/Onyx.js b/lib/Onyx.js index a2f14c29..2caa740e 100644 --- a/lib/Onyx.js +++ b/lib/Onyx.js @@ -1129,14 +1129,20 @@ function mergeCollection(collectionKey, collection) { } // Confirm all the collection keys belong to the same parent + let hasCollectionKeyCheckFailed = false; _.each(collection, (_data, dataKey) => { if (isKeyMatch(collectionKey, dataKey)) { return; } - - throw new Error(`Provided collection doesn't have all its data belonging to the same parent. CollectionKey: ${collectionKey}, DataKey: ${dataKey}`); + hasCollectionKeyCheckFailed = true; + Logger.logAlert(`Provided collection doesn't have all its data belonging to the same parent. CollectionKey: ${collectionKey}, DataKey: ${dataKey}`); }); + // Gracefully handle bad mergeCollection updates so it doesn't block the merge queue + if (hasCollectionKeyCheckFailed) { + return Promise.resolve(); + } + return getAllKeys() .then((persistedKeys) => { // Split to keys that exist in storage and keys that don't diff --git a/tests/unit/onyxTest.js b/tests/unit/onyxTest.js index cf269494..db4c429d 100644 --- a/tests/unit/onyxTest.js +++ b/tests/unit/onyxTest.js @@ -306,12 +306,18 @@ describe('Onyx', () => { }); }); - it('should throw error when a key not belonging to collection key is present in mergeCollection', () => { - try { - Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {test_1: {ID: 123}, notMyTest: {beep: 'boop'}}); - } catch (error) { - expect(error.message).toEqual(`Provided collection doesn't have all its data belonging to the same parent. CollectionKey: ${ONYX_KEYS.COLLECTION.TEST_KEY}, DataKey: notMyTest`); - } + it('should skip the update when a key not belonging to collection key is present in mergeCollection', () => { + const valuesReceived = {}; + connectionID = Onyx.connect({ + key: ONYX_KEYS.COLLECTION.TEST_KEY, + initWithStoredValues: false, + callback: (data, key) => valuesReceived[key] = data, + }); + + return Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {test_1: {ID: 123}, notMyTest: {beep: 'boop'}}) + .then(() => { + expect(valuesReceived).toEqual({}); + }); }); it('should return full object to callback when calling mergeCollection()', () => {