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,13 +13,28 @@ 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
1829 beforeEach ( ( ) => {
1930 errSpy . mockReset ( ) ;
2031 } ) ;
2132
33+ it ( 'ensure is React 19' , ( ) => {
34+ // Version should start with 19
35+ expect ( React . version ) . toMatch ( / ^ 1 9 / ) ;
36+ } ) ;
37+
2238 it ( 'getNodeRef' , ( ) => {
2339 const ref = React . createRef < HTMLDivElement > ( ) ;
2440 const node = < div ref = { ref } /> ;
@@ -27,4 +43,37 @@ describe('ref: React 19', () => {
2743
2844 expect ( errSpy ) . not . toHaveBeenCalled ( ) ;
2945 } ) ;
46+
47+ it ( 'useComposeRef' , ( ) => {
48+ const Demo = ( { children } : { children : React . ReactElement } ) => {
49+ const ref = React . useRef < HTMLDivElement > ( null ) ;
50+ const childRef = getNodeRef ( children ) ; // Should get child real `ref` props
51+ const mergedRef = useComposeRef ( ref , childRef ) ;
52+
53+ const [ childClassName , setChildClassName ] = React . useState < string | null > (
54+ null ,
55+ ) ;
56+ React . useEffect ( ( ) => {
57+ setChildClassName ( ref . current ?. className ) ;
58+ } , [ ] ) ;
59+
60+ return (
61+ < >
62+ { React . cloneElement ( children , { ref : mergedRef } ) }
63+ < div className = "test-output" > { childClassName } </ div >
64+ </ >
65+ ) ;
66+ } ;
67+
68+ const outerRef = React . createRef < HTMLDivElement > ( ) ;
69+
70+ const { container } = render (
71+ < Demo >
72+ < div className = "bamboo" ref = { outerRef } />
73+ </ Demo > ,
74+ ) ;
75+
76+ expect ( outerRef . current ?. className ) . toBe ( 'bamboo' ) ;
77+ expect ( container . querySelector ( '.test-output' ) ?. textContent ) . toBe ( 'bamboo' ) ;
78+ } ) ;
3079} ) ;
0 commit comments