Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

equals should skip null and undefined nodes fix #151 #192

Merged
merged 1 commit into from
May 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,11 @@ export function nodeEqual(a, b, lenComp = is) {
const right = propsOfNode(b);
for (let i = 0; i < leftKeys.length; i++) {
const prop = leftKeys[i];
if (!(prop in right)) return false;
// we will check children later
if (prop === 'children') {
if (!childrenEqual(
childrenToArray(left.children),
childrenToArray(right.children),
lenComp)) {
return false;
}
// continue;
} else if (!(prop in right)) {
return false;
} else if (right[prop] === left[prop]) {
// continue;
} else if (typeof right[prop] === typeof left[prop] && typeof left[prop] === 'object') {
Expand All @@ -99,8 +96,20 @@ export function nodeEqual(a, b, lenComp = is) {
}
}

const leftHasChildren = 'children' in left;
const rightHasChildren = 'children' in right;
if (leftHasChildren || rightHasChildren) {
if (!childrenEqual(
childrenToArray(left.children),
childrenToArray(right.children),
lenComp)) {
return false;
}
}

if (!isTextualNode(a)) {
return lenComp(leftKeys.length, Object.keys(right).length);
const rightKeys = Object.keys(right);
return lenComp(leftKeys.length - leftHasChildren, rightKeys.length - rightHasChildren);
}

return false;
Expand Down
97 changes: 97 additions & 0 deletions test/Utils-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,103 @@ describe('Utils', () => {

});

describe('children props', () => {
it('should match equal nodes', () => {
expect(nodeEqual(
<div>child</div>,
<div>child</div>
)).to.equal(true);
});

it('should not match not equal nodes', () => {
expect(nodeEqual(
<div>child</div>,
<div></div>
)).to.equal(false);

expect(nodeEqual(
<div></div>,
<div>child</div>
)).to.equal(false);
});

it('should skip null children', () => {
expect(nodeEqual(
<div>{null}</div>,
<div></div>
)).to.equal(true);
});

it('should skip undefined children', () => {
expect(nodeEqual(
<div>{undefined}</div>,
<div></div>
)).to.equal(true);
});

it('should skip empty children', () => {
expect(nodeEqual(
<div>{[]}</div>,
<div></div>
)).to.equal(true);
});

it('should skip array of null children', () => {
expect(nodeEqual(
<div>{[null, null, null]}</div>,
<div></div>
)).to.equal(true);
});

});

describe('basic props and children mixed', () => {

it('should match equal nodes', () => {
expect(nodeEqual(
<div className="foo">child</div>,
<div className="foo">child</div>
)).to.equal(true);
});

it('should not match when basic props are not equal', () => {
expect(nodeEqual(
<div className="foo">child</div>,
<div className="bar">child</div>
)).to.equal(false);

expect(nodeEqual(
<div children="child" className="foo" />,
<div children="child" className="bar" />
)).to.equal(false);
});

it('should not match when children are not equal', () => {
expect(nodeEqual(
<div className="foo">child</div>,
<div className="foo">other child</div>
)).to.equal(false);

expect(nodeEqual(
<div children="child" className="foo" />,
<div children="other child" className="foo" />
)).to.equal(false);
});

it('should match nodes when children are different but falsy', () => {
expect(nodeEqual(
<div className="foo">{null}</div>,
<div className="foo" />
)).to.equal(true);

expect(nodeEqual(
<div children={null} className="foo" />,
<div className="foo" />
)).to.equal(true);
});

});

});

describe('propFromEvent', () => {
Expand Down