Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wrapper.find with id in enzyme and jest doesn't find element

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')
    |                 ^                   
like image 415
Jonathan Avatar asked Aug 13 '19 09:08

Jonathan


People also ask

How do you determine the components of an Enzyme?

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.

What is Jest wrapper?

A Wrapper is an object that contains a mounted component or vnode and methods to test the component or vnode.

Can I use Jest without Enzyme?

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.


1 Answers

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

like image 51
Mukesh Soni Avatar answered Nov 16 '22 01:11

Mukesh Soni