Skip to content
Advertisement

react – how to test a component if it has value prop

App.js

JavaScript

Input.js

JavaScript

I want to use jest and react-testing-library to test for the <Input> component.

But I cannot figure out how to handle the value prop.

This is the test file I’ve made, but I cannot go further.

JavaScript

The test returns failed after I run it.

Codesandbox
https://codesandbox.io/s/affectionate-leftpad-cytmf0?file=/src/Input.jsx:0-219

Advertisement

Answer

With react-testing-library you want to avoid testing the internal values of components.
Instead you should be checking if the html that’s rendered is correct.

In your example: rather than checking whether the value prop is correct, you should check that given a certain value (e.g. “Test Value”) the input html element contains the text “Test Value”.

Reference: https://testing-library.com/docs/guiding-principles

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement