jasmine-enzyme
Quick Links
Installation
We suggest using yarn for installations.
yarn add jasmine-enzyme --dev
But npm works too!
$ npm install jasmine-enzyme --save-dev
Setup
For Jasmine, you'll need to call jasmineEnzyme()
in any before
method due to the way jasmine's plugin
system works.
; ;
Assertions
- 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:
;
Assert that the given wrapper is checked:
{ return <div> <input id="checked" defaultChecked /> <input id="not" defaultChecked=false /> <input id="tertiary" defaultChecked checked=false /> </div> ;} const wrapper = ; // mount/render/shallow when applicable ;not;
toBeDisabled()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;
Assert that the given wrapper is disabled:
{ return <div> <input id="disabled" disabled /> <input id="not"/> </div> ;} const wrapper = ; // mount/render/shallow when applicable ;not;
toBeEmptyRender()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;
Assert that the given wrapper has an empty render (null
or false
):
{ return null;} { return <div> <EmptyRenderFixture /> </div> ;} const wrapper = ; // mount/render/shallow when applicable ;not;
toExist()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;
Assert that the given enzyme wrapper has rendered content.
{ return <div> <span className="foo" /> <span className="bar baz" /> </div> ;} const wrapper = ; // mount/render/shallow when applicable ;not;
toContainMatchingElement()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;
Assert that the given wrapper contains at least one match for the given selector:
{ return <span className=propsclassName> User propsindex </span> ;} UserpropTypes = index: PropTypesnumberisRequired className: PropTypesstring; { return <div> <ul> <li> <User index=1 className="userOne" /> </li> <li> <User index=2 className="userTwo" /> </li> </ul> </div> ;} const wrapper = ; // mount/render/shallow when applicable ;not;
toContainMatchingElements()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;
Assert that the given wrapper contains a given number of matches for the given selector:
{ return <span className=propsclassName> User propsindex </span> ;} UserpropTypes = index: PropTypesnumberisRequired className: PropTypesstring; { return <div> <ul> <li> <User index=1 className="userOne" /> </li> <li> <User index=2 className="userTwo" /> </li> </ul> </div> ;} const wrapper = ; // mount/render/shallow when applicable ;not;
toContainExactlyOneMatchingElement()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;
Assert that the given wrapper contains exactly one match for the given selector:
{ return <span className=propsclassName> User propsindex </span> ;} UserpropTypes = index: PropTypesnumberisRequired className: PropTypesstring; { return <div> <ul> <li> <User index=1 className="userOne" /> </li> <li> <User index=2 className="userTwo" /> </li> </ul> </div> ;} const wrapper = ; // mount/render/shallow when applicable ;not;
toContainReact()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;
Assert that the given wrapper contains the provided react instance:
Component { return <span>User thispropsindex</span> } UserpropTypes = index: PropTypesnumberisRequired Component { return <div> <ul> <li><User index=1 /></li> <li><User index=2 /></li> </ul> </div> } const wrapper = ; // mount/render/shallow when applicable ;not;
toHaveClassName()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;
Assert that the given wrapper has the provided className:
{ return <div> <span className="foo" /> <span className="bar baz" /> </div> ;} const wrapper = ; // mount/render/shallow when applicable ;not; ;;
toHaveDisplayName()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;
Assert that the wrapper is of a certain tag type:
{ return <div> <span id="span" /> </div> ;} const wrapper = ; ;not;
toHaveHTML()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;
Assert that the given wrapper has the provided html:
Note Quotations are normalized.
{ return <div id="root"> <span id="child">Test</span> </div> ;} const wrapper = ; // mount/render/shallow when applicable ;
toHaveProp()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;;;
Assert that the given wrapper has the provided propKey and associated value if specified:
{ ... }UserpropTypes = foo: PropTypesstring bar: PropTypesarray; { return <div id="root"> <User foo='baz' bar=123 /> </div> ;} const wrapper = ; // mount/render/shallow when applicable ;; ;; ;
toHaveRef()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;
Assert that the mounted wrapper has the provided ref:
Component { return <div> <span ref="child" /> </div> ; } const wrapper = ; ;not;
toHaveState()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;;;
Assert that the component has the provided stateKey and optional value if specified:
Component { super; thisstate = foo: false ; } { return <div /> ; } const wrapper = ; // mount/render/shallow when applicable ;;;
toHaveStyle()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;;;
Assert that the component has style of the provided key and value:
{ const style1 = height: '100%' ; const style2 = flex: 8 ; return <div> <span id="style1" style=style1 /> <span id="style2" style=style2 /> </div> ;} const wrapper = ; // mount/render/shallow when applicable ;;
toHaveTagName()
Deprecated: Matcher toHaveTagName
is deprecated. Use the replacement, [toHaveDisplayName()](#tohavedisplayname)
instead.
toHaveText()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;
Assert that the wrapper's text matches the provided text exactly, using a strict comparison (===
).
{ return <div> <p id="full">Text</p> <p id="empty"></p> </div> ;} const wrapper = ; // mount/render/shallow when applicable ;not; ;not;
toIncludeText()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;
Assert that the wrapper includes the provided text:
{ return <div> <p id="full">Some important text</p> </div> ;} const wrapper = ; // mount/render/shallow when applicable ;not;
toHaveValue()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;
Assert that the given wrapper has the provided value
:
{ return <div> <input defaultValue="test" /> <input defaultValue="foo" value="bar" onChange=jest /> </div> ;} const wrapper = ; // mount/render/shallow when applicable ;;
toMatchElement()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;;;
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:
{ return <div> <span id="foo" className="bar" /> </div> ;} const wrapper = ; // mount/render/shallow when applicable ;;;not;
toMatchSelector()
render | mount | shallow |
---|---|---|
no | yes | yes |
Ways to use this API:
;
Assert that the wrapper matches the provided selector
:
{ return <div> <span id="foo" className="bar" /> </div> ;} const wrapper = ; // mount/render/shallow when applicable ;;;