Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement arrow interactions as arrowPress #300

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/__tests__/arrowpress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from 'react'
import {render, screen} from '@testing-library/react'
import userEvent from '../../src'

test('should handle ArrowDown', () => {
const onKeyDown = jest.fn()
const onKeyUp = jest.fn()

render(<div data-testid="element" onKeyDown={onKeyDown} onKeyUp={onKeyUp} />)

const div = screen.getByTestId('element')

userEvent.arrowPress(div, 'ArrowDown')
expect(onKeyDown).toHaveBeenCalledWith(expect.objectContaining({}))
expect(onKeyUp).toHaveBeenCalledWith(expect.objectContaining({}))
})

test('should handle ArrowLeft', () => {
const onKeyDown = jest.fn()
const onKeyUp = jest.fn()

render(<div data-testid="element" onKeyDown={onKeyDown} onKeyUp={onKeyUp} />)

const div = screen.getByTestId('element')

userEvent.arrowPress(div, 'ArrowLeft')
expect(onKeyDown).toHaveBeenCalledWith(expect.objectContaining({}))
expect(onKeyUp).toHaveBeenCalledWith(expect.objectContaining({}))
})

test('should handle ArrowRight', () => {
const onKeyDown = jest.fn()
const onKeyUp = jest.fn()

render(<div data-testid="element" onKeyDown={onKeyDown} onKeyUp={onKeyUp} />)

const div = screen.getByTestId('element')

userEvent.arrowPress(div, 'ArrowRight')
expect(onKeyDown).toHaveBeenCalledWith(expect.objectContaining({}))
expect(onKeyUp).toHaveBeenCalledWith(expect.objectContaining({}))
})

test('should handle ArrowUp', () => {
const onKeyDown = jest.fn()
const onKeyUp = jest.fn()

render(<div data-testid="element" onKeyDown={onKeyDown} onKeyUp={onKeyUp} />)

const div = screen.getByTestId('element')

userEvent.arrowPress(div, 'ArrowUp')
expect(onKeyDown).toHaveBeenCalledWith(expect.objectContaining({}))
expect(onKeyUp).toHaveBeenCalledWith(expect.objectContaining({}))
})
114 changes: 85 additions & 29 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,80 @@ function wait(time) {
}

function isMousePressEvent(event) {
return event === 'mousedown' || event === 'mouseup' || event === 'click' || event === 'dblclick';
return (
event === 'mousedown' ||
event === 'mouseup' ||
event === 'click' ||
event === 'dblclick'
)
}

function invert(map) {
const res = {};
const res = {}
for (const key of Object.keys(map)) {
res[map[key]] = key;
res[map[key]] = key
}

return res;
return res
}

const ARROWS_TO_WHICH = {
ArrowDown: 40,
ArrowLeft: 37,
ArrowRight: 39,
ArrowUp: 38,
}

// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
const BUTTONS_TO_NAMES = {
0: 'none',
1: 'primary',
2: 'secondary',
4: 'auxiliary'
};
const NAMES_TO_BUTTONS = invert(BUTTONS_TO_NAMES);
4: 'auxiliary',
}
const NAMES_TO_BUTTONS = invert(BUTTONS_TO_NAMES)

// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
const BUTTON_TO_NAMES = {
0: 'primary',
1: 'auxiliary',
2: 'secondary'
};
2: 'secondary',
}

const NAMES_TO_BUTTON = invert(BUTTON_TO_NAMES);
const NAMES_TO_BUTTON = invert(BUTTON_TO_NAMES)

function convertMouseButtons(event, init, property, mapping) {
if (!isMousePressEvent(event)) {
return 0;
return 0
}

if (init[property] != null) {
return init[property];
return init[property]
}

if (init.buttons != null) {
return mapping[BUTTONS_TO_NAMES[init.buttons]] || 0;
return mapping[BUTTONS_TO_NAMES[init.buttons]] || 0
}

if (init.button != null) {
return mapping[BUTTON_TO_NAMES[init.button]] || 0;
return mapping[BUTTON_TO_NAMES[init.button]] || 0
}

return property != 'button' && isMousePressEvent(event) ? 1 : 0;
return property != 'button' && isMousePressEvent(event) ? 1 : 0
}

function getMouseEventOptions(event, init, clickCount = 0) {
init = init || {};
init = init || {}
return {
...init,
// https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail
detail: event === 'mousedown' || event === 'mouseup' ? 1 + clickCount : clickCount,
detail:
event === 'mousedown' || event === 'mouseup'
? 1 + clickCount
: clickCount,
buttons: convertMouseButtons(event, init, 'buttons', NAMES_TO_BUTTONS),
button: convertMouseButtons(event, init, 'button', NAMES_TO_BUTTON),
};
}
}

function clickLabel(label, init) {
Expand Down Expand Up @@ -93,7 +108,10 @@ function clickBooleanElement(element, init) {
function clickElement(element, previousElement, init) {
fireEvent.mouseOver(element, getMouseEventOptions('mouseover', init))
fireEvent.mouseMove(element, getMouseEventOptions('mousemove', init))
const continueDefaultHandling = fireEvent.mouseDown(element, getMouseEventOptions('mousedown', init))
const continueDefaultHandling = fireEvent.mouseDown(
element,
getMouseEventOptions('mousedown', init),
)
const shouldFocus = element.ownerDocument.activeElement !== element
if (continueDefaultHandling) {
if (previousElement) previousElement.blur()
Expand All @@ -108,7 +126,10 @@ function clickElement(element, previousElement, init) {
function dblClickElement(element, previousElement, init) {
fireEvent.mouseOver(element, getMouseEventOptions('mouseover', init))
fireEvent.mouseMove(element, getMouseEventOptions('mousemove', init))
const continueDefaultHandling = fireEvent.mouseDown(element, getMouseEventOptions('mousedown', init))
const continueDefaultHandling = fireEvent.mouseDown(
element,
getMouseEventOptions('mousedown', init),
)
const shouldFocus = element.ownerDocument.activeElement !== element
if (continueDefaultHandling) {
if (previousElement) previousElement.blur()
Expand Down Expand Up @@ -220,8 +241,14 @@ function getPreviouslyFocusedElement(element) {
function click(element, init) {
const previouslyFocusedElement = getPreviouslyFocusedElement(element)
if (previouslyFocusedElement) {
fireEvent.mouseMove(previouslyFocusedElement, getMouseEventOptions('mousemove', init))
fireEvent.mouseLeave(previouslyFocusedElement, getMouseEventOptions('mouseleave', init))
fireEvent.mouseMove(
previouslyFocusedElement,
getMouseEventOptions('mousemove', init),
)
fireEvent.mouseLeave(
previouslyFocusedElement,
getMouseEventOptions('mouseleave', init),
)
}

switch (element.tagName) {
Expand All @@ -242,8 +269,14 @@ function click(element, init) {
function dblClick(element, init) {
const previouslyFocusedElement = getPreviouslyFocusedElement(element)
if (previouslyFocusedElement) {
fireEvent.mouseMove(previouslyFocusedElement, getMouseEventOptions('mousemove', init))
fireEvent.mouseLeave(previouslyFocusedElement, getMouseEventOptions('mouseleave', init))
fireEvent.mouseMove(
previouslyFocusedElement,
getMouseEventOptions('mousemove', init),
)
fireEvent.mouseLeave(
previouslyFocusedElement,
getMouseEventOptions('mouseleave', init),
)
}

switch (element.tagName) {
Expand All @@ -261,16 +294,22 @@ function dblClick(element, init) {
function selectOptions(element, values, init) {
const previouslyFocusedElement = getPreviouslyFocusedElement(element)
if (previouslyFocusedElement) {
fireEvent.mouseMove(previouslyFocusedElement, getMouseEventOptions('mousemove', init))
fireEvent.mouseLeave(previouslyFocusedElement, getMouseEventOptions('mouseleave', init))
fireEvent.mouseMove(
previouslyFocusedElement,
getMouseEventOptions('mousemove', init),
)
fireEvent.mouseLeave(
previouslyFocusedElement,
getMouseEventOptions('mouseleave', init),
)
}

clickElement(element, previouslyFocusedElement, init)

const valArray = Array.isArray(values) ? values : [values]
const selectedOptions = Array.from(
element.querySelectorAll('option'),
).filter(opt => valArray.includes(opt.value) || valArray.includes(opt))
const selectedOptions = Array.from(element.querySelectorAll('option')).filter(
opt => valArray.includes(opt.value) || valArray.includes(opt),
)

if (selectedOptions.length > 0) {
if (element.multiple) {
Expand Down Expand Up @@ -352,6 +391,22 @@ async function type(element, text, {allAtOnce = false, delay} = {}) {
}
}

function arrowPress(element, key) {
if (element.disabled) return

fireEvent.keyDown(element, {
key,
keyCode: key,
which: ARROWS_TO_WHICH[key],
})

fireEvent.keyUp(element, {
key,
keyCode: key,
which: ARROWS_TO_WHICH[key],
})
}

function upload(element, fileOrFiles, {clickInit, changeInit} = {}) {
if (element.disabled) return
const focusedElement = element.ownerDocument.activeElement
Expand Down Expand Up @@ -419,6 +474,7 @@ function tab({shift = false, focusTrap = document} = {}) {
}

const userEvent = {
arrowPress,
click,
dblClick,
selectOptions,
Expand Down