react enzyme simulate click component code example

Example: react enzyme simulate click

// App.js
function App() {
  const [count, setCount] = React.useState(0)

  const onClick = () => {
    setCount((prevState) => prevState + 1)
  }

  return (
    <div>
      <button onClick={onClick}>{count}</button>
    </div>
  )
}

export default App

//App.test.js
describe('Counter Group', () => {
  it('calls incCounter function when button is clicked', () => {
    const wrapper = shallow(<App />)
    const initClickCount = 2

    for (let i = 0; i < initClickCount; i++) {
      wrapper.find('button').simulate('click')
    }

    expect(wrapper.find('button').text()).toContain(2)

    console.log(wrapper.debug())
  })
})