-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathpaste.ts
163 lines (129 loc) Β· 4.31 KB
/
paste.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import userEvent from '#src'
import {CustomElements, render, setup} from '#testHelpers'
import {createDataTransfer} from '#src/utils'
test('paste with empty clipboard', async () => {
const {element, getEvents, user} = setup(`<input/>`)
await element.ownerDocument.defaultView?.navigator.clipboard.write([])
await user.paste()
expect(getEvents('paste')).toHaveLength(1)
expect(getEvents('input')).toHaveLength(0)
})
test('do not trigger input for paste with file data', async () => {
const {getEvents, user} = setup(`<input/>`)
const f0 = new File(['bar'], 'bar0.txt', {type: 'text/plain'})
const dt = createDataTransfer(window, [f0])
await user.paste(dt)
expect(getEvents('paste')).toHaveLength(1)
expect(getEvents('input')).toHaveLength(0)
})
test.each([
[
`<input/>`,
`
Events fired on: input[value="Hello, world!"]
input[value=""] - paste
input[value=""] - beforeinput
input[value="Hello, world!"] - input
`,
],
[
`<textarea/>`,
`
Events fired on: textarea[value="Hello, world!"]
textarea[value=""] - paste
textarea[value=""] - beforeinput
textarea[value="Hello, world!"] - input
`,
],
])('should paste text in %s', async (html, events) => {
const {element, getEventSnapshot, user} = setup(html)
const text = 'Hello, world!'
await element.ownerDocument.defaultView?.navigator.clipboard.writeText(text)
await user.paste()
expect(element).toHaveValue(text)
expect(element).toHaveProperty('selectionStart', 13)
expect(getEventSnapshot()).toMatchInlineSnapshot(events)
})
test('does not paste when readOnly', async () => {
const {getEventSnapshot, user} = setup('<input readonly />')
await user.paste('hi')
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: input[value=""]
input[value=""] - paste
`)
})
test('does not paste when disabled', async () => {
const {getEventSnapshot, user} = setup('<input disabled />')
await user.paste('hi')
expect(getEventSnapshot()).toMatchInlineSnapshot(
`No events were fired on: input[value=""]`,
)
})
test('prevent input per paste event handler', async () => {
const {element, eventWasFired, user} = setup(`<input />`)
element.addEventListener('paste', e => e.preventDefault())
await user.paste('hi')
expect(eventWasFired('paste')).toBe(true)
expect(eventWasFired('input')).toBe(false)
})
test.each(['input', 'textarea'])(
'should paste text in <%s> up to maxLength if provided',
async type => {
const {element, user} = setup<HTMLInputElement | HTMLTextAreaElement>(
`<${type} maxlength="10" />`,
)
await user.type(element, 'superlongtext')
expect(element).toHaveValue('superlongt')
element.value = ''
await user.paste('superlongtext')
expect(element).toHaveValue('superlongt')
},
)
test.each(['input', 'textarea'])(
'should append text in <%s> up to maxLength if provided',
async type => {
const {element, user} = setup<HTMLInputElement | HTMLTextAreaElement>(
`<${type} maxlength="10" />`,
)
await user.type(element, 'superlong')
await user.type(element, 'text')
expect(element).toHaveValue('superlongt')
element.value = ''
await user.paste('superlongtext')
expect(element).toHaveValue('superlongt')
},
)
test('should replace selected text all at once', async () => {
const {element, user} = setup<HTMLInputElement>(
'<input value="hello world" />',
{
selection: {anchorOffset: 6, focusOffset: 11},
},
)
await user.paste('friend')
expect(element).toHaveValue('hello friend')
})
describe('without Clipboard API', () => {
beforeEach(() => {
Object.defineProperty(window.navigator, 'clipboard', {
value: undefined,
configurable: true,
})
})
test('reject if trying to use missing API', async () => {
const {getEvents} = render(`<input/>`)
await expect(userEvent.paste()).rejects.toThrowErrorMatchingInlineSnapshot(
`\`userEvent.paste()\` without \`clipboardData\` requires the \`ClipboardAPI\` to be available.`,
)
expect(getEvents()).toHaveLength(0)
})
})
describe('on shadow DOM', () => {
test('paste into an input element', async () => {
const {element, user} = setup<CustomElements['shadow-input']>(
'<shadow-input></shadow-input>',
)
await user.paste('test')
expect(element.value).toEqual('test')
})
})