Skip to content

Commit 1f3f704

Browse files
committed
test: add test case
1 parent 2488bb6 commit 1f3f704

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

src/hooks/useScrollDrag.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,15 @@ export default function useScrollDrag(
3838
});
3939
};
4040

41-
const onMouseDown = () => {
42-
mouseDownLock = true;
41+
const onMouseDown = (e: MouseEvent) => {
42+
// Skip if nest List has handled this event
43+
const event = e as MouseEvent & {
44+
_virtualHandled?: boolean;
45+
};
46+
if (!event._virtualHandled) {
47+
event._virtualHandled = true;
48+
mouseDownLock = true;
49+
}
4350
};
4451
const onMouseUp = () => {
4552
mouseDownLock = false;
@@ -52,7 +59,7 @@ export default function useScrollDrag(
5259

5360
if (mouseY <= top) {
5461
const diff = top - mouseY;
55-
offset = smoothScrollOffset(diff);
62+
offset = -smoothScrollOffset(diff);
5663
continueScroll();
5764
} else if (mouseY >= bottom) {
5865
const diff = mouseY - bottom;

tests/scroll.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ jest.mock('../src/ScrollBar', () => {
2929
describe('List.Scroll', () => {
3030
let mockElement;
3131
let boundingRect = {
32+
top: 0,
33+
bottom: 0,
3234
width: 100,
3335
height: 100,
3436
};
@@ -54,6 +56,8 @@ describe('List.Scroll', () => {
5456

5557
beforeEach(() => {
5658
boundingRect = {
59+
top: 0,
60+
bottom: 0,
5761
width: 100,
5862
height: 100,
5963
};
@@ -552,4 +556,48 @@ describe('List.Scroll', () => {
552556
'0',
553557
);
554558
});
559+
560+
it('mouse down drag', () => {
561+
const onScroll = jest.fn();
562+
const { container } = render(
563+
<List
564+
component="ul"
565+
itemKey="id"
566+
itemHeight={20}
567+
height={100}
568+
data={genData(100)}
569+
onScroll={onScroll}
570+
>
571+
{({ id }) => <li>{id}</li>}
572+
</List>,
573+
);
574+
575+
function dragDown(mouseY) {
576+
fireEvent.mouseDown(container.querySelector('li'));
577+
578+
let moveEvent = createEvent.mouseMove(container.querySelector('li'));
579+
moveEvent.pageY = mouseY;
580+
fireEvent(container.querySelector('li'), moveEvent);
581+
582+
act(() => {
583+
jest.advanceTimersByTime(100);
584+
});
585+
586+
fireEvent.mouseUp(container.querySelector('li'));
587+
}
588+
589+
function getScrollTop() {
590+
const innerEle = container.querySelector('.rc-virtual-list-holder-inner');
591+
const { transform } = innerEle.style;
592+
return Number(transform.match(/\d+/)[0]);
593+
}
594+
595+
// Drag down
596+
dragDown(100);
597+
expect(getScrollTop()).toBeGreaterThan(0);
598+
599+
// Drag up
600+
dragDown(-100);
601+
expect(getScrollTop()).toBe(0);
602+
});
555603
});

0 commit comments

Comments
 (0)