@@ -15,6 +15,7 @@ let ReactFabric;
1515let createReactNativeComponentClass ;
1616let UIManager ;
1717let FabricUIManager ;
18+ let StrictMode ;
1819
1920jest . mock ( 'shared/ReactFeatureFlags' , ( ) =>
2021 require ( 'shared/forks/ReactFeatureFlags.native-fabric-oss' ) ,
@@ -25,6 +26,7 @@ describe('ReactFabric', () => {
2526 jest . resetModules ( ) ;
2627
2728 React = require ( 'react' ) ;
29+ StrictMode = React . StrictMode ;
2830 ReactFabric = require ( 'react-native-renderer/fabric' ) ;
2931 FabricUIManager = require ( 'FabricUIManager' ) ;
3032 UIManager = require ( 'UIManager' ) ;
@@ -436,4 +438,79 @@ describe('ReactFabric', () => {
436438 // This could change in the future.
437439 expect ( touchStart2 ) . toBeCalled ( ) ;
438440 } ) ;
441+
442+ it ( 'findNodeHandle should warn if used to find a host component inside StrictMode' , ( ) => {
443+ const View = createReactNativeComponentClass ( 'RCTView' , ( ) => ( {
444+ validAttributes : { foo : true } ,
445+ uiViewClassName : 'RCTView' ,
446+ } ) ) ;
447+
448+ let parent = undefined ;
449+ let child = undefined ;
450+
451+ class ContainsStrictModeChild extends React . Component {
452+ render ( ) {
453+ return (
454+ < StrictMode >
455+ < View ref = { n => ( child = n ) } />
456+ </ StrictMode >
457+ ) ;
458+ }
459+ }
460+
461+ ReactFabric . render ( < ContainsStrictModeChild ref = { n => ( parent = n ) } /> , 11 ) ;
462+
463+ let match ;
464+ expect ( ( ) => ( match = ReactFabric . findNodeHandle ( parent ) ) ) . toWarnDev ( [
465+ 'Warning: findNodeHandle is deprecated in StrictMode. ' +
466+ 'findNodeHandle was passed an instance of ContainsStrictModeChild which renders a StrictMode subtree. ' +
467+ 'The nearest child is in StrictMode. ' +
468+ 'Use an explicit ref directly on the element you want to get a handle on.' +
469+ '\n' +
470+ '\n in RCTView (at **)' +
471+ '\n in StrictMode (at **)' +
472+ '\n in ContainsStrictModeChild (at **)' +
473+ '\n' +
474+ '\nLearn more about using refs safely here:' +
475+ '\nhttps://fb.me/react-strict-mode-find-node' ,
476+ ] ) ;
477+ expect ( match ) . toBe ( child . _nativeTag ) ;
478+ } ) ;
479+
480+ it ( 'findNodeHandle should warn if passed a component that is inside StrictMode' , ( ) => {
481+ const View = createReactNativeComponentClass ( 'RCTView' , ( ) => ( {
482+ validAttributes : { foo : true } ,
483+ uiViewClassName : 'RCTView' ,
484+ } ) ) ;
485+
486+ let parent = undefined ;
487+ let child = undefined ;
488+
489+ class IsInStrictMode extends React . Component {
490+ render ( ) {
491+ return < View ref = { n => ( child = n ) } /> ;
492+ }
493+ }
494+
495+ ReactFabric . render (
496+ < StrictMode >
497+ < IsInStrictMode ref = { n => ( parent = n ) } />
498+ </ StrictMode > ,
499+ 11 ,
500+ ) ;
501+
502+ let match ;
503+ expect ( ( ) => ( match = ReactFabric . findNodeHandle ( parent ) ) ) . toWarnDev ( [
504+ 'Warning: findNodeHandle is deprecated in StrictMode. ' +
505+ 'findNodeHandle was passed an instance of IsInStrictMode which is in a StrictMode subtree. ' +
506+ 'Use an explicit ref directly on the element you want to get a handle on.' +
507+ '\n' +
508+ '\n in IsInStrictMode (at **)' +
509+ '\n in StrictMode (at **)' +
510+ '\n' +
511+ '\nLearn more about using refs safely here:' +
512+ '\nhttps://fb.me/react-strict-mode-find-node' ,
513+ ] ) ;
514+ expect ( match ) . toBe ( child . _nativeTag ) ;
515+ } ) ;
439516} ) ;
0 commit comments