/
paste.js
105 lines (85 loc) 路 3.04 KB
/
paste.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
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
import userEvent from '../'
import {setup} from './helpers/utils'
test('should paste text in input', () => {
const {element, getEventSnapshot} = setup('<input />')
const text = 'Hello, world!'
userEvent.paste(element, text)
expect(element).toHaveValue(text)
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: input[value="Hello, world!"]
input[value=""] - focus
input[value=""] - paste
input[value="Hello, world!"] - input
"{CURSOR}" -> "Hello, world!{CURSOR}"
input[value="Hello, world!"] - select
`)
})
test('should paste text in textarea', () => {
const {element, getEventSnapshot} = setup('<textarea />')
const text = 'Hello, world!'
userEvent.paste(element, text)
expect(element).toHaveValue(text)
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: textarea[value="Hello, world!"]
textarea[value=""] - focus
textarea[value=""] - paste
textarea[value="Hello, world!"] - input
"{CURSOR}" -> "Hello, world!{CURSOR}"
textarea[value="Hello, world!"] - select
`)
})
test('does not paste when readOnly', () => {
const {element, getEventSnapshot} = setup('<input readonly />')
userEvent.paste(element, 'hi')
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: input[value=""]
input[value=""] - focus
input[value=""] - paste
`)
})
test('does not paste when disabled', () => {
const {element, getEventSnapshot} = setup('<input disabled />')
userEvent.paste(element, 'hi')
expect(getEventSnapshot()).toMatchInlineSnapshot(
`No events were fired on: input[value=""]`,
)
})
test.each(['input', 'textarea'])(
'should paste text in <%s> up to maxLength if provided',
type => {
const {element} = setup(`<${type} maxlength="10" />`)
userEvent.type(element, 'superlongtext')
expect(element).toHaveValue('superlongt')
element.value = ''
userEvent.paste(element, 'superlongtext')
expect(element).toHaveValue('superlongt')
},
)
test.each(['input', 'textarea'])(
'should append text in <%s> up to maxLength if provided',
type => {
const {element} = setup(`<${type} maxlength="10" />`)
userEvent.type(element, 'superlong')
userEvent.type(element, 'text')
expect(element).toHaveValue('superlongt')
element.value = ''
userEvent.paste(element, 'superlongtext')
expect(element).toHaveValue('superlongt')
},
)
test('should replace selected text all at once', () => {
const {element} = setup('<input value="hello world" />')
const selectionStart = 'hello world'.search('world')
const selectionEnd = selectionStart + 'world'.length
element.setSelectionRange(selectionStart, selectionEnd)
userEvent.paste(element, 'friend')
expect(element).toHaveValue('hello friend')
})
test('should give error if we are trying to call paste on an invalid element', () => {
const {element} = setup('<div />')
expect(() =>
userEvent.paste(element, "I'm only a div :("),
).toThrowErrorMatchingInlineSnapshot(
`"the current element is of type DIV and doesn't have a valid value"`,
)
})