Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/document/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function prepareInterceptor<
*/
applyNative?: boolean
realArgs?: ImplReturn<ElementType[PropName]>
then?: () => void
},
) {
const prototypeDescriptor = Object.getOwnPropertyDescriptor(
Expand All @@ -49,7 +50,11 @@ export function prepareInterceptor<
this: ElementType,
...args: Params<ElementType[PropName]>
) {
const {applyNative = true, realArgs} = interceptorImpl.call(this, ...args)
const {
applyNative = true,
realArgs,
then,
} = interceptorImpl.call(this, ...args)

const realFunc = ((!applyNative && objectDescriptor) ||
(prototypeDescriptor as PropertyDescriptor))[target] as (
Expand All @@ -62,6 +67,8 @@ export function prepareInterceptor<
} else {
realFunc.call(this, ...realArgs)
}

then?.()
}
;(intercept as Interceptable)[Interceptor] = Interceptor

Expand Down
3 changes: 1 addition & 2 deletions src/document/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ function valueInterceptor(
if (isUI) {
this[UIValue] = String(v)
setPreviousValue(this, String(this.value))
} else {
trackOrSetValue(this, String(v))
}

return {
applyNative: !!isUI,
realArgs: sanitizeValue(this, v),
then: isUI ? undefined : () => trackOrSetValue(this, String(v)),
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/document/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ test('maintain selection range on elements without support for selection range',
expect(element.selectionStart).toBe(null)
})

test('reset UI selection if value is programmatically set', async () => {
const {element} = render<HTMLInputElement>(`<input/>`)

prepare(element)

setUIValue(element, 'abc')
setUISelection(element, {anchorOffset: 1, focusOffset: 2})

element.value = 'abcdef'
expect(element.selectionStart).toBe(6)
expect(getUISelection(element)).toHaveProperty('focusOffset', 6)
expect(getUISelection(element)).toHaveProperty('startOffset', 6)
})

test('clear UI selection if selection is programmatically set', async () => {
const {element} = render<HTMLInputElement>(`<input/>`)

Expand Down