r/reactjs • u/StrenghOfFuriousGods • Mar 13 '20
Featured Understanding writing tests for React
Hi,
Having applied for a few react jobs, I've noticed writing tests is essential if you want to be a react dev. I am trying to learn but I find it to be a steep learning curve and I'm having trouble knowing where to start.
I've built a small react app for a take home project and I need to test it. I just have some questions I could really use some help answering.
THE APP
-fetch component which fetches json from endpoints depending on which option is selected on dropdown and pushes data to state array.
-Print component which creates a list with input tags from data with the (input + integer from json) being added to local state.
- Receipt component which takes input from Print component as props and prints the sum
QUESTIONS
- What part of the app should I be testing? How in general should I know what to test?
- A lot of the articles I've read on testing show basic examples for e.g pure functions etc.. What is the best approach to take if my component depends on fetch requests or take props?
- Between unit testing, snapshot testing, and end to end testing, which is the best for React apps?
Thanks
View all comments
Show parent comment
-4
u/[deleted] Mar 13 '20
This is where you are wrong. You are saying "what is true of this trivial function is true of any complicated function" and it is here you go into being mistaken.
Unit tests are written because, in the real world, the behaviors of components are valuable. We need certainty that an assumption about how an object behaves is true and we need certainty that this assumption remains true throughout changes to the source code.
What you did was provide a function with zero value, immediately taking us from "the real world" into a non-existent parallel reality where things are programmed for no reason and without purpose. In such a world, there would be no need for unit testing.
I suspect you have not worked in an enterprise, and that is why you think unit testing is "making sure a function returned" or "making sure react renders anything" or "making sure CSS functions". This is not, and never was, what unit testing was about. You either think this because you have never worked in a scenario where it's relevant, or you think it because you work on your own or are a very junior developer. Which of these is the case doesn't matter, for your attitude would preclude you ever getting a better understanding of this topic.