1
1
/* eslint-disable no-eval */
2
2
import { fireEvent , render } from '@testing-library/react' ;
3
- import React from 'react' ;
3
+ import React , { ReactNode } from 'react' ;
4
4
import 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' ;
6
11
7
12
describe ( 'ref' , ( ) => {
8
13
describe ( 'composeRef' , ( ) => {
@@ -12,7 +17,7 @@ describe('ref', () => {
12
17
13
18
const mergedRef = composeRef ( refFunc1 , refFunc2 ) ;
14
19
const testRefObj = { } ;
15
- mergedRef ( testRefObj ) ;
20
+ ( mergedRef as any ) ( testRefObj ) ;
16
21
expect ( refFunc1 ) . toHaveBeenCalledWith ( testRefObj ) ;
17
22
expect ( refFunc2 ) . toHaveBeenCalledWith ( testRefObj ) ;
18
23
} ) ;
@@ -25,7 +30,7 @@ describe('ref', () => {
25
30
26
31
it ( 'useComposeRef' , ( ) => {
27
32
const Demo = ( { ref1, ref2 } ) => {
28
- const mergedRef = useComposeRef ( ref1 , ref2 ) ;
33
+ const mergedRef = useComposeRef < HTMLDivElement > ( ref1 , ref2 ) ;
29
34
return < div ref = { mergedRef } /> ;
30
35
} ;
31
36
@@ -70,14 +75,14 @@ describe('ref', () => {
70
75
} ) ;
71
76
72
77
describe ( 'supportRef' , ( ) => {
73
- class Holder extends React . Component {
78
+ class Holder extends React . Component < { children : ReactNode } > {
74
79
render ( ) {
75
80
return this . props . children ;
76
81
}
77
82
}
78
83
79
84
it ( 'function component' , ( ) => {
80
- const holderRef = React . createRef ( ) ;
85
+ const holderRef = React . createRef < Holder > ( ) ;
81
86
82
87
function FC ( ) {
83
88
return < div /> ;
@@ -93,7 +98,7 @@ describe('ref', () => {
93
98
} ) ;
94
99
95
100
it ( 'arrow function component' , ( ) => {
96
- const holderRef = React . createRef ( ) ;
101
+ const holderRef = React . createRef < Holder > ( ) ;
97
102
98
103
// Use eval since jest will convert arrow function to function
99
104
const FC = eval ( '() => null' ) ;
@@ -107,7 +112,7 @@ describe('ref', () => {
107
112
} ) ;
108
113
109
114
it ( 'forwardRef function component' , ( ) => {
110
- const holderRef = React . createRef ( ) ;
115
+ const holderRef = React . createRef < Holder > ( ) ;
111
116
112
117
const FRC = React . forwardRef ( ( ) => < div /> ) ;
113
118
render (
@@ -120,7 +125,7 @@ describe('ref', () => {
120
125
} ) ;
121
126
122
127
it ( 'class component' , ( ) => {
123
- const holderRef = React . createRef ( ) ;
128
+ const holderRef = React . createRef < Holder > ( ) ;
124
129
125
130
class CC extends React . Component {
126
131
state = { } ;
@@ -139,7 +144,7 @@ describe('ref', () => {
139
144
} ) ;
140
145
141
146
it ( 'memo of function component' , ( ) => {
142
- const holderRef = React . createRef ( ) ;
147
+ const holderRef = React . createRef < Holder > ( ) ;
143
148
144
149
const FC = ( ) => < div /> ;
145
150
const MemoFC = React . memo ( FC ) ;
@@ -153,7 +158,7 @@ describe('ref', () => {
153
158
} ) ;
154
159
155
160
it ( 'memo of forwardRef function component' , ( ) => {
156
- const holderRef = React . createRef ( ) ;
161
+ const holderRef = React . createRef < Holder > ( ) ;
157
162
158
163
const FRC = React . forwardRef ( ( ) => < div /> ) ;
159
164
const MemoFC = React . memo ( FRC ) ;
@@ -166,4 +171,20 @@ describe('ref', () => {
166
171
expect ( supportRef ( holderRef . current . props . children ) ) . toBeTruthy ( ) ;
167
172
} ) ;
168
173
} ) ;
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
+ } ) ;
169
190
} ) ;
0 commit comments