Skip to content

Commit

Permalink
Add RexExp support to hasClass
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 3e0f867
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/MountedTraversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ export function instHasClassName(inst, className) {
return ` ${classes} `.indexOf(` ${className} `) > -1;
}

export function instMatchClassName(inst, regex) {
if (!isDOMComponent(inst)) {
return false;
}
const classes = findDOMNode(inst).className || '';
return regex.test(classes);
}

export function instHasId(inst, id) {
if (!isDOMComponent(inst)) return false;
const instId = findDOMNode(inst).id || '';
Expand Down
4 changes: 4 additions & 0 deletions src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import compact from 'lodash/compact';
import createWrapperComponent from './ReactWrapperComponent';
import {
instHasClassName,
instMatchClassName,
childrenOfInst,
parentsOfInst,
buildInstPredicate,
Expand Down Expand Up @@ -525,6 +526,9 @@ export default class ReactWrapper {
* @returns {Boolean}
*/
hasClass(className) {
if (className instanceof RegExp) {
return this.single(n => instMatchClassName(n, className));
}
if (className && className.indexOf('.') !== -1) {
console.log(
'It looks like you\'re calling `ReactWrapper::hasClass()` with a CSS selector. ' +
Expand Down
5 changes: 5 additions & 0 deletions src/ShallowTraversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export function hasClassName(node, className) {
return ` ${classes} `.indexOf(` ${className} `) > -1;
}

export function matchClassName(node, regex) {
const classes = propsOfNode(node).className || '';
return regex.test(classes);
}

export function treeForEach(tree, fn) {
if (tree !== null && tree !== false && typeof tree !== 'undefined') {
fn(tree);
Expand Down
4 changes: 4 additions & 0 deletions src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import {
getTextFromNode,
hasClassName,
matchClassName,
childrenOfNode,
parentsOfNode,
treeFilter,
Expand Down Expand Up @@ -513,6 +514,9 @@ export default class ShallowWrapper {
* @returns {Boolean}
*/
hasClass(className) {
if (className instanceof RegExp) {
return this.single(n => matchClassName(n, className));
}
if (className && className.indexOf('.') !== -1) {
console.warn(
'It looks like you\'re calling `ShallowWrapper::hasClass()` with a CSS selector. ' +
Expand Down
15 changes: 15 additions & 0 deletions test/ReactWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,21 @@ describeWithDOM('mount', () => {
expect(wrapper.hasClass('FoOo')).to.equal(true);
expect(wrapper.hasClass('doesnt-exist')).to.equal(false);
});

it('should match class names with a regular expression', () => {
const wrapper = mount(
<div className="foo bar baz some-long-string FoOo bling-42" />
);

expect(wrapper.hasClass(/foo/)).to.equal(true);
expect(wrapper.hasClass(/bar/)).to.equal(true);
expect(wrapper.hasClass(/baz/)).to.equal(true);
expect(wrapper.hasClass(/some-long-string/)).to.equal(true);
expect(wrapper.hasClass(/FoOo/)).to.equal(true);
expect(wrapper.hasClass(/bling/)).to.equal(true);
expect(wrapper.hasClass(/doesnt-exist/)).to.equal(false);
});

});

describe('.forEach(fn)', () => {
Expand Down
14 changes: 14 additions & 0 deletions test/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,20 @@ describe('shallow', () => {
expect(wrapper.hasClass('FoOo')).to.equal(true);
expect(wrapper.hasClass('doesnt-exist')).to.equal(false);
});

it('should match class names with a regular expression', () => {
const wrapper = shallow(
<div className="foo bar baz some-long-string FoOo bling-42" />
);

expect(wrapper.hasClass(/foo/)).to.equal(true);
expect(wrapper.hasClass(/bar/)).to.equal(true);
expect(wrapper.hasClass(/baz/)).to.equal(true);
expect(wrapper.hasClass(/some-long-string/)).to.equal(true);
expect(wrapper.hasClass(/FoOo/)).to.equal(true);
expect(wrapper.hasClass(/bling/)).to.equal(true);
expect(wrapper.hasClass(/doesnt-exist/)).to.equal(false);
});
});

describe('.forEach(fn)', () => {
Expand Down

0 comments on commit 3e0f867

Please sign in to comment.