@@ -24,32 +24,36 @@ import React from "react";
2424import TestRenderer from " react-test-renderer" ;
2525
2626// 1. import
27- import { assertComponents } from " react-assert" ;
27+ import { assertComponents , mockComponent } from " react-assert" ;
28+
29+ function SubComponent () {
30+ return < p className= " sub" > Sub< / p> ;
31+ }
32+ SubComponent .displayName = " SubComponent" ;
2833
2934function MyComponent (props ) {
35+ const { SubComp } = MyComponent;
36+
3037 return (
3138 < div>
32- < SubComponent / >
39+ < SubComp / >
3340 < p className= " my" > {props .text }< / p>
3441 < / div>
3542 );
3643}
3744MyComponent .displayName = " MyComponent" ;
38-
39- function SubComponent () {
40- return < p className= " sub" > Sub< / p> ;
41- }
45+ MyComponent .SubComp = SubComponent;
4246
4347describe (" MyComponent" , () => {
44- it (" should render component " , () => {
48+ it (" should render nested components " , () => {
4549 // given
4650 const text = " Hello" ;
4751
4852 // when
4953 const result = TestRenderer .create (< MyComponent text= {text} / > ).root ;
5054
5155 // then
52- // 2. call it with result.children and expected components tree
56+ // 2. call assertComponents to check expected components tree
5357 assertComponents (
5458 result .children ,
5559 < div>
@@ -58,5 +62,25 @@ describe("MyComponent", () => {
5862 < / div>
5963 );
6064 });
65+
66+ it (" should render mock components" , () => {
67+ // given
68+ // 3. use mockComponent to mock nested components
69+ MyComponent .SubComp = mockComponent (SubComponent);
70+ const { SubComp } = MyComponent;
71+ const text = " Hello" ;
72+
73+ // when
74+ const result = TestRenderer .create (< MyComponent text= {text} / > ).root ;
75+
76+ // then
77+ assertComponents (
78+ result .children ,
79+ < div>
80+ < SubComp / >
81+ < p className= " my" > {text}< / p>
82+ < / div>
83+ );
84+ });
6185});
6286```
0 commit comments