M
M
magary42020-05-27 23:14:14
Unit testing
magary4, 2020-05-27 23:14:14

How to test react component?

have the following component

const RecipeOverview = () => {
  const { recipes } = useContext(Context);

  return (
    <div className="recipe-overview">
      {recipes.map(recipe => <RecipeItem recipe={recipe} key={recipe.id} />)}
    </div>
  );
};


what tests can be written for it?
It only comes to mind to wet the RecipeItem well and see that as many mocks were rendered as came in recipes,
what else can I do?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Didenko, 2020-05-28
@Dasslier

Because there is a component without any logic at all, I would only take a snapshot. Or would not do tests for him at all

R
Robur, 2020-05-28
@Robur

To test something, you need to decide what you will test.
To decide what you will test, you need to find what can suddenly break.
What do you want to test in this component that
1. relates to the logic of this component
2. can break silently
3. is important enough to justify the cost of writing and maintaining the test?
variable assignment? it shouldn't break.
Work .map (that example that you described)? this is external logic, and indeed - built-in functionality.
The fact that in Recipe you pass the recipe? it cannot break unless you deliberately remove it. At this point, the test will also become invalid, and it will not help you in any way, it will have to be changed.
In general, you don't need to test it. Unless for training, try how it is.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question