Skip to content

Commit 5907385

Browse files
committed
fix(userEvent): correct events fired for several things
1 parent df6339a commit 5907385

23 files changed

+615
-247
lines changed

src/user-event/__mocks__/utils.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// this helps us track what the state is before and after an event is fired
22
// this is needed for determining the snapshot values
3-
const {getElementDisplayName} = require('../__tests__/helpers/utils')
43
const actual = jest.requireActual('../utils')
54

65
function getTrackedElementValues(element) {
@@ -18,10 +17,7 @@ function getTrackedElementValues(element) {
1817
function wrapWithTestData(fn) {
1918
return async (element, init) => {
2019
const before = getTrackedElementValues(element)
21-
const testData = {
22-
before,
23-
elementDisplayName: getElementDisplayName(element),
24-
}
20+
const testData = {before}
2521

2622
// put it on the element so the event handler can grab it
2723
element.testData = testData

src/user-event/__tests__/blur.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import {userEvent} from '../../'
2+
import {setup} from './helpers/utils'
3+
4+
test('blur a button', async () => {
5+
const {element, getEventSnapshot, clearEventCalls} = setup(`<button />`)
6+
await userEvent.focus(element)
7+
clearEventCalls()
8+
await userEvent.blur(element)
9+
expect(getEventSnapshot()).toMatchInlineSnapshot(`
10+
Events fired on: button
11+
12+
button - blur
13+
button - focusout
14+
`)
15+
expect(element).not.toHaveFocus()
16+
})
17+
18+
test('no events fired on an unblurable input', async () => {
19+
const {element, getEventSnapshot, clearEventCalls} = setup(`<div />`)
20+
await userEvent.focus(element)
21+
clearEventCalls()
22+
await userEvent.blur(element)
23+
expect(getEventSnapshot()).toMatchInlineSnapshot(
24+
`No events were fired on: div`,
25+
)
26+
expect(element).not.toHaveFocus()
27+
})
28+
29+
test('blur with tabindex', async () => {
30+
const {element, getEventSnapshot, clearEventCalls} = setup(
31+
`<div tabindex="0" />`,
32+
)
33+
await userEvent.focus(element)
34+
clearEventCalls()
35+
await userEvent.blur(element)
36+
expect(getEventSnapshot()).toMatchInlineSnapshot(`
37+
Events fired on: div
38+
39+
div - blur
40+
div - focusout
41+
`)
42+
expect(element).not.toHaveFocus()
43+
})
44+
45+
test('no events fired on a disabled blurable input', async () => {
46+
const {element, getEventSnapshot, clearEventCalls} = setup(
47+
`<button disabled />`,
48+
)
49+
await userEvent.focus(element)
50+
clearEventCalls()
51+
await userEvent.blur(element)
52+
expect(getEventSnapshot()).toMatchInlineSnapshot(
53+
`No events were fired on: button`,
54+
)
55+
expect(element).not.toHaveFocus()
56+
})
57+
58+
test('no events fired if the element is not focused', async () => {
59+
const {element, getEventSnapshot} = setup(`<button />`)
60+
await userEvent.blur(element)
61+
expect(getEventSnapshot()).toMatchInlineSnapshot(
62+
`No events were fired on: button`,
63+
)
64+
expect(element).not.toHaveFocus()
65+
})

src/user-event/__tests__/clear.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test('clears text', async () => {
1111
input[value="hello"] - focus
1212
input[value="hello"] - select
1313
input[value="hello"] - keydown: Delete (46)
14-
input[value="hello"] - input
14+
input[value=""] - input
1515
"{SELECTION}hello{/SELECTION}" -> "{CURSOR}"
1616
input[value=""] - keyup: Delete (46)
1717
`)

0 commit comments

Comments
 (0)