Skip to content

Commit

Permalink
[Fix] shallow/mount: hasClass: avoid a crash with a non-string …
Browse files Browse the repository at this point in the history
…argument
  • Loading branch information
alvteren authored and ljharb committed Mar 18, 2019
1 parent 4196bae commit 1cf510d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
18 changes: 18 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Expand Up @@ -4780,6 +4780,24 @@ describeWithDOM('mount', () => {
expect(wrapper.hasClass('classB')).to.equal(false);
expect(wrapper.hasClass(String(obj))).to.equal(true);
});

it('allows hyphens', () => {
const wrapper = mount(<div className="foo-bar" />);
expect(wrapper.hasClass('foo-bar')).to.equal(true);
});

it('works if className has a function in toString property', () => {
function classes() {}
classes.toString = () => 'foo-bar';
const wrapper = mount(<div className={classes} />);
expect(wrapper.hasClass('foo-bar')).to.equal(true);
});

it('works if searching with a RegExp', () => {
const wrapper = mount(<div className="ComponentName-classname-123" />);
expect(wrapper.hasClass(/(ComponentName)-(classname)-(\d+)/)).to.equal(true);
expect(wrapper.hasClass(/(ComponentName)-(other)-(\d+)/)).to.equal(false);
});
});

describe('.forEach(fn)', () => {
Expand Down
18 changes: 18 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Expand Up @@ -4726,6 +4726,24 @@ describe('shallow', () => {
expect(wrapper.hasClass('classB')).to.equal(false);
expect(wrapper.hasClass(String(obj))).to.equal(true);
});

it('allows hyphens', () => {
const wrapper = shallow(<div className="foo-bar" />);
expect(wrapper.hasClass('foo-bar')).to.equal(true);
});

it('works if className has a function in toString property', () => {
function classes() {}
classes.toString = () => 'foo-bar';
const wrapper = shallow(<div className={classes} />);
expect(wrapper.hasClass('foo-bar')).to.equal(true);
});

it('works if searching with a RegExp', () => {
const wrapper = shallow(<div className="ComponentName-classname-123" />);
expect(wrapper.hasClass(/(ComponentName)-(classname)-(\d+)/)).to.equal(true);
expect(wrapper.hasClass(/(ComponentName)-(other)-(\d+)/)).to.equal(false);
});
});

describe('.forEach(fn)', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/src/ReactWrapper.js
Expand Up @@ -855,7 +855,7 @@ class ReactWrapper {
* @returns {Boolean}
*/
hasClass(className) {
if (className && className.indexOf('.') !== -1) {
if (typeof className === 'string' && className.indexOf('.') !== -1) {
// eslint-disable-next-line no-console
console.warn('It looks like you\'re calling `ReactWrapper::hasClass()` with a CSS selector. hasClass() expects a class name, not a CSS selector.');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/src/ShallowWrapper.js
Expand Up @@ -1253,7 +1253,7 @@ class ShallowWrapper {
* @returns {Boolean}
*/
hasClass(className) {
if (className && className.indexOf('.') !== -1) {
if (typeof className === 'string' && className.indexOf('.') !== -1) {
// eslint-disable-next-line no-console
console.warn('It looks like you\'re calling `ShallowWrapper::hasClass()` with a CSS selector. hasClass() expects a class name, not a CSS selector.');
}
Expand Down

0 comments on commit 1cf510d

Please sign in to comment.