showing results for - "react ezyme simulate click"
Linda
11 Apr 2017
1// App.js
2function App() {
3  const [count, setCount] = React.useState(0)
4
5  const onClick = () => {
6    setCount((prevState) => prevState + 1)
7  }
8
9  return (
10    <div>
11      <button onClick={onClick}>{count}</button>
12    </div>
13  )
14}
15
16export default App
17
18//App.test.js
19describe('Counter Group', () => {
20  it('calls incCounter function when button is clicked', () => {
21    const wrapper = shallow(<App />)
22    const initClickCount = 2
23
24    for (let i = 0; i < initClickCount; i++) {
25      wrapper.find('button').simulate('click')
26    }
27
28    expect(wrapper.find('button').text()).toContain(2)
29
30    console.log(wrapper.debug())
31  })
32})
Léonie
01 Jul 2017
1// button.js
2import React from "react";
3
4class Button extends React.Component {
5  constructor(props) {
6    super(props);
7    this.state = {
8      count: 0
9    };
10    this.onClick = this.onClick.bind(this);
11  }
12
13  onClick() {
14    this.setState({ count: 1 });
15  }
16
17  renderState() {
18    return this.state.count;
19  }
20
21  render() {
22    return (
23      <div>
24        <button onClick={this.onClick}>{this.count}</button>
25      </div>
26    );
27  }
28}
29
30export default Button;
31
32//button.test.js
33import React from "react";
34import { mount } from "enzyme";
35import Button from "./Button";
36
37describe("button test", () => {
38  let wrapper;
39  beforeEach(() => {
40    wrapper = shallow(<Button />);
41  });
42
43  it("test increment button", () => {
44    // click event inc from zero to one
45    wrapper.find("button").simulate("click");
46    // replace value click event with new value to 3
47    wrapper.setState({ count: 3 });
48    // expect value is correct or not
49    expect(wrapper.state("count")).toBe(3);
50    expect(wrapper.instance().renderState()).toBe(3);
51  });
52});
53
Lina
05 Nov 2019
1//Counter.js
2import React from 'react'
3
4function Counter() {
5  const [count, setCount] = React.useState(0)
6
7  const onClick = () => setCount((prevState) => prevState + 1)
8
9  return (
10    <div>
11      <button onClick={onClick}>{count}</button>
12    </div>
13  )
14}
15
16export default Counter
17
18//App.js
19import React from 'react'
20import Counter from './Counter'
21
22const App = (props) => {
23  return (
24    <>
25      <Counter />
26    </>
27  )
28}
29
30export default App
31
32//App.test.js
33import React from 'react'
34import { mount } from 'enzyme'
35import App from './App'
36import Counter from './Counter'
37
38describe('Counter Group', () => {
39  it('calls incCounter function when button is clicked', () => {
40    const wrapper = mount(<App />)
41    const subComponent = wrapper.find(Counter)
42    const initClickCount = 2
43
44    expect(subComponent.find('button').exists()).toBeTruthy()
45
46    for (let i = 0; i < initClickCount; i++) {
47      subComponent.find('button').simulate('click')
48    }
49
50    expect(subComponent.find('button').text()).toContain(2)
51
52    console.log(wrapper.debug())
53  })
54})
55