简体   繁体   中英

Can a function that was passed as a component property be called in a test using react-testing-library

What I want is:

  1. Find the way to call the component's function;
  2. Test the value after function execution.

For example, the behavior is:

  1. A user types something into an input;
  2. After each typing, a function is fired and updates the value and passes this value into another component.

I have to write a test that the passed value to not exceeds the max length. In this case, updated value will represent a property from the local state that is passed to another component after its update. So, how can I call this function and verify that the max length is not greater then I expect?

I've created a sample where I show how to manage to fire a function and control the value I pass after function execution.

A brief description is:

  1. Mock nested component
  2. return a jsx with indicating parameters as a function params for the mocked component
  3. simulate an event for mocked component (function attached to the event will be fired and local state will be updated)
  4. use getByTestId to get an element that contains updated data
  5. Verify if data is what is expected to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM