Skip to content

Commit 35c1570

Browse files
committed
test(useMenuPress): a pointer that went down elsewhere is the menu's opening press
The menu press model's continuation suite, as the useMenuPress module record's FR11 names it. A handed finger picks only after UIMenu's handoff distance, under either touch model, counted from the first position the menu sees. Its lift before that acts on nothing, and a lift over no row leaves the menu open. A handed mouse follows the settle rule. A live gesture keeps the pointer, and the public hook returns no continuation. Ported from meta#8507. These cases fail until the next commit.
1 parent 5294225 commit 35c1570

1 file changed

Lines changed: 159 additions & 0 deletions

File tree

‎packages/core/src/hooks/useMenuPress.test.tsx‎

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import {
2222
__resetMenuPressForTest,
2323
useDropdownMenuPress,
2424
useMenuPress,
25+
type DropdownMenuPressReturn,
26+
type UseMenuPressReturn,
2527
} from './useMenuPress';
2628
import {MENU_PRESS_HANDOFF_PX, MENU_PRESS_SETTLE_MS} from './menuPressGesture';
2729
import {
@@ -39,6 +41,8 @@ interface HarnessProps {
3941
getScroller?: () => HTMLElement | null;
4042
onHighlight?: (row: HTMLElement | null) => void;
4143
touchModel?: 'pointer' | 'uikit';
44+
/** Receives the hook's return, for calls the trigger does not make. */
45+
pressRef?: {current: DropdownMenuPressReturn | null};
4246
}
4347

4448
function Harness({
@@ -49,6 +53,7 @@ function Harness({
4953
getScroller,
5054
onHighlight,
5155
touchModel,
56+
pressRef,
5257
}: HarnessProps) {
5358
const menuRef = useRef<HTMLDivElement>(null);
5459
const triggerRef = useRef<HTMLButtonElement>(null);
@@ -77,6 +82,9 @@ function Harness({
7782
onHighlight,
7883
touchModel,
7984
});
85+
if (pressRef != null) {
86+
pressRef.current = press;
87+
}
8088
return (
8189
<>
8290
<button
@@ -988,6 +996,145 @@ describe('useMenuPress — the opening finger hands off (DropdownMenu\u2019s tou
988996
});
989997
});
990998

999+
describe('useMenuPress — a pointer that went down elsewhere is the opening press (continueGesture, module FR11)', () => {
1000+
/** Hand pointer 7 to the menu, as a context menu's long press does. */
1001+
const continueAt = (
1002+
press: {current: DropdownMenuPressReturn | null},
1003+
pointerType: 'touch' | 'mouse',
1004+
) => {
1005+
act(() => {
1006+
press.current?.continueGesture({
1007+
pointerId: 7,
1008+
pointerType,
1009+
time: Date.now(),
1010+
});
1011+
});
1012+
};
1013+
const handed = {pointerType: 'touch', pointerId: 7};
1014+
1015+
it("with the 'uikit' touch model, a handed finger picks only after the handoff distance from the first position the menu sees", () => {
1016+
const onSelect = vi.fn();
1017+
const press: {current: DropdownMenuPressReturn | null} = {current: null};
1018+
render(<Harness touchModel="uikit" onSelect={onSelect} pressRef={press} />);
1019+
continueAt(press, 'touch');
1020+
fireEvent.pointerMove(row('B'), {...handed, clientX: 0, clientY: 4});
1021+
expect(row('B')).not.toHaveFocus();
1022+
fireEvent.pointerMove(row('B'), {...handed, clientX: 0, clientY: 20});
1023+
expect(row('B')).toHaveFocus();
1024+
fireEvent.pointerUp(row('B'), {...handed, clientX: 0, clientY: 20});
1025+
expect(onSelect).toHaveBeenCalledTimes(1);
1026+
expect(onSelect).toHaveBeenCalledWith('B');
1027+
});
1028+
1029+
it("with the 'pointer' touch model too, a handed finger picks only after the handoff distance", () => {
1030+
const onSelect = vi.fn();
1031+
const press: {current: DropdownMenuPressReturn | null} = {current: null};
1032+
render(<Harness onSelect={onSelect} pressRef={press} />);
1033+
continueAt(press, 'touch');
1034+
fireEvent.pointerMove(row('B'), {...handed, clientX: 0, clientY: 4});
1035+
expect(row('B')).not.toHaveFocus();
1036+
fireEvent.pointerMove(row('B'), {...handed, clientX: 0, clientY: 20});
1037+
expect(row('B')).toHaveFocus();
1038+
fireEvent.pointerUp(row('B'), {...handed, clientX: 0, clientY: 20});
1039+
expect(onSelect).toHaveBeenCalledTimes(1);
1040+
expect(onSelect).toHaveBeenCalledWith('B');
1041+
});
1042+
1043+
it('counts the distance from the first position the menu sees, not from where the finger was as it opened', () => {
1044+
const press: {current: DropdownMenuPressReturn | null} = {current: null};
1045+
render(<Harness touchModel="uikit" pressRef={press} />);
1046+
continueAt(press, 'touch');
1047+
// However far it went before the menu saw it, the first sample only
1048+
// marks where the distance starts.
1049+
fireEvent.pointerMove(row('C'), {...handed, clientX: 0, clientY: 40});
1050+
expect(row('C')).not.toHaveFocus();
1051+
fireEvent.pointerMove(row('C'), {
1052+
...handed,
1053+
clientX: 0,
1054+
clientY: 40 + MENU_PRESS_HANDOFF_PX - 1,
1055+
});
1056+
expect(row('C')).not.toHaveFocus();
1057+
fireEvent.pointerMove(row('C'), {
1058+
...handed,
1059+
clientX: 0,
1060+
clientY: 40 + MENU_PRESS_HANDOFF_PX,
1061+
});
1062+
expect(row('C')).toHaveFocus();
1063+
});
1064+
1065+
it('a handed finger lifted before the handoff acts on nothing, and its click is spent', () => {
1066+
const onSelect = vi.fn();
1067+
const onDismiss = vi.fn();
1068+
const press: {current: DropdownMenuPressReturn | null} = {current: null};
1069+
render(
1070+
<Harness
1071+
touchModel="uikit"
1072+
onSelect={onSelect}
1073+
onDismiss={onDismiss}
1074+
pressRef={press}
1075+
/>,
1076+
);
1077+
continueAt(press, 'touch');
1078+
fireEvent.pointerMove(row('B'), {...handed, clientX: 0, clientY: 3});
1079+
fireEvent.pointerUp(row('B'), {...handed, clientX: 0, clientY: 3});
1080+
fireEvent.click(row('B'), {detail: 1});
1081+
expect(onSelect).not.toHaveBeenCalled();
1082+
expect(onDismiss).not.toHaveBeenCalled();
1083+
});
1084+
1085+
it('a handed finger that slides past the handoff and lifts over no row leaves the menu open', () => {
1086+
const onSelect = vi.fn();
1087+
const onDismiss = vi.fn();
1088+
const press: {current: DropdownMenuPressReturn | null} = {current: null};
1089+
render(
1090+
<Harness onSelect={onSelect} onDismiss={onDismiss} pressRef={press} />,
1091+
);
1092+
continueAt(press, 'touch');
1093+
fireEvent.pointerMove(row('B'), {...handed, clientX: 0, clientY: 0});
1094+
fireEvent.pointerMove(screen.getByTestId('outside'), {
1095+
...handed,
1096+
clientX: 0,
1097+
clientY: 30,
1098+
});
1099+
fireEvent.pointerUp(screen.getByTestId('outside'), {
1100+
...handed,
1101+
clientX: 0,
1102+
clientY: 30,
1103+
});
1104+
expect(onSelect).not.toHaveBeenCalled();
1105+
expect(onDismiss).not.toHaveBeenCalled();
1106+
});
1107+
1108+
it('a handed mouse follows the settle rule: released at once it acts on nothing; moved onto a row it picks it', () => {
1109+
const onSelect = vi.fn();
1110+
const press: {current: DropdownMenuPressReturn | null} = {current: null};
1111+
render(<Harness onSelect={onSelect} pressRef={press} />);
1112+
const handedMouse = {pointerType: 'mouse', pointerId: 7, button: 0};
1113+
continueAt(press, 'mouse');
1114+
fireEvent.pointerUp(row('A'), handedMouse);
1115+
expect(onSelect).not.toHaveBeenCalled();
1116+
continueAt(press, 'mouse');
1117+
fireEvent.pointerMove(row('B'), handedMouse);
1118+
expect(row('B')).toHaveFocus();
1119+
fireEvent.pointerUp(row('B'), handedMouse);
1120+
expect(onSelect).toHaveBeenCalledTimes(1);
1121+
expect(onSelect).toHaveBeenCalledWith('B');
1122+
});
1123+
1124+
it('is ignored while another gesture is live', () => {
1125+
const onSelect = vi.fn();
1126+
const press: {current: DropdownMenuPressReturn | null} = {current: null};
1127+
render(<Harness onSelect={onSelect} pressRef={press} />);
1128+
fireEvent.pointerDown(row('A'), touch(1));
1129+
continueAt(press, 'touch');
1130+
fireEvent.pointerMove(row('C'), {...handed, clientX: 0, clientY: 40});
1131+
expect(row('C')).not.toHaveFocus();
1132+
fireEvent.pointerUp(row('A'), touch(1));
1133+
expect(onSelect).toHaveBeenCalledTimes(1);
1134+
expect(onSelect).toHaveBeenCalledWith('A');
1135+
});
1136+
});
1137+
9911138
describe('useMenuPress — the public hook takes no touch model option', () => {
9921139
function PublicHarness({onSelect}: {onSelect: (label: string) => void}) {
9931140
const menuRef = useRef<HTMLDivElement>(null);
@@ -1038,6 +1185,18 @@ describe('useMenuPress — the public hook takes no touch model option', () => {
10381185
fireEvent.pointerUp(row('A'), {...touch(), clientX: 0, clientY: 2});
10391186
expect(onSelect).toHaveBeenCalledWith('A');
10401187
});
1188+
1189+
it('returns no continuation: continueGesture is core-internal', () => {
1190+
const seen: {current: UseMenuPressReturn | null} = {current: null};
1191+
function Probe() {
1192+
const menuRef = useRef<HTMLDivElement>(null);
1193+
seen.current = useMenuPress({menuRef, itemSelector: ITEM_SELECTOR});
1194+
return <div ref={menuRef} role="menu" />;
1195+
}
1196+
render(<Probe />);
1197+
expect(seen.current).not.toBeNull();
1198+
expect('continueGesture' in (seen.current as object)).toBe(false);
1199+
});
10411200
});
10421201

10431202
describe('useMenuPress — edge autoscroll', () => {

0 commit comments

Comments
 (0)