File tree Expand file tree Collapse file tree 2 files changed +83
-0
lines changed
packages/react-test-renderer/src Expand file tree Collapse file tree 2 files changed +83
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,9 @@ import {
22
22
HostPortal ,
23
23
HostText ,
24
24
HostRoot ,
25
+ ContextConsumer ,
26
+ ContextProvider ,
27
+ Mode ,
25
28
} from 'shared/ReactTypeOfWork' ;
26
29
import invariant from 'fbjs/lib/invariant' ;
27
30
@@ -366,6 +369,9 @@ function toTree(node: ?Fiber) {
366
369
case HostText :
367
370
return node . stateNode . text ;
368
371
case Fragment :
372
+ case ContextProvider :
373
+ case ContextConsumer :
374
+ case Mode :
369
375
return childrenToTree ( node . child ) ;
370
376
default :
371
377
invariant (
@@ -463,6 +469,9 @@ class ReactTestInstance {
463
469
children . push ( '' + node . memoizedProps ) ;
464
470
break ;
465
471
case Fragment :
472
+ case ContextProvider :
473
+ case ContextConsumer :
474
+ case Mode :
466
475
descend = true ;
467
476
break ;
468
477
default :
Original file line number Diff line number Diff line change @@ -879,4 +879,78 @@ describe('ReactTestRenderer', () => {
879
879
'world' ,
880
880
] ) ;
881
881
} ) ;
882
+
883
+ it ( 'supports context providers and consumers' , ( ) => {
884
+ const { Consumer, Provider} = React . createContext ( 'a' ) ;
885
+
886
+ function Child ( props ) {
887
+ return props . value ;
888
+ }
889
+
890
+ function App ( ) {
891
+ return (
892
+ < Provider value = "b" >
893
+ < Consumer > { value => < Child value = { value } /> } </ Consumer >
894
+ </ Provider >
895
+ ) ;
896
+ }
897
+
898
+ const renderer = ReactTestRenderer . create ( < App /> ) ;
899
+ const child = renderer . root . findByType ( Child ) ;
900
+ expect ( child . children ) . toEqual ( [ 'b' ] ) ;
901
+ expect ( prettyFormat ( renderer . toTree ( ) ) ) . toEqual (
902
+ prettyFormat ( {
903
+ instance : null ,
904
+ nodeType : 'component' ,
905
+ props : { } ,
906
+ rendered : {
907
+ instance : null ,
908
+ nodeType : 'component' ,
909
+ props : {
910
+ value : 'b' ,
911
+ } ,
912
+ rendered : 'b' ,
913
+ type : Child ,
914
+ } ,
915
+ type : App ,
916
+ } ) ,
917
+ ) ;
918
+ } ) ;
919
+
920
+ it ( 'supports modes' , ( ) => {
921
+ function Child ( props ) {
922
+ return props . value ;
923
+ }
924
+
925
+ function App ( props ) {
926
+ return (
927
+ < React . StrictMode >
928
+ < Child value = { props . value } />
929
+ </ React . StrictMode >
930
+ ) ;
931
+ }
932
+
933
+ const renderer = ReactTestRenderer . create ( < App value = "a" /> ) ;
934
+ const child = renderer . root . findByType ( Child ) ;
935
+ expect ( child . children ) . toEqual ( [ 'a' ] ) ;
936
+ expect ( prettyFormat ( renderer . toTree ( ) ) ) . toEqual (
937
+ prettyFormat ( {
938
+ instance : null ,
939
+ nodeType : 'component' ,
940
+ props : {
941
+ value : 'a' ,
942
+ } ,
943
+ rendered : {
944
+ instance : null ,
945
+ nodeType : 'component' ,
946
+ props : {
947
+ value : 'a' ,
948
+ } ,
949
+ rendered : 'a' ,
950
+ type : Child ,
951
+ } ,
952
+ type : App ,
953
+ } ) ,
954
+ ) ;
955
+ } ) ;
882
956
} ) ;
You can’t perform that action at this time.
0 commit comments