Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(document): use setters/methods on element as default #987

Merged
merged 1 commit into from Jul 14, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/document/interceptor.ts
Expand Up @@ -51,7 +51,7 @@ export function prepareInterceptor<
...args: Params<ElementType[PropName]>
) {
const {
applyNative = true,
applyNative = false,
realArgs,
then,
} = interceptorImpl.call(this, ...args)
Expand Down
8 changes: 4 additions & 4 deletions src/document/selection.ts
Expand Up @@ -32,8 +32,7 @@ export function prepareSelectionInterceptor(
function interceptorImpl(
this: HTMLInputElement | HTMLTextAreaElement,
start: number | Value | null,
end: number | null,
direction: 'forward' | 'backward' | 'none' = 'none',
...others
) {
const isUI = start && typeof start === 'object' && start[UISelection]

Expand All @@ -42,10 +41,11 @@ export function prepareSelectionInterceptor(
}

return {
realArgs: [Number(start), end, direction] as [
applyNative: !!isUI,
realArgs: [Number(start), ...others] as [
number,
number,
typeof direction,
'forward' | 'backward' | 'none' | undefined,
],
}
},
Expand Down
42 changes: 42 additions & 0 deletions tests/document/index.ts
Expand Up @@ -202,3 +202,45 @@ test('track changes to value and selection per setRangeText', () => {
expect(getUIValue(element)).toBe('aYcd')
expect(getUISelection(element)).toHaveProperty('focusOffset', 1)
})

test('circumvent setters/methods for UI changes', () => {
const {element} = render<HTMLInputElement>(`<input/>`, {focus: false})

const prototypeDescr = Object.getOwnPropertyDescriptors<HTMLInputElement>(
Object.getPrototypeOf(element) as HTMLInputElement,
)
const valueSpy = jest.fn(prototypeDescr.value.set)
const setSelectionRangeSpy = jest.fn(prototypeDescr.setSelectionRange.value)

Object.defineProperties(element, {
value: {
get: () => {
throw new Error()
},
...prototypeDescr.value,
set: valueSpy,
},
setSelectionRange: {
...prototypeDescr.setSelectionRange,
value: setSelectionRangeSpy,
},
})

prepare(element)
element.focus()

setUIValue(element, 'abcd')
setUISelection(element, {focusOffset: 3})

expect(element).toHaveValue('abcd')
expect(element).toHaveProperty('selectionStart', 3)
expect(valueSpy).not.toBeCalled()
expect(setSelectionRangeSpy).not.toBeCalled()

element.value = 'efgh'
element.setSelectionRange(1, 2)
expect(element).toHaveValue('efgh')
expect(element).toHaveProperty('selectionStart', 1)
expect(valueSpy).toBeCalledWith('efgh')
expect(setSelectionRangeSpy).toBeCalledWith(1, 2)
})