Skip to content

Commit

Permalink
Add test for keyboard movement
Browse files Browse the repository at this point in the history
  • Loading branch information
benchristel committed Jun 26, 2024
1 parent 0dc02f4 commit 652c5b8
Showing 1 changed file with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ function TestDraggable(props: {
constrain,
gestureTarget,
});
return <button ref={gestureTarget}>dragging: {String(dragging)}</button>;
return (
<button ref={gestureTarget} tabIndex={0}>
dragging: {String(dragging)}
</button>
);
}

describe("useDraggable", () => {
Expand Down Expand Up @@ -177,6 +181,45 @@ describe("useDraggable", () => {
// - (11, 9), neither userTransform nor the inverse was applied.
expect(onMoveSpy).toHaveBeenCalledWith([10.5, 9.5]);
});

it("moves a draggable element with the keyboard", async () => {
// Arrange: a 200x200px graph with a 20-unit range in each dimension.
// One graph unit = 10px.
const mafsProps = {
width: 200,
height: 200,
viewBox: {
x: [-10, 10] as Interval,
y: [-10, 10] as Interval,
padding: 0,
},
};
const onMoveSpy = jest.fn();
render(
<Mafs {...mafsProps}>
<TestDraggable
point={[0, 0]}
onMove={onMoveSpy}
constrain={(point) => [
Math.round(point[0]),
Math.round(point[1]),
]}
/>
</Mafs>,
);
// focus the draggable element
await userEvent.tab();
await userEvent.tab();

// Pre-assert: the draggable element is actually focused
expect(screen.getByRole("button")).toHaveFocus();

// Act:
await userEvent.keyboard("{arrowright>1}{arrowup>1}");

// Assert: the element moved one step to the right and then one step up
expect(onMoveSpy.mock.calls).toEqual([[[1, 0]], [[0, 1]]]);
});
});

function mouseDownAt(element: Element, clientX: number, clientY: number) {
Expand Down

0 comments on commit 652c5b8

Please sign in to comment.