Skip to content

Commit

Permalink
fix(event): assign pointer coords to MouseEvent (#1039)
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-fritsche authored Jan 15, 2025
1 parent 7b11b0e commit 8528972
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/event/createEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,14 @@ function initMouseEvent(
button,
buttons,
relatedTarget,
}: MouseEventInit & {x?: number; y?: number},
offsetX,
offsetY,
pageX,
pageY,
}: MouseEventInit &
Partial<
Pick<MouseEvent, 'x' | 'y' | 'offsetX' | 'offsetY' | 'pageX' | 'pageY'>
>,
) {
assignProps(event, {
screenX: sanitizeNumber(screenX),
Expand All @@ -231,6 +238,10 @@ function initMouseEvent(
button: sanitizeNumber(button),
buttons: sanitizeNumber(buttons),
relatedTarget,
offsetX: sanitizeNumber(offsetX),
offsetY: sanitizeNumber(offsetY),
pageX: sanitizeNumber(pageX),
pageY: sanitizeNumber(pageY),
})
}

Expand Down
22 changes: 22 additions & 0 deletions tests/pointer/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,25 @@ test('move touch over elements', async () => {
div - click: primary
`)
})

test('declare pointer coordinates', async () => {
const {element, getEvents, user} = setup(`<div></div>`)

const coords: Partial<MouseEvent> = {
x: 1,
y: 2,
offsetX: 3,
offsetY: 4,
pageX: 5,
pageY: 6,
screenX: 7,
screenY: 8,
}

await user.pointer({target: element, coords})

// .toEqual(expect.objectContaining) yields a misleading diff
Object.entries(coords).forEach(([prop, value]) => {
expect(getEvents('mouseover')[0]).toHaveProperty(prop, value)
})
})

0 comments on commit 8528972

Please sign in to comment.