diff --git a/src/__tests__/ChartTests.js b/src/__tests__/ChartTests.js index ccbc4a7d1..65a6eb099 100644 --- a/src/__tests__/ChartTests.js +++ b/src/__tests__/ChartTests.js @@ -1,4 +1,4 @@ -import ChartComponent from "../Chart"; +import ChartComponent from '../Chart'; describe('Chart re-rendering', () => { @@ -14,6 +14,40 @@ describe('Chart re-rendering', () => { expect(updateRequired).toBeTruthy(); }); + it('required when data is changed in an inner object/array of the data', () => { + const originalData = { + 'data': { + 'datasets': [ + { + 'data': [ + 122968 + ] + }, + { + 'data': [ + 14738 + ] + } + ] + } + }; + // The new data has only one data set instead of two + const newData = { + 'data': { + 'datasets': [ + { + 'data': [ + 122968 + ] + } + ] + } + }; + const chart = new ChartComponent(originalData); + const updateRequired = chart.shouldComponentUpdate(newData); + expect(updateRequired).toBeTruthy(); + }); + it('required when chart options change', () => { const chart = new ChartComponent({type: 'bar', options: {hover: {mode: 'single'}}}); const updateRequired = chart.shouldComponentUpdate({type: 'bar', options: {hover: {mode: 'label'}}}); diff --git a/src/utils/deepEqual.js b/src/utils/deepEqual.js index 53410e66f..0caadfc0d 100644 --- a/src/utils/deepEqual.js +++ b/src/utils/deepEqual.js @@ -22,10 +22,15 @@ const deepEqual = (objA, objB) => { } let keysA = Object.keys(objA); + let keysB = Object.keys(objB); + let allKeys = keysA.concat(keysB); - // Test for A's keys different from B. - for (let i = 0; i < keysA.length; i++) { - if (!hasOwnProperty.call(objB, keysA[i])) { + // Verify both objects have all the keys + for (let i = 0; i < allKeys.length; i++) { + if (!hasOwnProperty.call(objB, allKeys[i])) { + return false; + } + if (!hasOwnProperty.call(objA, allKeys[i])) { return false; } }