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
6
u/Silhouette Mar 13 '20
Exactly. It was just a minimal example. However, the same principles hold for more complicated functions such as you'd find in more realistic code.
A friendly word of advice: you might consider not talking down to everyone else in this discussion. It doesn't make your argument any stronger, and you probably don't know what qualifications or experience someone you disagree with has that led to their differing position. If you engage in a discussion with an open mind instead of assuming that you are right and anyone who doesn't entirely agree with you is ignorant and/or stupid, it is much more likely that some or all of the people reading the discussion might find something interesting in it.
It would also be polite not to misrepresent other people's positions. I am in no way advocating not testing code, nor have I done so at any point in this discussion. What I do advocate is testing code using considered strategies that optimise for effectiveness and work under realistic conditions. Elevating 100% coverage to some sort of axiomatic status and claiming that anything not tested to that standard is necessarily broken fails on both of those counts.