Skip to content

Commit 1e926c3

Browse files
committed
feat(userEvent): build-in @testing-library/user-event
1 parent b23a2bc commit 1e926c3

File tree

16 files changed

+3486
-0
lines changed

16 files changed

+3486
-0
lines changed

src/user-event/__tests__/clear.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import userEvent from '..'
2+
import {setup} from './helpers/utils'
3+
4+
test('clears text', () => {
5+
const {element, getEventCalls} = setup('<input value="hello" />')
6+
userEvent.clear(element)
7+
expect(element).toHaveValue('')
8+
expect(getEventCalls()).toMatchInlineSnapshot(`
9+
Events fired on: input[value=""]
10+
11+
mouseover: Left (0)
12+
mousemove: Left (0)
13+
mousedown: Left (0)
14+
focus
15+
mouseup: Left (0)
16+
click: Left (0)
17+
mousedown: Left (0)
18+
mouseup: Left (0)
19+
click: Left (0)
20+
dblclick: Left (0)
21+
keydown: Backspace (8)
22+
keyup: Backspace (8)
23+
input: "{SELECTION}hello{/SELECTION}" -> "hello"
24+
change
25+
`)
26+
})
27+
28+
test('does not clear text on disabled inputs', () => {
29+
const {element, getEventCalls} = setup('<input value="hello" disabled />')
30+
userEvent.clear(element)
31+
expect(element).toHaveValue('hello')
32+
expect(getEventCalls()).toMatchInlineSnapshot(
33+
`No events were fired on: input[value="hello"]`,
34+
)
35+
})
36+
37+
test('does not clear text on readonly inputs', () => {
38+
const {element, getEventCalls} = setup('<input value="hello" readonly />')
39+
userEvent.clear(element)
40+
expect(element).toHaveValue('hello')
41+
expect(getEventCalls()).toMatchInlineSnapshot(`
42+
Events fired on: input[value="hello"]
43+
44+
mouseover: Left (0)
45+
mousemove: Left (0)
46+
mousedown: Left (0)
47+
focus
48+
mouseup: Left (0)
49+
click: Left (0)
50+
mousedown: Left (0)
51+
mouseup: Left (0)
52+
click: Left (0)
53+
dblclick: Left (0)
54+
keydown: Backspace (8)
55+
keyup: Backspace (8)
56+
`)
57+
})
58+
59+
test('clears even on inputs that cannot (programmatically) have a selection', () => {
60+
const {element: email} = setup('<input value="[email protected]" type="email" />')
61+
userEvent.clear(email)
62+
expect(email).toHaveValue('')
63+
64+
const {element: password} = setup('<input value="pswrd" type="password" />')
65+
userEvent.clear(password)
66+
expect(password).toHaveValue('')
67+
68+
const {element: number} = setup('<input value="12" type="number" />')
69+
userEvent.clear(number)
70+
// jest-dom does funny stuff with toHaveValue on number inputs
71+
expect(number.value).toBe('')
72+
})

0 commit comments

Comments
 (0)