Skip to content

Commit

Permalink
Merge pull request #31 from brafdlog/master
Browse files Browse the repository at this point in the history
Fix for bug in deepEquals
close #30
  • Loading branch information
gor181 authored Sep 24, 2016
2 parents e9b7c1e + 29412b3 commit 7a36b84
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
36 changes: 35 additions & 1 deletion src/__tests__/ChartTests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ChartComponent from "../Chart";
import ChartComponent from '../Chart';

describe('Chart re-rendering', () => {

Expand All @@ -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'}}});
Expand Down
11 changes: 8 additions & 3 deletions src/utils/deepEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 7a36b84

Please sign in to comment.