Repository navigation
fix: treat programmatic null input values as empty string #1329
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,8 +72,9 @@ export function prepareInterceptor< | |
| then, | ||
| } = interceptorImpl.call(this, ...args) | ||
|
|
||
| const realFunc = ((!applyNative && objectDescriptor) || | ||
| (prototypeDescriptor as PropertyDescriptor))[target] as ( | ||
| const sourceDescriptor = | ||
| !applyNative && objectDescriptor ? objectDescriptor : prototypeDescriptor | ||
| const realFunc = sourceDescriptor?.[target] as ( | ||
| this: ElementType, | ||
| ...a: unknown[] | ||
| ) => unknown | ||
|
|
@@ -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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| 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)), | ||
| } | ||
| }, | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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/>`) | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.