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
15 changes: 9 additions & 6 deletions src/document/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ export function prepareInterceptor<
then,
} = interceptorImpl.call(this, ...args)

const realFunc = ((!applyNative && objectDescriptor) ||
(prototypeDescriptor as PropertyDescriptor))[target] as (
const sourceDescriptor =
Comment thread
snowystinger marked this conversation as resolved.
!applyNative && objectDescriptor ? objectDescriptor : prototypeDescriptor
const realFunc = sourceDescriptor?.[target] as (
this: ElementType,
...a: unknown[]
) => unknown
Expand Down Expand Up @@ -102,18 +103,20 @@ export function prepareValueInterceptor(
'value',
function interceptorImpl(
this: HTMLInputElement | HTMLTextAreaElement,
v: UIValueString | string,
v: UIValueString | string | null,
) {
const isUI = isUIValue(v)
// `value` is a [LegacyNullToEmptyString] DOMString.
const value = v === null ? '' : v
const isUI = isUIValue(value)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value can never be null, so why did we add it to the signature for isUIValue?


if (isUI) {
startTrackValue(this)
}

return {
applyNative: !!isUI,
realArgs: sanitizeValue(this, v),
then: isUI ? undefined : () => trackOrSetValue(this, String(v)),
realArgs: sanitizeValue(this, value),
then: isUI ? undefined : () => trackOrSetValue(this, String(value)),
}
},
)
Expand Down
24 changes: 24 additions & 0 deletions tests/document/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,30 @@ test('reset UI selection if value is programmatically set', async () => {
expect(getUISelection(element)).toHaveProperty('startOffset', 6)
})

test('programmatic `null` value is treated as empty string', async () => {
const {element} = render<HTMLInputElement>(`<input value="abc"/>`)

prepare(element)

// HTMLInputElement.value is a [LegacyNullToEmptyString] DOMString.
// Callers still assign `null` at runtime; the IDL coerces it to `""`.
element.value = null as unknown as string

expect(element).toHaveValue('')
expect(getUIValue(element)).toBe('')
})

test('programmatic `null` value on number input is treated as empty', async () => {
const {element} = render<HTMLInputElement>(`<input type="number" value="1"/>`)

prepare(element)

element.value = null as unknown as string

expect(element).toHaveValue(null)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright, this appears to be null due to https://github.com/testing-library/jest-dom/blob/3782c78b3dc9824675afe0cb8f1722f8c96f494d/src/utils.js#L191

which is why the line below is empty string

expect(getUIValue(element)).toBe('')
})

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

Expand Down
Loading