Skip to content

Commit 075e682

Browse files
diego-codesKent C. Dodds
authored and
Kent C. Dodds
committed
fix(events): add selection-related properties to target (#264)
1 parent ed0541d commit 075e682

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/__tests__/events.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,19 @@ test('assigns target properties', () => {
160160
expect(node.value).toBe(value)
161161
})
162162

163+
test('assigns selection-related target properties', () => {
164+
const node = document.createElement('input')
165+
const spy = jest.fn()
166+
const value = 'ab'
167+
const selectionStart = 1
168+
const selectionEnd = 2
169+
node.addEventListener('change', spy)
170+
fireEvent.change(node, {target: {value, selectionStart, selectionEnd}})
171+
expect(node.value).toBe(value)
172+
expect(node.selectionStart).toBe(selectionStart)
173+
expect(node.selectionEnd).toBe(selectionEnd)
174+
})
175+
163176
test('assigning a value to a target that cannot have a value throws an error', () => {
164177
const node = document.createElement('div')
165178
expect(() =>

src/events.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ Object.keys(eventMap).forEach(key => {
316316
createEvent[key] = (node, init) => {
317317
const eventInit = {...defaultInit, ...init}
318318
const {target: {value, files, ...targetProperties} = {}} = eventInit
319-
Object.assign(node, targetProperties)
320319
if (value !== undefined) {
321320
setNativeValue(node, value)
322321
}
@@ -331,6 +330,7 @@ Object.keys(eventMap).forEach(key => {
331330
value: files,
332331
})
333332
}
333+
Object.assign(node, targetProperties)
334334
const window = getWindowFromNode(node)
335335
const EventConstructor = window[EventType] || window.Event
336336
return new EventConstructor(eventName, eventInit)

0 commit comments

Comments
 (0)