I'm new to Jest and Enzyme testing, and I would like to know why the find function is not working with id.
//html from react, just the code where is the id increment
<div className="App-body">
<div className="changePage">
<button className="Selections" onClick={this.decrementPage}>Previous</button>
<p className="changePageNumber">{this.state.page}</p>
<button className="Selections" id="increment" onClick={this.incrementPage}>Next</button>
</div>
</div>
//test
it('next page', () => {
const wrapper = shallow(<Home />)
const incrementPage = wrapper.find('#increment')
incrementPage.simulate('click')
const countState = wrapper.state().page
expect(countState).toEqual(2)
})
Method “simulate” is meant to be run on 1 node. 0 found instead.
25 | //const text = wrapper.find('p').text()
26 | const incrementPage = wrapper.find('#increment')
> 27| incrementPage.simulate('click')
| ^
enzyme allows you to find components based on a component's displayName . If a component exists in a render tree where its displayName is set and has its first character as a capital letter, you can use a string to find it: function MyComponent() { return <div />; } MyComponent.
A Wrapper is an object that contains a mounted component or vnode and methods to test the component or vnode.
Both Jest and Enzyme are specifically designed to test React applications, Jest can be used with any other Javascript app but Enzyme only works with React. Jest can be used without Enzyme to render components and test with snapshots, Enzyme simply adds additional functionality.
Try using mount
instead of shallow
. shallow
does not render beyond the first level of elements. In your case, only the div with className 'App-Body' is rendered
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With