Jasmine assertions for enzyme
npm install jasmine-enzyme
!License
Quick Links
* Installation
* Setup
* Assertions
We suggest using yarn for installations.
```
yarn add jasmine-enzyme --dev
But npm works too!
``
$ npm install jasmine-enzyme --save-dev
For Jasmine, you'll need to call jasmineEnzyme() in any before method due to the way jasmine's plugin
system works.
`js
import jasmineEnzyme from 'jasmine-enzyme';
describe('test', () => {
beforeEach(() => {
jasmineEnzyme();
});
// tests
});
`
> * Not all assertions work with every rendering strategy.
> If you are wondering what rendering mechanism to use when, refer to
> enzyme's documentation.
* toBeChecked()
* toBeDisabled()
* toBeEmptyRender()
* toExist()
* toContainMatchingElement()
* toContainMatchingElements()
* toContainExactlyOneMatchingElement()
* toContainReact()
* toHaveClassName()
* toHaveDisplayName()
* toHaveHTML()
* toHaveProp()
* toHaveRef()
* toHaveState()
* toHaveStyle()
* toHaveTagName()
* toHaveText()
* toIncludeText()
* toHaveValue()
* toMatchElement()
* toMatchSelector()
#### toBeChecked()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toBeChecked();
Assert that the given wrapper is checked:
`js
import React from 'react'
import {mount, shallow} from 'enzyme'
function Fixture() {
return (
const wrapper = mount(
expect(wrapper.find('#checked')).toBeChecked();
expect(wrapper.find('#not')).not.toBeChecked();
`
#### toBeDisabled()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toBeDisabled();
Assert that the given wrapper is disabled:
`js
import React from 'react'
import {mount, shallow} from 'enzyme'
function Fixture() {
return (
const wrapper = mount(
expect(wrapper.find('#disabled')).toBeDisabled();
expect(wrapper.find('#not')).not.toBeDisabled();
`
#### toBeEmptyRender()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toBeEmptyRender();
Assert that the given wrapper has an empty render (null or false):
`js
function EmptyRenderFixture() {
return null;
}
function NonEmptyRenderFixture() {
return (
const wrapper = mount(
expect(wrapper.find('EmptyRenderFixture')).toBeEmptyRender();
expect(wrapper).not.toBeEmptyRender();
`
#### toExist()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toExist();
Assert that the given enzyme wrapper has rendered content.
`js
function Fixture() {
return (
);
}
const wrapper = mount(
expect(wrapper.find('span')).toExist();
expect(wrapper.find('ul')).not.toExist();
`
#### toContainMatchingElement()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toContainMatchingElement('.foo');
Assert that the given wrapper contains at least one match for the given selector:
`js
function User(props) {
return (
User {props.index}
);
}
User.propTypes = {
index: PropTypes.number.isRequired,
className: PropTypes.string,
};
function Fixture() {
return (
const wrapper = mount(
expect(wrapper).toContainMatchingElement('.userOne');
expect(wrapper).not.toContainMatchingElement('.userThree');
`
#### toContainMatchingElements()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toContainMatchingElements(2, '.foo');
Assert that the given wrapper contains a given number of matches for the given selector:
`js
function User(props) {
return (
User {props.index}
);
}
User.propTypes = {
index: PropTypes.number.isRequired,
className: PropTypes.string,
};
function Fixture() {
return (
const wrapper = mount(
expect(wrapper).toContainMatchingElements(2, 'User');
expect(wrapper).not.toContainMatchingElements(2, '.userTwo');
`
#### toContainExactlyOneMatchingElement()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toContainExactlyOneMatchingElement('.foo');
Assert that the given wrapper contains exactly one match for the given selector:
`js
function User(props) {
return (
User {props.index}
);
}
User.propTypes = {
index: PropTypes.number.isRequired,
className: PropTypes.string,
};
function Fixture() {
return (
const wrapper = mount(
expect(wrapper).toContainExactlyOneMatchingElement('.userOne');
expect(wrapper).not.toContainExactlyOneMatchingElement('User');
`
#### toContainReact()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toContainReact(foo);
Assert that the given wrapper contains the provided react instance:
`js
class User extends React.Component {
render () {
return (
User {this.props.index}
)
}
}
User.propTypes = {
index: PropTypes.number.isRequired
}
class Fixture extends React.Component {
render () {
return (
const wrapper = mount(
expect(wrapper).toContainReact(
expect(wrapper).not.toContainReact(
`
#### toHaveClassName()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toHaveClassName('foo');
Assert that the given wrapper has the provided className:
`js
function Fixture() {
return (
);
}
const wrapper = mount(
expect(wrapper.find('.foo')).toHaveClassName('foo');
expect(wrapper.find('.foo')).not.toHaveClassName('baz');
expect(wrapper.find('.bar')).toHaveClassName('bar baz');
expect(wrapper.find('.bar')).toHaveClassName('baz');
`
#### toHaveDisplayName()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toHaveDisplayName('div');
Assert that the wrapper is of a certain tag type:
`js
function Fixture() {
return (
);
}
const wrapper = mount(
expect(wrapper.find('#span')).toHaveDisplayName('span');
expect(wrapper.find('#span')).not.toHaveDisplayName('div');
`
#### toHaveHTML()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toHaveHTML('html');
Assert that the given wrapper has the provided html:
> Note Quotations are normalized.
`js
function Fixture() {
return (
Test
);
}
const wrapper = mount(
expect(wrapper.find('#child')).toHaveHTML(
'Test'
);
`
#### toHaveProp()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toHaveProp('foo', 'value');
expect().toHaveProp('foo');
expect().toHaveProp({foo: 'value'});
Assert that the given wrapper has the provided propKey and associated value if specified:
`js
function User() { ... }
User.propTypes = {
foo: PropTypes.string,
bar: PropTypes.array,
};
function Fixture() {
return (
const wrapper = mount(
expect(wrapper.find(User)).toHaveProp('foo');
expect(wrapper.find(User)).toHaveProp('foo', 'baz');
expect(wrapper.find(User)).toHaveProp('bar');
expect(wrapper.find(User)).toHaveProp('bar', [1,2,3]);
expect(wrapper.find(User)).toHaveProp({
bar: [1, 2, 3],
foo: 'baz',
});
`
#### toHaveRef()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toHaveRef('foo');
Assert that the mounted wrapper has the provided ref:
`js
class Fixture extends React.Component {
render() {
return (
);
}
}
const wrapper = mount(
expect(wrapper).toHaveRef('child');
expect(wrapper).not.toHaveRef('foo');
`
#### toHaveState()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toHaveState('foo');
expect().toHaveState('foo', 'bar');
expect().toHaveState({ foo: 'bar' });
Assert that the component has the provided stateKey and optional value if specified:
`js
class Fixture extends React.Component {
constructor() {
super();
this.state = {
foo: false,
};
}
render() {
return (
const wrapper = mount(
expect(wrapper).toHaveState('foo');
expect(wrapper).toHaveState('foo', false);
expect(wrapper).toHaveState({ foo: false });
`
#### toHaveStyle()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toHaveStyle('height');
expect().toHaveStyle('height', '100%');
expect().toHaveStyle({ height: '100%' });
Assert that the component has style of the provided key and value:
`js
function Fixture() {
const style1 = { height: '100%' };
const style2 = { flex: 8 };
return (
const wrapper = mount(
expect(wrapper.find('#style1')).toHaveStyle('height', '100%');
expect(wrapper.find('#style2')).toHaveStyle('flex', 8);
`
#### toHaveTagName()
Deprecated: Matcher toHaveTagName is deprecated. Use the replacement, toHaveDisplayName() instead.
#### toHaveText()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toHaveText('bar');
Assert that the wrapper's text matches the provided text exactly, using a strict comparison (===).
` Textjs
function Fixture() {
return (
);
}
const wrapper = mount(
expect(wrapper.find('#full')).toHaveText('Text');
expect(wrapper.find('#full')).not.toHaveText('Wrong');
expect(wrapper.find('#full')).toHaveText();
expect(wrapper.find('#empty')).not.toHaveText();
`
#### toIncludeText()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toIncludeText('bar');
Assert that the wrapper includes the provided text:
` Some important textjs
function Fixture() {
return (
);
}
const wrapper = mount(
expect(wrapper.find('#full')).toIncludeText('important');
expect(wrapper.find('#full')).not.toIncludeText('Wrong');
`
#### toHaveValue()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toHaveValue('bar');
Assert that the given wrapper has the provided value:
`js
function Fixture() {
return (
);
}
const wrapper = mount(
expect(wrapper.find('input').at(0)).toHaveValue('test');
expect(wrapper.find('input').at(1)).toHaveValue('bar');
`
#### toMatchElement()
| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js`
expect().toMatchElement(
expect().toMatchElement(
expect().toMatchElement(
Assert the wrapper matches the provided react instance. This is a matcher form of Enzyme's wrapper.matchesElement(), which returns a bool with no indication of what caused a failed match. This matcher includes the actual and expected debug trees as contextual information when it fails. Like matchesElement(), props are ignored. If you want to compare prop values as well, pass { ignoreProps: false } as options. Uses enzyme's debug() under the hood and compares debug strings, which makes for a human readable diff when expects fail.
Example:
`js
function Fixture() {
return (
);
}
const wrapper = shallow(
expect(wrapper).toMatchElement(
expect(wrapper.find('span')).toMatchElement();
expect(wrapper.find('span')).toMatchElement(
,
{ ignoreProps: false }
);
expect(wrapper).not.toMatchElement(
####
toMatchSelector()| render | mount | shallow |
| -------|-------|-------- |
| no | yes | yes |
Ways to use this API:
`js
expect().toMatchSelector('.foo');
`Assert that the wrapper matches the provided
selector:`js
function Fixture() {
return (
);
}const wrapper = mount( ); // mount/render/shallow when applicable
expect(wrapper.find('span')).toMatchSelector('span');
expect(wrapper.find('span')).toMatchSelector('#foo');
expect(wrapper.find('span')).toMatchSelector('.bar');
``