Skip to content

Commit

Permalink
[New] mount/shallow: add regex support to .hasClass
Browse files Browse the repository at this point in the history
  • Loading branch information
stackchain authored and ljharb committed Jan 24, 2019
1 parent cbb4919 commit 20f1218
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
12 changes: 9 additions & 3 deletions docs/api/ReactWrapper/hasClass.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@ Returns whether or not the wrapped node has a `className` prop including the pas

#### Arguments

1. `className` (`String`): A single class name.
1. `className` (`String` | `RegExp`): A single class name or a regex expression.


#### Returns

`Boolean`: whether or not the wrapped node has the class.
`Boolean`: whether or not the wrapped node has found the class name.


#### Example
#### Examples


```jsx
const wrapper = mount(<MyComponent />);
expect(wrapper.find('.my-button').hasClass('disabled')).to.equal(true);
```

```jsx
// Searching using RegExp works fine when classes were injected by a jss decorator
const wrapper = mount(<MyComponent />);
expect(wrapper.find('.my-button').hasClass(/(ComponentName)-(other)-(\d+)/)).to.equal(true);
```

### Common Gotchas

- `.hasClass()` expects a class name, NOT a CSS selector. `.hasClass('.foo')` should be
Expand Down
8 changes: 7 additions & 1 deletion docs/api/ShallowWrapper/hasClass.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Returns whether or not the wrapped node has a `className` prop including the pas

#### Arguments

1. `className` (`String`): A single class name.
1. `className` (`String` | `RegExp`): A single class name or a regex expression.


#### Returns
Expand All @@ -21,6 +21,12 @@ const wrapper = shallow(<MyComponent />);
expect(wrapper.find('.my-button').hasClass('disabled')).to.equal(true);
```

```jsx
// Searching using RegExp works fine when classes were injected by a jss decorator
const wrapper = mount(<MyComponent />);
expect(wrapper.find('.my-button').hasClass(/(ComponentName)-(other)-(\d+)/)).to.equal(true);
```

### Common Gotchas

- `.hasClass()` expects a class name, NOT a CSS selector. `.hasClass('.foo')` should be
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme-test-suite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@
"eslint-plugin-react": "^7.12.4",
"react-is": "^16.7.0"
}
}
}
10 changes: 10 additions & 0 deletions packages/enzyme-test-suite/test/RSTTraversal-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ describe('RSTTraversal', () => {
const node = $(<div className={classes} />);
expect(hasClassName(node, 'foo-bar')).to.equal(true);
});

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

it('fails if searching for a missing classname with a RegExp', () => {
const node = $(<div className="ComponentName-classname-123 ComponentName-otherclassname-23" />);
expect(hasClassName(node, /(ComponentName)-(other)-(\d+)/)).to.equal(false);
});
});

describe('treeForEach', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/enzyme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"is-boolean-object": "^1.0.0",
"is-callable": "^1.1.4",
"is-number-object": "^1.0.3",
"is-regex": "^1.0.4",
"is-string": "^1.0.4",
"is-subset": "^0.1.1",
"lodash.escape": "^4.0.1",
Expand Down
2 changes: 2 additions & 0 deletions packages/enzyme/src/RSTTraversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import flat from 'array.prototype.flat';
import entries from 'object.entries';
import isSubset from 'is-subset';
import functionName from 'function.prototype.name';
import isRegex from 'is-regex';
import getAdapter from './getAdapter';

export function propsOfNode(node) {
Expand Down Expand Up @@ -34,6 +35,7 @@ export function childrenOfNode(node) {
export function hasClassName(node, className) {
let classes = propsOfNode(node).className || '';
classes = String(classes).replace(/\s/g, ' ');
if (isRegex(className)) return className.test(classes);
return ` ${classes} `.indexOf(` ${className} `) > -1;
}

Expand Down

0 comments on commit 20f1218

Please sign in to comment.