Skip to content

Commit c4de7b7

Browse files
committed
test: add unit test
1 parent f63f76a commit c4de7b7

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

tests/Virtual.spec.tsx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import { resetWarned } from 'rc-util/lib/warning';
66
import React from 'react';
77
import { VirtualTable, type Reference, type VirtualTableProps } from '../src';
88

9+
const identity = (value: any) => value;
10+
911
global.scrollToConfig = null;
12+
global.collectGetScrollInfoReturn = identity;
1013

1114
vi.mock('rc-virtual-list', async () => {
1215
const RealVirtualList = ((await vi.importActual('rc-virtual-list')) as any).default;
@@ -20,6 +23,10 @@ vi.mock('rc-virtual-list', async () => {
2023
global.scrollToConfig = config;
2124
return myRef.current.scrollTo(config);
2225
},
26+
getScrollInfo: () => {
27+
const originResult = myRef.current.getScrollInfo();
28+
return global.collectGetScrollInfoReturn(originResult);
29+
},
2330
}));
2431

2532
return <RealVirtualList ref={myRef} {...props} data-scroll-width={props.scrollWidth} />;
@@ -59,7 +66,8 @@ describe('Table.Virtual', () => {
5966
beforeEach(() => {
6067
scrollLeftCalled = false;
6168
setScrollLeft.mockReset();
62-
global.scrollToConfig = null;
69+
global.scrollToConfig = vi.fn(identity);
70+
// global.collectGetScrollInfoReturn.mockReset();
6371
vi.useFakeTimers();
6472
resetWarned();
6573
});
@@ -563,4 +571,26 @@ describe('Table.Virtual', () => {
563571
).toBeTruthy();
564572
});
565573
});
574+
575+
/**
576+
* In antd, we need to call the scrollTop method through ref to achieve scrolling.
577+
* see: https://github.com/ant-design/ant-design/issues/54734
578+
*/
579+
it('should get and set scrollTop correctly', async () => {
580+
const ref = React.createRef<any>();
581+
582+
global.collectGetScrollInfoReturn = (origin: any) => ({
583+
...origin,
584+
y: 50,
585+
});
586+
587+
getTable({ internalRefs: { body: ref } });
588+
589+
expect(ref.current.scrollTop).toBe(50);
590+
591+
ref.current.scrollTop = 200;
592+
expect(global.scrollToConfig).toEqual({
593+
top: 200,
594+
});
595+
});
566596
});

0 commit comments

Comments
 (0)