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

Support arrowup and arrowdown events #511

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,13 @@ The following special character strings are supported:
| `{selectall}` | N/A | N/A | Selects all the text of the element. Note that this will only work for elements that support selection ranges (so, not `email`, `password`, `number`, among others) |
| `{arrowleft}` | ArrowLeft | N/A | |
| `{arrowright}` | ArrowRight | N/A | |
| `{arrowup}` | ArrowUp | N/A | |
| `{arrowdown}` | ArrowDown | N/A | |
| `{shift}` | Shift | `shiftKey` | Does **not** capitalize following characters. |
| `{ctrl}` | Control | `ctrlKey` | |
| `{alt}` | Alt | `altKey` | |
| `{meta}` | OS | `metaKey` | |
| `{capslock}` | CapsLock | `modifierCapsLock` | Fires both keydown and keyup when used (simulates a user clicking their "Caps Lock" button to enable caps lock). |
| `{capslock}` | CapsLock | `modifierCapsLock` | Fires both keydown and keyup when used (simulates a user clicking their "Caps Lock" button to enable caps lock). |

> **A note about modifiers:** Modifier keys (`{shift}`, `{ctrl}`, `{alt}`,
> `{meta}`) will activate their corresponding event modifiers for the duration
Expand Down Expand Up @@ -610,6 +612,7 @@ Thanks goes to these people ([emoji key][emojis]):

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification.
Expand Down
56 changes: 56 additions & 0 deletions src/__tests__/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -1066,3 +1066,59 @@ test('navigation key: {arrowleft} and {arrowright} moves the cursor', () => {
input[value="abc"] - keyup: c (99)
`)
})

test('{arrowdown} fires keyup/keydown events', () => {
const {element} = setup('<input />')
const handleKeyDown = jest.fn()
const handleKeyUp = jest.fn()

addListeners(element, {
eventHandlers: {
keyDown: handleKeyDown,
keyUp: handleKeyUp,
},
})
corymharper marked this conversation as resolved.
Show resolved Hide resolved

userEvent.type(element, '{arrowdown}')

expect(handleKeyDown).toHaveBeenCalledWith(
expect.objectContaining({
key: 'ArrowDown',
keyCode: 40,
}),
)
expect(handleKeyUp).toHaveBeenCalledWith(
expect.objectContaining({
key: 'ArrowDown',
keyCode: 40,
}),
)
})

test('{arrowup} fires keyup/keydown events', () => {
const {element} = setup('<input />')
const handleKeyDown = jest.fn()
const handleKeyUp = jest.fn()

addListeners(element, {
eventHandlers: {
keyDown: handleKeyDown,
keyUp: handleKeyUp,
},
})

userEvent.type(element, '{arrowup}')

expect(handleKeyDown).toHaveBeenCalledWith(
expect.objectContaining({
key: 'ArrowUp',
keyCode: 38,
}),
)
expect(handleKeyUp).toHaveBeenCalledWith(
expect.objectContaining({
key: 'ArrowUp',
keyCode: 38,
}),
)
})
40 changes: 40 additions & 0 deletions src/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ const modifierCallbackMap = {
const specialCharCallbackMap = {
'{arrowleft}': navigationKey('ArrowLeft'),
'{arrowright}': navigationKey('ArrowRight'),
'{arrowdown}': handleArrowDown,
'{arrowup}': handleArrowUp,
'{enter}': handleEnter,
'{esc}': handleEsc,
'{del}': handleDel,
Expand Down Expand Up @@ -711,4 +713,42 @@ function handleSpaceOnClickable({currentElement, eventOverrides}) {
}
}

function handleArrowDown({currentElement, eventOverrides}) {
const key = 'ArrowDown'
const keyCode = 40

fireEvent.keyDown(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})

fireEvent.keyUp(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
}

function handleArrowUp({currentElement, eventOverrides}) {
const key = 'ArrowUp'
const keyCode = 38

fireEvent.keyDown(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})

fireEvent.keyUp(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
}

export {type}