react enzyme simulate click sub component

// 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())
  })
})

4
6

                                    /**
 * This test passes with or without the onMouseOver/onMouseLeave handlers.
 * In a real browser, onClickMock is not called without those handlers.
 */
const data = [
  {title: 'Option 1', description: 'a cool option'}
];
it('should invoke a childs onClick handler when the child is clicked', () =&gt; {
  const onClickMock = jest.fn();
  const component = shallow(
    &lt;Dropdown
      buttonContent='Dropdown'&gt;
      &lt;ul&gt;
        { data.map((item, index) =&gt; {
          return (
            &lt;li
              key={ index }
              onClick={ onClickMock }
              data-test-section={ `dropdown-item-${index}` }&gt;
              { item.title }
            &lt;/li&gt;
          );
        })
      }
    &lt;/ul&gt;
  &lt;/Dropdown&gt;
);

   component.find('button').simulate('click');
   component.find('[data-test-section=&quot;dropdown-item-1&quot;]').simulate('click');
   expect(onClickMock.mock.calls.length).toBe(1);
 });

4 (6 Votes)
0
3.9
10
Mike VanIn 100 points

                                    //Counter.js
import React from 'react'

function Counter() {
  const [count, setCount] = React.useState(0)

  const onClick = () =&gt; setCount((prevState) =&gt; prevState + 1)

  return (
    &lt;div&gt;
      &lt;button onClick={onClick}&gt;{count}&lt;/button&gt;
    &lt;/div&gt;
  )
}

export default Counter

//App.js
import React from 'react'
import Counter from './Counter'

const App = (props) =&gt; {
  return (
    &lt;&gt;
      &lt;Counter /&gt;
    &lt;/&gt;
  )
}

export default App

//App.test.js
import React from 'react'
import { mount } from 'enzyme'
import App from './App'
import Counter from './Counter'

describe('Counter Group', () =&gt; {
  it('calls incCounter function when button is clicked', () =&gt; {
    const wrapper = mount(&lt;App /&gt;)
    const subComponent = wrapper.find(Counter)
    const initClickCount = 2

    expect(subComponent.find('button').exists()).toBeTruthy()

    for (let i = 0; i &lt; initClickCount; i++) {
      subComponent.find('button').simulate('click')
    }

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

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

3.9 (10 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source