Skip to content

Commit

Permalink
Handle (in)significant whitespace in className
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Dail authored and Brandon Dail committed Apr 26, 2016
1 parent de3664f commit c64e776
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 @@ -51,7 +51,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 @@ -28,7 +28,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 @@ -900,6 +900,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 @@ -1007,6 +1007,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 c64e776

Please sign in to comment.