11/* eslint-disable no-eval */
22import React from 'react' ;
3- import { getNodeRef } from '../src/ref' ;
3+ import { getNodeRef , useComposeRef } from '../src/ref' ;
4+ import { render } from '@testing-library/react' ;
45
56jest . mock ( 'react' , ( ) => {
67 const react19 = jest . requireActual ( 'react-19' ) ;
@@ -12,6 +13,16 @@ jest.mock('react-dom', () => {
1213 return reactDom19 ;
1314} ) ;
1415
16+ jest . mock ( 'react-dom/client' , ( ) => {
17+ const reactDom19Client = jest . requireActual ( 'react-dom-19/client' ) ;
18+ return reactDom19Client ;
19+ } ) ;
20+
21+ jest . mock ( 'react-dom/test-utils' , ( ) => {
22+ const reactDom19Test = jest . requireActual ( 'react-dom-19/test-utils' ) ;
23+ return reactDom19Test ;
24+ } ) ;
25+
1526describe ( 'ref: React 19' , ( ) => {
1627 const errSpy = jest . spyOn ( console , 'error' ) ;
1728
@@ -27,4 +38,37 @@ describe('ref: React 19', () => {
2738
2839 expect ( errSpy ) . not . toHaveBeenCalled ( ) ;
2940 } ) ;
41+
42+ it ( 'useComposeRef' , ( ) => {
43+ const Demo = ( { children } : { children : React . ReactElement } ) => {
44+ const ref = React . useRef < HTMLDivElement > ( null ) ;
45+ const childRef = getNodeRef ( children ) ; // Should get child real `ref` props
46+ const mergedRef = useComposeRef ( ref , childRef ) ;
47+
48+ const [ childClassName , setChildClassName ] = React . useState < string | null > (
49+ null ,
50+ ) ;
51+ React . useEffect ( ( ) => {
52+ setChildClassName ( ref . current ?. className ) ;
53+ } , [ ] ) ;
54+
55+ return (
56+ < >
57+ { React . cloneElement ( children , { ref : mergedRef } ) }
58+ < div className = "test-output" > { childClassName } </ div >
59+ </ >
60+ ) ;
61+ } ;
62+
63+ const outerRef = React . createRef < HTMLDivElement > ( ) ;
64+
65+ const { container } = render (
66+ < Demo >
67+ < div className = "bamboo" ref = { outerRef } />
68+ </ Demo > ,
69+ ) ;
70+
71+ expect ( outerRef . current ?. className ) . toBe ( 'bamboo' ) ;
72+ expect ( container . querySelector ( '.test-output' ) ?. textContent ) . toBe ( 'bamboo' ) ;
73+ } ) ;
3074} ) ;
0 commit comments