@@ -15,6 +15,7 @@ let ReactFabric;
15
15
let createReactNativeComponentClass ;
16
16
let UIManager ;
17
17
let FabricUIManager ;
18
+ let StrictMode ;
18
19
19
20
jest . mock ( 'shared/ReactFeatureFlags' , ( ) =>
20
21
require ( 'shared/forks/ReactFeatureFlags.native-fabric-oss' ) ,
@@ -25,6 +26,7 @@ describe('ReactFabric', () => {
25
26
jest . resetModules ( ) ;
26
27
27
28
React = require ( 'react' ) ;
29
+ StrictMode = React . StrictMode ;
28
30
ReactFabric = require ( 'react-native-renderer/fabric' ) ;
29
31
FabricUIManager = require ( 'FabricUIManager' ) ;
30
32
UIManager = require ( 'UIManager' ) ;
@@ -436,4 +438,79 @@ describe('ReactFabric', () => {
436
438
// This could change in the future.
437
439
expect ( touchStart2 ) . toBeCalled ( ) ;
438
440
} ) ;
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 StrictMode children. ' +
467
+ 'Instead, add a ref directly to the element you want to reference.' +
468
+ '\n' +
469
+ '\n in RCTView (at **)' +
470
+ '\n in StrictMode (at **)' +
471
+ '\n in ContainsStrictModeChild (at **)' +
472
+ '\n' +
473
+ '\nLearn more about using refs safely here:' +
474
+ '\nhttps://fb.me/react-strict-mode-find-node' ,
475
+ ] ) ;
476
+ expect ( match ) . toBe ( child . _nativeTag ) ;
477
+ } ) ;
478
+
479
+ it ( 'findNodeHandle should warn if passed a component that is inside StrictMode' , ( ) => {
480
+ const View = createReactNativeComponentClass ( 'RCTView' , ( ) => ( {
481
+ validAttributes : { foo : true } ,
482
+ uiViewClassName : 'RCTView' ,
483
+ } ) ) ;
484
+
485
+ let parent = undefined ;
486
+ let child = undefined ;
487
+
488
+ class IsInStrictMode extends React . Component {
489
+ render ( ) {
490
+ return < View ref = { n => ( child = n ) } /> ;
491
+ }
492
+ }
493
+
494
+ ReactFabric . render (
495
+ < StrictMode >
496
+ < IsInStrictMode ref = { n => ( parent = n ) } />
497
+ </ StrictMode > ,
498
+ 11 ,
499
+ ) ;
500
+
501
+ let match ;
502
+ expect ( ( ) => ( match = ReactFabric . findNodeHandle ( parent ) ) ) . toWarnDev ( [
503
+ 'Warning: findNodeHandle is deprecated in StrictMode. ' +
504
+ 'findNodeHandle was passed an instance of IsInStrictMode which is inside StrictMode. ' +
505
+ 'Instead, add a ref directly to the element you want to reference.' +
506
+ '\n' +
507
+ '\n in RCTView (at **)' +
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
+ } ) ;
439
516
} ) ;
0 commit comments