-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathref-19.test.tsx
More file actions
97 lines (78 loc) · 2.53 KB
/
ref-19.test.tsx
File metadata and controls
97 lines (78 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* eslint-disable no-eval */
import React from 'react';
import { getNodeRef, useComposeRef, supportRef } from '../src/ref';
import { render } from '@testing-library/react';
jest.mock('react', () => {
const react19 = jest.requireActual('react-19');
return react19;
});
jest.mock('react-dom', () => {
const reactDom19 = jest.requireActual('react-dom-19');
return reactDom19;
});
jest.mock('react-dom/client', () => {
const reactDom19Client = jest.requireActual('react-dom-19/client');
return reactDom19Client;
});
jest.mock('react-dom/test-utils', () => {
const reactDom19Test = jest.requireActual('react-dom-19/test-utils');
return reactDom19Test;
});
describe('ref: React 19', () => {
const errSpy = jest.spyOn(console, 'error');
beforeEach(() => {
errSpy.mockReset();
});
it('ensure is React 19', () => {
// Version should start with 19
expect(React.version).toMatch(/^19/);
});
it('getNodeRef', () => {
const ref = React.createRef<HTMLDivElement>();
const node = <div ref={ref} />;
expect(getNodeRef(node)).toBe(ref);
expect(errSpy).not.toHaveBeenCalled();
});
it('useComposeRef', () => {
const Demo: React.FC<React.PropsWithChildren> = ({ children }) => {
const ref = React.useRef<HTMLDivElement>(null);
const childRef = getNodeRef(children); // Should get child real `ref` props
const mergedRef = useComposeRef(ref, childRef);
const [childClassName, setChildClassName] = React.useState<string | null>(
null,
);
React.useEffect(() => {
setChildClassName(ref.current?.className);
}, []);
return (
<>
{React.cloneElement<{ ref?: any }>(
children as React.ReactElement<any>,
{ ref: mergedRef },
)}
<div className="test-output">{childClassName}</div>
</>
);
};
const outerRef = React.createRef<HTMLDivElement>();
const { container } = render(
<Demo>
<div className="bamboo" ref={outerRef} />
</Demo>,
);
expect(outerRef.current?.className).toBe('bamboo');
expect(container.querySelector('.test-output')?.textContent).toBe('bamboo');
});
it('supportRef with not provide ref', () => {
const Empty = () => <div />;
const Checker = ({ children }: { children: React.ReactElement }) => {
return <p>{String(supportRef(children))}</p>;
};
const { container } = render(
<Checker>
<Empty />
</Checker>,
);
expect(container.querySelector('p')?.textContent).toBe('true');
});
});