Skip to content

Commit 1e52be8

Browse files
committed
Fix bug in detecting props change - if the new props have an inner object/array with additional/less keys this was still considered equal
1 parent e9b7c1e commit 1e52be8

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

src/__tests__/ChartTests.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,62 @@ describe('Chart re-rendering', () => {
1414
expect(updateRequired).toBeTruthy();
1515
});
1616

17+
it('required when data is changed in an inner object/array of the data', () => {
18+
const originalData = {
19+
"data": {
20+
"labels": [
21+
1
22+
],
23+
"datasets": [
24+
{
25+
"label": "a",
26+
"backgroundColor": "#36A2EB",
27+
"data": [
28+
122968
29+
]
30+
},
31+
{
32+
"label": "b",
33+
"backgroundColor": "#FF6384",
34+
"data": [
35+
14738
36+
]
37+
}
38+
]
39+
},
40+
"type": "bar",
41+
"legend": {
42+
"display": true,
43+
"position": "bottom"
44+
}
45+
}
46+
// The new data has only one data set instead of two
47+
const newData = {
48+
"data": {
49+
"labels": [
50+
1
51+
],
52+
"datasets": [
53+
{
54+
"label": "a",
55+
"backgroundColor": "#36A2EB",
56+
"data": [
57+
122968
58+
]
59+
}
60+
]
61+
},
62+
"type": "bar",
63+
"legend": {
64+
"display": true,
65+
"position": "bottom"
66+
}
67+
}
68+
const chart = new ChartComponent(originalData);
69+
const updateRequired = chart.shouldComponentUpdate(newData);
70+
expect(updateRequired).toBeTruthy();
71+
});
72+
1773
it('required when chart options change', () => {
1874
const chart = new ChartComponent({type: 'bar', options: {hover: {mode: 'single'}}});
1975
const updateRequired = chart.shouldComponentUpdate({type: 'bar', options: {hover: {mode: 'label'}}});

src/utils/deepEqual.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ const deepEqual = (objA, objB) => {
2222
}
2323

2424
let keysA = Object.keys(objA);
25+
let keysB = Object.keys(objB);
26+
let allKeys = keysA.concat(keysB);
2527

26-
// Test for A's keys different from B.
27-
for (let i = 0; i < keysA.length; i++) {
28-
if (!hasOwnProperty.call(objB, keysA[i])) {
28+
// Verify both objects have all the keys
29+
for (let i = 0; i < allKeys.length; i++) {
30+
if (!hasOwnProperty.call(objB, allKeys[i])) {
31+
return false;
32+
}
33+
if (!hasOwnProperty.call(objA, allKeys[i])) {
2934
return false;
3035
}
3136
}

0 commit comments

Comments
 (0)