Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test child component method with Enzyme?

I have a component like that:

<Parent>   <Child/> </Parent> 

and <Child/> component have a method foo. I want test the foo method but I don't know how to access it. I tried:

mount(<Parent><Child/></Parent>).props().children.foo 

or

mount(<Parent><Child/></Parent>).children().foo 

but both them are undefined. I can't use .instance() because it's not root. I can't mount <Child/> only because the <Parent> add something (react-router's context.router) on context and I need them when init <Child/>. Any idea with this?

like image 756
Fomahaut Avatar asked Feb 15 '17 09:02

Fomahaut


People also ask

How do you test a child's component of an enzyme?

find('ChildComponent'); const child1 = element.at(0); const child2 = element.at(1); expect(element. length). toBe(2); expect(child1.name()). toBe('ChildComponent'); expect(child1.

How do you find the value of child components?

We can get child component values in the parent component by creating a reference to the child component using the @ref directive in the Parent component. Using the reference instance, you can access the child component values in the parent.


1 Answers

This worked for me:

mount(<Parent><Child/></Parent>).find(Child).instance().foo 
like image 183
jackocnr Avatar answered Sep 22 '22 23:09

jackocnr