W
W
Wood Walker2018-05-09 15:53:00
React
Wood Walker, 2018-05-09 15:53:00

Questions about Jest testing with React?

In general, there is approximately such a component, I can’t figure out how to test it using jest + enzyme. This is an abstract example that is similar to mine, please help me understand how and what to test there. I can't figure it out myself, I just can't. I used the jest off site, various Google sources, plus I looked at some tests in the book. For example, in this abstract case, I think you need to check that the methods work correctly, but how can you refer to them in the test to check?

class Test extends Component{
method1() {}
method2() {}
render() {
return(
<div>
<InideComponent1 func={method1} prop={prop1} />
<InideComponent2 func={method2} prop={prop2} />
</div>
)
}
}

And it is not clear what can be tested here.
<div className="container">
<Component_1/>
<Component_2/>
</div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pfanaskov, 2018-06-25
@dollar1610

Think of the component as a black box. You give him props, he draws a tree for you, and you check it - in the simplest case. And it doesn't matter if it calls its internal methods or not. It is more difficult when there is a state and / or some sequence of actions from the user after rendering is expected (for example, clicking on a button). In this case, again, the required state with props is set, user actions are simulated, and the result looks - rendered elements, called callbacks in props ... You must test the external interface of the component, the maximum correct setting of the state as a result of any manipulations.
Here is a good article on this topic https://medium.com/selleo/testing-react-components...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question