Skip to content

Commit 4f28cc4

Browse files
author
刘欢
committed
test: add coverage for all handles disabled scenario
1 parent a7db356 commit 4f28cc4

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

src/hooks/useDrag.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ function useDrag(
7878
}
7979
triggerChange(changeValues);
8080

81+
// Optional callback for drag change (not used in current implementation)
8182
if (onDragChange) {
8283
onDragChange({
8384
rawValues: nextValues,
@@ -92,6 +93,7 @@ function useDrag(
9293
(valueIndex: number, offsetPercent: number, deleteMark: boolean) => {
9394
if (valueIndex === -1) {
9495
// >>>> Dragging on the track
96+
// Defensive: should not happen as Tracks/index.tsx blocks this when any handle is disabled
9597
if (isHandleDisabled && originValues.some((_, index) => isHandleDisabled(index))) {
9698
return;
9799
}
@@ -129,8 +131,8 @@ function useDrag(
129131
const onStartMove: OnStartMove = (e, valueIndex, startValues?: number[]) => {
130132
e.stopPropagation();
131133

132-
// 如果是点击 track 触发的,需要传入变化后的初始值,而不能直接用 rawValues
133134
const initialValues = startValues || rawValues;
135+
// Defensive: should not happen as Handle.tsx blocks this when handle is disabled
134136
if (isHandleDisabled && isHandleDisabled(valueIndex)) {
135137
return;
136138
}
@@ -148,7 +150,7 @@ function useDrag(
148150
// We declare it here since closure can't get outer latest value
149151
let deleteMark = false;
150152

151-
// Internal trigger event
153+
// Optional callback for drag start (not used in current implementation)
152154
if (onDragStart) {
153155
onDragStart({
154156
rawValues: initialValues,

tests/Range.test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,5 +981,27 @@ describe('Range', () => {
981981

982982
expect(onChange).not.toHaveBeenCalled();
983983
});
984+
985+
it('all handles disabled: find nearest enabled returns -1', () => {
986+
// This test specifically covers line 426 in Slider.tsx
987+
// When all handles are disabled and clicking near one,
988+
// the nearestIndex search returns -1 and returns early
989+
const onChange = jest.fn();
990+
const { container } = render(
991+
<Slider range defaultValue={[0, 50, 100]} disabled={[true, true, true]} onChange={onChange} />,
992+
);
993+
994+
// Click at position 10 (near first disabled handle)
995+
const rail = container.querySelector('.rc-slider-rail');
996+
const mouseDown = createEvent.mouseDown(rail);
997+
Object.defineProperties(mouseDown, {
998+
clientX: { get: () => 10 },
999+
clientY: { get: () => 10 },
1000+
});
1001+
fireEvent(rail, mouseDown);
1002+
1003+
// Should not trigger onChange because all handles are disabled
1004+
expect(onChange).not.toHaveBeenCalled();
1005+
});
9841006
});
9851007
});

0 commit comments

Comments
 (0)