-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: rewrite
userEvent.clear
API (#779)
BREAKING CHANGE: An error is thrown when calling `userEvent.clear` on an element which is not editable. BREAKING CHANGE: An error is thrown when event handlers prevent `userEvent.clear` from focussing/selecting content.
- Loading branch information
1 parent
8de88f4
commit b542540
Showing
11 changed files
with
279 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,32 @@ | ||
import {isDisabled, isElementType} from './utils' | ||
import {prepareDocument} from './document' | ||
import type {UserEvent} from './setup' | ||
import { | ||
focus, | ||
isAllSelected, | ||
isDisabled, | ||
isEditable, | ||
prepareInput, | ||
selectAll, | ||
} from './utils' | ||
|
||
export function clear(this: UserEvent, element: Element) { | ||
if (!isElementType(element, ['input', 'textarea'])) { | ||
// TODO: support contenteditable | ||
throw new Error( | ||
'clear currently only supports input and textarea elements.', | ||
) | ||
if (!isEditable(element) || isDisabled(element)) { | ||
throw new Error('clear()` is only supported on editable elements.') | ||
} | ||
|
||
if (isDisabled(element)) { | ||
return | ||
} | ||
|
||
// TODO: track the selection range ourselves so we don't have to do this input "type" trickery | ||
// just like cypress does: https://github.com/cypress-io/cypress/blob/8d7f1a0bedc3c45a2ebf1ff50324b34129fdc683/packages/driver/src/dom/selection.ts#L16-L37 | ||
prepareDocument(element.ownerDocument) | ||
|
||
const elementType = element.type | ||
focus(element) | ||
|
||
if (elementType !== 'textarea') { | ||
// setSelectionRange is not supported on certain types of inputs, e.g. "number" or "email" | ||
;(element as HTMLInputElement).type = 'text' | ||
if (element.ownerDocument.activeElement !== element) { | ||
throw new Error('The element to be cleared could not be focused.') | ||
} | ||
|
||
this.type(element, '{selectall}{del}', { | ||
delay: 0, | ||
initialSelectionStart: | ||
element.selectionStart ?? /* istanbul ignore next */ undefined, | ||
initialSelectionEnd: | ||
element.selectionEnd ?? /* istanbul ignore next */ undefined, | ||
}) | ||
selectAll(element) | ||
|
||
if (elementType !== 'textarea') { | ||
;(element as HTMLInputElement).type = elementType | ||
if (!isAllSelected(element)) { | ||
throw new Error('The element content to be cleared could not be selected.') | ||
} | ||
|
||
prepareInput('', element, 'deleteContentBackward')?.commit() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,81 @@ | ||
import {UISelectionRange} from '../../document' | ||
import { | ||
calculateNewValue, | ||
EditableInputType, | ||
fireInputEvent, | ||
getInputRange, | ||
} from '../../utils' | ||
import {fireEvent} from '@testing-library/dom' | ||
import {calculateNewValue, editInputElement, getInputRange} from '../../utils' | ||
|
||
export function prepareInput( | ||
data: string, | ||
element: Element, | ||
inputType: string = 'insertText', | ||
): | ||
| { | ||
newValue: string | ||
commit: () => void | ||
} | ||
| undefined { | ||
) { | ||
const inputRange = getInputRange(element) | ||
|
||
// TODO: implement for ranges on multiple nodes | ||
/* istanbul ignore if */ | ||
if ( | ||
!inputRange || | ||
('startContainer' in inputRange && | ||
inputRange.startContainer !== inputRange.endContainer) | ||
) { | ||
if (!inputRange) { | ||
return | ||
} | ||
const node = getNode(element, inputRange) | ||
|
||
const {newValue, newOffset, oldValue} = calculateNewValue( | ||
data, | ||
node, | ||
inputRange, | ||
inputType, | ||
) | ||
if ('startContainer' in inputRange) { | ||
return { | ||
commit: () => { | ||
const del = !inputRange.collapsed | ||
|
||
if ( | ||
newValue === oldValue && | ||
newOffset === inputRange.startOffset && | ||
newOffset === inputRange.endOffset | ||
) { | ||
return | ||
} | ||
if (del) { | ||
inputRange.deleteContents() | ||
} | ||
if (data) { | ||
if (inputRange.endContainer.nodeType === 3) { | ||
const offset = inputRange.endOffset | ||
;(inputRange.endContainer as Text).insertData(offset, data) | ||
inputRange.setStart(inputRange.endContainer, offset + data.length) | ||
inputRange.setEnd(inputRange.endContainer, offset + data.length) | ||
} else { | ||
const text = element.ownerDocument.createTextNode(data) | ||
inputRange.insertNode(text) | ||
inputRange.setStart(text, data.length) | ||
inputRange.setEnd(text, data.length) | ||
} | ||
} | ||
|
||
return { | ||
newValue, | ||
commit: () => | ||
fireInputEvent(element as HTMLElement, { | ||
newValue, | ||
newSelection: { | ||
node, | ||
offset: newOffset, | ||
}, | ||
eventOverrides: { | ||
if (del || data) { | ||
fireEvent.input(element, {inputType}) | ||
} | ||
}, | ||
} | ||
} else { | ||
return { | ||
getNewValue: () => | ||
calculateNewValue( | ||
data, | ||
element as HTMLTextAreaElement, | ||
inputRange, | ||
inputType, | ||
}, | ||
}), | ||
} | ||
} | ||
).newValue, | ||
commit: () => { | ||
const {newValue, newOffset, oldValue} = calculateNewValue( | ||
data, | ||
element as HTMLTextAreaElement, | ||
inputRange, | ||
inputType, | ||
) | ||
|
||
function getNode(element: Element, inputRange: Range | UISelectionRange) { | ||
if ('startContainer' in inputRange) { | ||
if (inputRange.startContainer.nodeType === 3) { | ||
return inputRange.startContainer as Text | ||
} | ||
if ( | ||
newValue === oldValue && | ||
newOffset === inputRange.startOffset && | ||
newOffset === inputRange.endOffset | ||
) { | ||
return | ||
} | ||
|
||
try { | ||
return inputRange.startContainer.insertBefore( | ||
element.ownerDocument.createTextNode(''), | ||
inputRange.startContainer.childNodes.item(inputRange.startOffset), | ||
) | ||
} catch { | ||
/* istanbul ignore next */ | ||
throw new Error( | ||
'Invalid operation. Can not insert text at this position. The behavior is not implemented yet.', | ||
) | ||
editInputElement(element as HTMLTextAreaElement, { | ||
newValue, | ||
newSelection: { | ||
node: element, | ||
offset: newOffset, | ||
}, | ||
eventOverrides: { | ||
inputType, | ||
}, | ||
}) | ||
}, | ||
} | ||
} | ||
|
||
return element as | ||
| HTMLTextAreaElement | ||
| (HTMLInputElement & {type: EditableInputType}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.