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

test: add tests for functional keys #590

Merged
merged 3 commits into from
Mar 17, 2021
Merged
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
5 changes: 4 additions & 1 deletion src/__tests__/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ function addListeners(element, {eventHandlers = {}} = {}) {
generalListener.mockClear()
eventHandlerCalls.current = []
}
const getEvents = () => generalListener.mock.calls.map(([e]) => e)
const getEvents = type =>
generalListener.mock.calls
.map(([e]) => e)
.filter(e => !type || e.type === type)
const eventWasFired = eventType => getEvents().some(e => e.type === eventType)

function getClickEventsSnapshot() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,88 @@ test('backspace to valid value', () => {
input[value="5"] - keyup: Backspace (8)
`)
})

test('trigger click event on [Enter] keydown on HTMLAnchorElement', () => {
const {element, getEventSnapshot, getEvents} = setup(
`<a href="example.com" target="_blank"/>`,
)
;(element as HTMLAnchorElement).focus()

userEvent.keyboard('[Enter]')

expect(getEvents('click')).toHaveLength(1)
expect(getEvents('click')[0]).toHaveProperty('detail', 0)

// this snapshot should probably not contain a keypress event
// see https://github.com/testing-library/user-event/issues/589
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: a

a - focus
a - focusin
a - keydown: Enter (13)
a - keypress: Enter (13)
a - click: Left (0)
a - keyup: Enter (13)
`)
})

test('trigger click event on [Enter] keypress on HTMLButtonElement', () => {
const {element, getEventSnapshot, getEvents} = setup(`<button/>`)
;(element as HTMLButtonElement).focus()

userEvent.keyboard('[Enter]')

expect(getEvents('click')).toHaveLength(1)
expect(getEvents('click')[0]).toHaveProperty('detail', 0)
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: button

button - focus
button - focusin
button - keydown: Enter (13)
button - keypress: Enter (13)
button - click: Left (0)
button - keyup: Enter (13)
`)
})

test('trigger click event on [Space] keyup on HTMLButtonElement', () => {
const {element, getEventSnapshot, getEvents} = setup(`<button/>`)
;(element as HTMLButtonElement).focus()

userEvent.keyboard('[Space]')

expect(getEvents('click')).toHaveLength(1)
expect(getEvents('click')[0]).toHaveProperty('detail', 0)
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: button

button - focus
button - focusin
button - keydown: (32)
button - keypress: (32)
button - keyup: (32)
button - click: Left (0)
`)
})

test('trigger click event on [Space] keyup on HTMLInputElement type=button', () => {
const {element, getEventSnapshot, getEvents} = setup(`<button/>`)
;(element as HTMLButtonElement).focus()

userEvent.keyboard('[Space]')

expect(getEvents('click')).toHaveLength(1)
expect(getEvents('click')[0]).toHaveProperty('detail', 0)
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: button

button - focus
button - focusin
button - keydown: (32)
button - keypress: (32)
button - keyup: (32)
button - click: Left (0)
`)
})