@@ -61,15 +61,18 @@ describe('Ref', () => {
6161 } ) ;
6262
6363 it ( 'passes an updated node' , ( ) => {
64+ const ComponentA = ( ) => < div /> ;
65+ const ComponentB = ( ) => < button /> ;
66+
6467 const innerRef = jest . fn ( ) ;
6568 const wrapper = mount (
6669 < Ref innerRef = { innerRef } >
67- < div />
70+ < ComponentA />
6871 </ Ref > ,
6972 ) ;
7073
7174 expect ( innerRef ) . toHaveBeenCalledWith ( expect . objectContaining ( { tagName : 'DIV' } ) ) ;
72- wrapper . setProps ( { children : < button /> } ) ;
75+ wrapper . setProps ( { children : < ComponentB /> } ) ;
7376
7477 expect ( innerRef ) . toHaveBeenCalledTimes ( 2 ) ;
7578 expect ( innerRef ) . toHaveBeenCalledWith ( expect . objectContaining ( { tagName : 'BUTTON' } ) ) ;
@@ -110,36 +113,62 @@ describe('Ref', () => {
110113 } ) ;
111114
112115 it ( 'resolves once on node/ref change' , ( ) => {
116+ const ComponentA = ( ) => < div /> ;
117+ const ComponentB = ( ) => < span /> ;
118+
113119 const initialRef = jest . fn ( ) ;
114120 const updatedRef = jest . fn ( ) ;
121+
115122 const wrapper = mount (
116123 < Ref innerRef = { initialRef } >
117- < div />
124+ < ComponentA />
118125 </ Ref > ,
119126 ) ;
120127
121128 expect ( initialRef ) . toHaveBeenCalledTimes ( 1 ) ;
122129 expect ( updatedRef ) . not . toHaveBeenCalled ( ) ;
123130
131+ // ---
132+
124133 jest . resetAllMocks ( ) ;
125- wrapper . setProps ( { children : < span /> , innerRef : updatedRef } ) ;
134+
135+ wrapper . setProps ( { children : < ComponentB /> , innerRef : updatedRef } ) ;
126136
127137 expect ( initialRef ) . not . toHaveBeenCalled ( ) ;
128138 expect ( updatedRef ) . toHaveBeenCalledTimes ( 1 ) ;
129139 } ) ;
130140
131141 it ( 'always returns "null" for react-test-renderer' , ( ) => {
142+ const Component = ( ) => < div /> ;
132143 const innerRef = jest . fn ( ) ;
133- const ref = jest . fn ( ) ;
134144
135145 create (
136146 < Ref innerRef = { innerRef } >
137- < div ref = { ref } />
147+ < Component />
138148 </ Ref > ,
139149 ) ;
140150
141151 expect ( innerRef ) . toHaveBeenCalledWith ( null ) ;
142- expect ( ref ) . toHaveBeenCalledWith ( null ) ;
152+ } ) ;
153+
154+ it ( 'handles unmount' , ( ) => {
155+ const Component = ( ) => < div /> ;
156+ const innerRef = jest . fn ( ) ;
157+
158+ const wrapper = mount (
159+ < Ref innerRef = { innerRef } >
160+ < Component />
161+ </ Ref > ,
162+ ) ;
163+
164+ expect ( innerRef ) . toHaveBeenCalledWith ( expect . objectContaining ( { tagName : 'DIV' } ) ) ;
165+
166+ // ---
167+
168+ jest . resetAllMocks ( ) ;
169+ wrapper . unmount ( ) ;
170+
171+ expect ( innerRef ) . toHaveBeenCalledWith ( null ) ;
143172 } ) ;
144173 } ) ;
145174
@@ -201,6 +230,65 @@ describe('Ref', () => {
201230 } ) ;
202231 } ) ;
203232
233+ describe ( 'kind="forward" (plain element)' , ( ) => {
234+ it ( 'applies refs' , ( ) => {
235+ const innerRef = jest . fn ( ) ;
236+ const elRef = jest . fn ( ) ;
237+
238+ mount (
239+ < Ref innerRef = { innerRef } >
240+ < button ref = { elRef } />
241+ </ Ref > ,
242+ ) ;
243+
244+ expect ( innerRef ) . toHaveBeenCalledWith ( expect . objectContaining ( { tagName : 'BUTTON' } ) ) ;
245+ expect ( elRef ) . toHaveBeenCalledWith ( expect . objectContaining ( { tagName : 'BUTTON' } ) ) ;
246+ } ) ;
247+
248+ it ( 'passes an updated node' , ( ) => {
249+ const innerRef = jest . fn ( ) ;
250+
251+ mount (
252+ < Ref innerRef = { innerRef } >
253+ < button />
254+ </ Ref > ,
255+ ) ;
256+
257+ expect ( innerRef ) . toHaveBeenCalledWith ( expect . objectContaining ( { tagName : 'BUTTON' } ) ) ;
258+
259+ // ---
260+
261+ jest . resetAllMocks ( ) ;
262+
263+ mount (
264+ < Ref innerRef = { innerRef } >
265+ < div />
266+ </ Ref > ,
267+ ) ;
268+
269+ expect ( innerRef ) . toHaveBeenCalledWith ( expect . objectContaining ( { tagName : 'DIV' } ) ) ;
270+ } ) ;
271+
272+ it ( 'handles unmount' , ( ) => {
273+ const innerRef = jest . fn ( ) ;
274+
275+ const wrapper = mount (
276+ < Ref innerRef = { innerRef } >
277+ < button />
278+ </ Ref > ,
279+ ) ;
280+
281+ expect ( innerRef ) . toHaveBeenCalledWith ( expect . objectContaining ( { tagName : 'BUTTON' } ) ) ;
282+
283+ // ---
284+
285+ jest . resetAllMocks ( ) ;
286+ wrapper . unmount ( ) ;
287+
288+ expect ( innerRef ) . toHaveBeenCalledWith ( null ) ;
289+ } ) ;
290+ } ) ;
291+
204292 describe ( 'kind="self"' , ( ) => {
205293 it ( 'works with "forwardRef" API' , ( ) => {
206294 const forwardedRef = React . createRef < HTMLButtonElement > ( ) ;
@@ -239,7 +327,10 @@ describe('Ref', () => {
239327 expect ( innerRef ) . toHaveBeenCalledWith ( expect . objectContaining ( { tagName : 'DIV' } ) ) ;
240328 expect ( outerRef ) . toHaveBeenCalledWith ( expect . objectContaining ( { tagName : 'DIV' } ) ) ;
241329
330+ // ---
331+
242332 jest . resetAllMocks ( ) ;
333+
243334 wrapper . setProps ( {
244335 children : (
245336 < Ref innerRef = { innerRef } >
0 commit comments