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