So now that we know which tools we use and why testing in general is a good idea,
we of course have to find out what we should test and there, the answer is straightforward.
Actually writing good tests is quite complex and requires a lot of practice which is also one of the
reasons why this is no complete testing guide,
not at all.
So in general, here are some things you would not want to test,
you don't want to test the library,
you don't want to test react or axios or redux itself,
these are third party libraries which already were tested by the developers.
You don't need to test if they work correctly,
you don't need to test if the store, the redux store is working correctly,
you want to test the code
you add to your application and there you want to test the code which does not use that library.
So for example, if you're sending an axios post request, you don't need to test if that is sent successfully,
if it fails you probably have no internet connection, it's not the axios library. You typically want
to fake data you get back from my server in such a use case and just test what you are doing with such returned
data
so that's the idea behind testing.
Additionally you don't want to test too complex connections especially in react,
there is the danger of testing that you click a button in one component and you change something in
a totally different component.
Now you don't have to test if react is able to use the concept of props to emit an event and pass that
on,
you would be interested in testing if the button click in your react triggers a specific prop in the
first place
or if the change in data you receive via props in one component leads to a different result being rendered,
that is what you want to test.
There are some testing guides which goes so far to say for react components, you only need to test if
a react component itself is rendered correctly.
Now I will show you how to test different thing with react components but definitely keep in mind, don't
test too complex connections.
So these are the things you don't want to test,
what are you going to test then? Typical examples are isolated units,
you want to test that reducer function you created,
you want to test that component function you created,
you also want to test conditional output
if your component has a property which leads to something being rendered if that property is true, then
you want to test if this really happens.
What happens if some property changes in your component?
Does that affect the output correctly?
These are things you want to test in your unit tests.
Now with that knowledge gained, let's learn how we actually write unit tests in a react app.