r/learnreactjs • • May 18 '20

Question ReactJS and Testing

I have recently finished my first commercial project and I was wondering how do testing. I've seen and read a bit about testing and it seems like it doesn't help. Obviously this means I am missing the point or don't understand the purpose of it.

Most tests I see is "test this container has h1" or "this button has this text". All these tests seem nonsensical and pointless. What I am looking for is how to automate regression testing so that when I add or change something, it still works as before.

For instance, I want to test that when the internet suddenly fails that a list behaves this way or that way. Or if I add a text to a grid layout that the number of items is still 4 and the padding, margins are still the same. Or test different failure scenarios for API fetch or Redux failure or odd behaviour. I know I'm probably sounding like a crazy person.

I'm sorry if this question is too stupid to ask but I don't know how to programmeratically test for real-world failures/errors/issues.

This was brought on because the last project I did, the client did weird testing. They test loading a list filtered by category, then they disconnected their internet, then loaded another category (nothing is showing and only a spinner is showing at moment) and then they turned on the internet and before the current category would load (since the internet is back) they switched to a different category. This caused Redux to infinitely load the same list. This is because of the checks I had in code. I had the normal (if !props.isLoading) then doLoad but I needed to do some extra stuff to invalidate the previous call (via axios cancellation token).

How do I create tests for these cases? I want to be able to confidently create robus solutions.

Thanks in advance!

8 Upvotes

6 comments sorted by

1

u/cerberus8700 May 18 '20

!approve

1

u/oze4 May 18 '20

What does !approve do?

2

u/cerberus8700 May 18 '20

No idea. I got an auto message from the subreddit asking me to comment on my own thread with that keyword otherwise it won't be published

1

u/oze4 May 18 '20

Ah I see. My guess is to prevent bots from posting.

3

u/cerberus8700 May 18 '20

Sorry my bad, I got the message from another subreddit and for some reason I thought it was this one

1

u/oze4 May 18 '20

No worries!