-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathindex.ts
137 lines (111 loc) Β· 3.78 KB
/
index.ts
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import userEvent from '../../index'
import {addListeners, setup} from '../helpers/utils'
it('type without focus', () => {
const {element} = setup('<input/>')
const {getEventSnapshot} = addListeners(document.body)
userEvent.keyboard('foo')
expect(element).toHaveValue('')
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: body
body - keydown: f (102)
body - keypress: f (102)
body - keyup: f (102)
body - keydown: o (111)
body - keypress: o (111)
body - keyup: o (111)
body - keydown: o (111)
body - keypress: o (111)
body - keyup: o (111)
`)
})
it('type with focus', () => {
const {element} = setup('<input/>')
const {getEventSnapshot} = addListeners(document.body)
;(element as HTMLInputElement).focus()
userEvent.keyboard('foo')
expect(element).toHaveValue('foo')
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: body
input[value=""] - focusin
input[value=""] - keydown: f (102)
input[value=""] - keypress: f (102)
input[value="f"] - input
input[value="f"] - keyup: f (102)
input[value="f"] - keydown: o (111)
input[value="f"] - keypress: o (111)
input[value="fo"] - input
input[value="fo"] - keyup: o (111)
input[value="fo"] - keydown: o (111)
input[value="fo"] - keypress: o (111)
input[value="foo"] - input
input[value="foo"] - keyup: o (111)
`)
})
it('type asynchronous', async () => {
const {element} = setup('<input/>')
const {getEventSnapshot} = addListeners(document.body)
;(element as HTMLInputElement).focus()
// eslint-disable-next-line testing-library/no-await-sync-events
await userEvent.keyboard('foo', {delay: 1})
expect(element).toHaveValue('foo')
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: body
input[value=""] - focusin
input[value=""] - keydown: f (102)
input[value=""] - keypress: f (102)
input[value="f"] - input
input[value="f"] - keyup: f (102)
input[value="f"] - keydown: o (111)
input[value="f"] - keypress: o (111)
input[value="fo"] - input
input[value="fo"] - keyup: o (111)
input[value="fo"] - keydown: o (111)
input[value="fo"] - keypress: o (111)
input[value="foo"] - input
input[value="foo"] - keyup: o (111)
`)
})
describe('error', () => {
afterEach(() => {
;(console.error as jest.MockedFunction<typeof console.error>).mockClear()
})
it('error in sync', async () => {
const err = jest.spyOn(console, 'error')
err.mockImplementation(() => {})
userEvent.keyboard('{!')
// the catch will be asynchronous
await Promise.resolve()
expect(err).toHaveBeenCalledWith(expect.any(Error))
expect(err.mock.calls[0][0]).toHaveProperty(
'message',
'Expected key descriptor but found "!" in "{!"',
)
})
it('error in async', async () => {
const promise = userEvent.keyboard('{!', {delay: 1})
return expect(promise).rejects.toThrowError(
'Expected key descriptor but found "!" in "{!"',
)
})
})
it('continue typing with state', () => {
const {element, getEventSnapshot, clearEventCalls} = setup('<input/>')
;(element as HTMLInputElement).focus()
clearEventCalls()
const state = userEvent.keyboard('[ShiftRight>]')
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: input[value=""]
input[value=""] - keydown: Shift (16) {shift}
`)
clearEventCalls()
userEvent.keyboard('F[/ShiftRight]', {keyboardState: state})
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: input[value="F"]
input[value=""] - keydown: F (70) {shift}
input[value=""] - keypress: F (70) {shift}
input[value="F"] - input
"{CURSOR}" -> "F{CURSOR}"
input[value="F"] - keyup: F (70) {shift}
input[value="F"] - keyup: Shift (16)
`)
})