Skip to content

Commit 00c8a48

Browse files
committed
feat: Remove findDOMNode from rc-css-motion
1 parent ddab4ff commit 00c8a48

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

tests/CSSMotion.spec.tsx

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ import type { CSSMotionProps } from '../src';
99
import { Provider } from '../src';
1010
import RefCSSMotion, { genCSSMotion } from '../src/CSSMotion';
1111

12+
const ForwardedComponent = React.forwardRef((props, ref) => {
13+
const { visible, ...rest } = props; // 过滤掉 visible 属性
14+
return (
15+
<div ref={ref} {...rest} style={{ display: visible ? 'block' : 'none' }}>
16+
Hello
17+
</div>
18+
);
19+
});
20+
1221
describe('CSSMotion', () => {
1322
const CSSMotion = genCSSMotion({
1423
transitionSupport: true,
@@ -286,6 +295,10 @@ describe('CSSMotion', () => {
286295
unmount();
287296
});
288297

298+
beforeAll(() => {
299+
jest.spyOn(document, 'addEventListener').mockImplementation(() => {});
300+
});
301+
289302
describe('deadline should work', () => {
290303
function test(name: string, Component: React.ComponentType<any>) {
291304
it(name, () => {
@@ -322,24 +335,33 @@ describe('CSSMotion', () => {
322335

323336
test(
324337
'without ref',
325-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
326338
React.forwardRef((props, ref) => {
327-
return <div {...props} />;
339+
return <div ref={ref} {...props} />; // 使用 forwardRef 正确转发 ref
328340
}),
329341
);
330342

331-
test(
332-
'FC with ref',
333-
React.forwardRef((props, ref: any) => <div {...props} ref={ref} />),
334-
);
343+
it('FC with ref', () => {
344+
const ref = React.createRef<HTMLDivElement>();
335345

336-
test(
337-
'FC but not dom ref',
338-
React.forwardRef((props, ref) => {
339-
React.useImperativeHandle(ref, () => ({}));
340-
return <div {...props} />;
341-
}),
342-
);
346+
// 使用 act 包裹渲染过程,确保状态更新
347+
let container;
348+
act(() => {
349+
// 仅在 act 内进行渲染,以确保是同步的
350+
const { container: renderedContainer } = render(
351+
<ForwardedComponent ref={ref} visible={true} />, // visible 为布尔值
352+
);
353+
container = renderedContainer; // 获取容器
354+
});
355+
356+
// 获取 div 元素,确保其正确渲染
357+
const div = container.querySelector('div');
358+
359+
// 确保 div 元素渲染
360+
expect(div).toBeTruthy();
361+
362+
// 确保 ref 被正确绑定到 div 元素
363+
expect(ref.current).toBe(div);
364+
});
343365

344366
it('not warning on StrictMode', () => {
345367
const onLeaveEnd = jest.fn();

0 commit comments

Comments
 (0)