Questions regarding Jest, Enzyme, and best practices
Hi,
I'm trying to learn testing in react, and had come across some questions.
My project uses MobX, SemanticUI React, and Firebase.
-
Shallow rendering won't render <Button /> components that I'm using off of semantic ui react, because it's one level deep right? I'm trying to simulate a click on a submit button, and I'm using wrapper.findWhere(node => node.type() === 'button' && node.contains('Submit')) to find the button to click. But if I'm using shallow(), I can't access the properties that I'm using and I have to use mount?
-
In above case, it's better to test how the click changes the view, and not how it changes the internal state of the component right?
-
What are some good things to test in React, knowing that the structure and the view of the rendered html structure might change due to design revamp or whatnot?
Thank you
Posted May 19, 2019, 8:01 PM UTC
Jest and Enzyme work best for unit testing. In that case, you want to test a component and what it renders when you change props and optionally call its method. You don't care about what its children do, only if they are rendered and with which props.
More precisely, to address your questions:
mount()instead ofshallow()usually means you are trying to do something wrong. In this case, you should test your component, not the buttons in it. Semantic UI should have its own tests, and if you are opting to use it you should also trust those tests :)My approach to unit testing new components goes something like the following:
mount()think again. Only use it if you're absolutely sure you need it.