Skip to content

Commit

Permalink
Handle (in)significant whitespace in className (enzymejs#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
aweary authored and dustinsanders committed Apr 30, 2016
1 parent 9913d0f commit fdc060e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/MountedTraversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export function instHasClassName(inst, className) {
if (!isDOMComponent(inst)) {
return false;
}
const classes = findDOMNode(inst).className || '';
let classes = findDOMNode(inst).className || '';
classes = classes.replace(/\s/g, ' ');
return ` ${classes} `.indexOf(` ${className} `) > -1;
}

Expand Down
3 changes: 2 additions & 1 deletion src/ShallowTraversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export function childrenOfNode(node) {
}

export function hasClassName(node, className) {
const classes = propsOfNode(node).className || '';
let classes = propsOfNode(node).className || '';
classes = classes.replace(/\s/g, ' ');
return ` ${classes} `.indexOf(` ${className} `) > -1;
}

Expand Down
17 changes: 17 additions & 0 deletions test/ReactWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,23 @@ describeWithDOM('mount', () => {
expect(wrapper.is('.foo.bar')).to.equal(true);
});

it('should ignore insignificant whitespace', () => {
const className = `
foo
`;
const wrapper = mount(<div className={className} />);
expect(wrapper.is('.foo')).to.equal(true);
});

it('should handle all significant whitespace', () => {
const className = `foo
bar
baz`;
const wrapper = mount(<div className={className} />);
expect(wrapper.is('.foo.bar.baz')).to.equal(true);
});

it('should return false when selector does not match', () => {
const wrapper = mount(<div className="bar baz" />);
expect(wrapper.is('.foo')).to.equal(false);
Expand Down
16 changes: 16 additions & 0 deletions test/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,22 @@ describe('shallow', () => {
expect(wrapper.is('.foo.bar')).to.equal(true);
});

it('should ignore insignificant whitespace', () => {
const className = `foo
`;
const wrapper = shallow(<div className={className} />);
expect(wrapper.is('.foo')).to.equal(true);
});

it('should handle all significant whitespace', () => {
const className = `foo
bar
baz`;
const wrapper = shallow(<div className={className} />);
expect(wrapper.is('.foo.bar.baz')).to.equal(true);
});

it('should return false when selector does not match', () => {
const wrapper = shallow(<div className="bar baz" />);
expect(wrapper.is('.foo')).to.equal(false);
Expand Down

0 comments on commit fdc060e

Please sign in to comment.