-
Notifications
You must be signed in to change notification settings - Fork 455
/
clear.js
73 lines (63 loc) 路 2.45 KB
/
clear.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import * as userEvent from '..'
import {setup} from './helpers/utils'
test('clears text', async () => {
const {element, getEventCalls} = setup('<input value="hello" />')
await userEvent.clear(element)
expect(element).toHaveValue('')
expect(getEventCalls()).toMatchInlineSnapshot(`
Events fired on: input[value=""]
input[value="hello"] - focus
input[value="hello"] - select
input[value="hello"] - select
input[value="hello"] - keydown: Delete (46)
input[value="hello"] - input
"{SELECTION}hello{/SELECTION}" -> "{CURSOR}"
input[value=""] - select
input[value=""] - keyup: Delete (46)
`)
})
test('works with textarea', async () => {
const {element} = setup('<textarea>hello</textarea>')
await userEvent.clear(element)
expect(element).toHaveValue('')
})
test('does not clear text on disabled inputs', async () => {
const {element, getEventCalls} = setup('<input value="hello" disabled />')
await userEvent.clear(element)
expect(element).toHaveValue('hello')
expect(getEventCalls()).toMatchInlineSnapshot(
`No events were fired on: input[value="hello"]`,
)
})
test('does not clear text on readonly inputs', async () => {
const {element, getEventCalls} = setup('<input value="hello" readonly />')
await userEvent.clear(element)
expect(element).toHaveValue('hello')
expect(getEventCalls()).toMatchInlineSnapshot(`
Events fired on: input[value="hello"]
input[value="hello"] - focus
input[value="hello"] - select
input[value="hello"] - select
input[value="hello"] - keydown: Delete (46)
input[value="hello"] - keyup: Delete (46)
`)
})
test('clears even on inputs that cannot (programmatically) have a selection', async () => {
const {element: email} = setup('<input value="a@b.c" type="email" />')
await userEvent.clear(email)
expect(email).toHaveValue('')
const {element: password} = setup('<input value="pswrd" type="password" />')
await userEvent.clear(password)
expect(password).toHaveValue('')
const {element: number} = setup('<input value="12" type="number" />')
await userEvent.clear(number)
// jest-dom does funny stuff with toHaveValue on number inputs
expect(number.value).toBe('')
})
test('non-inputs/textareas are currently unsupported', async () => {
const {element} = setup('<div />')
const error = await userEvent.clear(element).catch(e => e)
expect(error).toMatchInlineSnapshot(
`[Error: clear currently only supports input and textarea elements.]`,
)
})