How can I unit test the parent component to respond, which calls methods for children

The β€œTips” section of the response documentation describes a template for communication between components, where the parent component calls the method for the child (via the ref link).

See https://facebook.imtqy.com/react/tips/expose-component-functions.html

I use a third-party reaction component that requires me to use this approach.

I would like to unit test my component and verify that it calls the child method (with the correct parameters) under the right circumstances, but I find it hard to understand how ...

For example, in the documentation on Facebook, how would I write a test that checks that Todos triggers animations on the latest Todo, when should it?

+7
unit-testing reactjs
source share
1 answer

Consider using Facebook Sample Stream . Using this template, you will separate user interface components from state management; government is facilitated by "shops." Stores complete state coordination between components. Now your views are not communicated directly. In addition, data is transmitted in only one way.

flow pattern

Note that there are many Flux implementations that you can use, such as reFlux . You can find many of them by searching for available NPM packages.

... So how to check this?

Once you have installed the template, you can use Google resources, blogs and examples for testing. There are currently two main approaches for unit testing: Jest and Jasmine. Facebook recommends using Jest :

In order for the unit test to work on a truly isolated application device, we need to mock every module except the one we are testing. Jest makes mockery of other parts of the Flux app trivial.

...

Flux stores often receive a lot of formal unit test coverage, as this is the state of the application and logic. Stores are arguably the most important place in the Flux coverage application.

+1
source share

All Articles