11/* eslint-disable no-eval */
22import { fireEvent , render } from '@testing-library/react' ;
3- import React from 'react' ;
3+ import React , { ReactNode } from 'react' ;
44import useEvent from '../src/hooks/useEvent' ;
5- import { composeRef , supportRef , useComposeRef } from '../src/ref' ;
5+ import {
6+ composeRef ,
7+ supportNodeRef ,
8+ supportRef ,
9+ useComposeRef ,
10+ } from '../src/ref' ;
611
712describe ( 'ref' , ( ) => {
813 describe ( 'composeRef' , ( ) => {
@@ -12,7 +17,7 @@ describe('ref', () => {
1217
1318 const mergedRef = composeRef ( refFunc1 , refFunc2 ) ;
1419 const testRefObj = { } ;
15- mergedRef ( testRefObj ) ;
20+ ( mergedRef as any ) ( testRefObj ) ;
1621 expect ( refFunc1 ) . toHaveBeenCalledWith ( testRefObj ) ;
1722 expect ( refFunc2 ) . toHaveBeenCalledWith ( testRefObj ) ;
1823 } ) ;
@@ -25,7 +30,7 @@ describe('ref', () => {
2530
2631 it ( 'useComposeRef' , ( ) => {
2732 const Demo = ( { ref1, ref2 } ) => {
28- const mergedRef = useComposeRef ( ref1 , ref2 ) ;
33+ const mergedRef = useComposeRef < HTMLDivElement > ( ref1 , ref2 ) ;
2934 return < div ref = { mergedRef } /> ;
3035 } ;
3136
@@ -70,14 +75,14 @@ describe('ref', () => {
7075 } ) ;
7176
7277 describe ( 'supportRef' , ( ) => {
73- class Holder extends React . Component {
78+ class Holder extends React . Component < { children : ReactNode } > {
7479 render ( ) {
7580 return this . props . children ;
7681 }
7782 }
7883
7984 it ( 'function component' , ( ) => {
80- const holderRef = React . createRef ( ) ;
85+ const holderRef = React . createRef < Holder > ( ) ;
8186
8287 function FC ( ) {
8388 return < div /> ;
@@ -93,7 +98,7 @@ describe('ref', () => {
9398 } ) ;
9499
95100 it ( 'arrow function component' , ( ) => {
96- const holderRef = React . createRef ( ) ;
101+ const holderRef = React . createRef < Holder > ( ) ;
97102
98103 // Use eval since jest will convert arrow function to function
99104 const FC = eval ( '() => null' ) ;
@@ -107,7 +112,7 @@ describe('ref', () => {
107112 } ) ;
108113
109114 it ( 'forwardRef function component' , ( ) => {
110- const holderRef = React . createRef ( ) ;
115+ const holderRef = React . createRef < Holder > ( ) ;
111116
112117 const FRC = React . forwardRef ( ( ) => < div /> ) ;
113118 render (
@@ -120,7 +125,7 @@ describe('ref', () => {
120125 } ) ;
121126
122127 it ( 'class component' , ( ) => {
123- const holderRef = React . createRef ( ) ;
128+ const holderRef = React . createRef < Holder > ( ) ;
124129
125130 class CC extends React . Component {
126131 state = { } ;
@@ -139,7 +144,7 @@ describe('ref', () => {
139144 } ) ;
140145
141146 it ( 'memo of function component' , ( ) => {
142- const holderRef = React . createRef ( ) ;
147+ const holderRef = React . createRef < Holder > ( ) ;
143148
144149 const FC = ( ) => < div /> ;
145150 const MemoFC = React . memo ( FC ) ;
@@ -153,7 +158,7 @@ describe('ref', () => {
153158 } ) ;
154159
155160 it ( 'memo of forwardRef function component' , ( ) => {
156- const holderRef = React . createRef ( ) ;
161+ const holderRef = React . createRef < Holder > ( ) ;
157162
158163 const FRC = React . forwardRef ( ( ) => < div /> ) ;
159164 const MemoFC = React . memo ( FRC ) ;
@@ -166,4 +171,20 @@ describe('ref', () => {
166171 expect ( supportRef ( holderRef . current . props . children ) ) . toBeTruthy ( ) ;
167172 } ) ;
168173 } ) ;
174+
175+ describe ( 'nodeSupportRef' , ( ) => {
176+ it ( 'invalid element but valid ReactNode' , ( ) => {
177+ expect ( supportNodeRef ( true ) ) . toBeFalsy ( ) ;
178+ expect ( supportNodeRef ( 'div' ) ) . toBeFalsy ( ) ;
179+ expect ( supportNodeRef ( 123 ) ) . toBeFalsy ( ) ;
180+ expect ( supportNodeRef ( < > </ > ) ) . toBeFalsy ( ) ;
181+ } ) ;
182+
183+ it ( 'FC' , ( ) => {
184+ const FC = ( ) => < div /> ;
185+ const RefFC = React . forwardRef ( FC ) ;
186+ expect ( supportNodeRef ( < FC /> ) ) . toBeFalsy ( ) ;
187+ expect ( supportNodeRef ( < RefFC /> ) ) . toBeTruthy ( ) ;
188+ } ) ;
189+ } ) ;
169190} ) ;
0 commit comments