Skip to content

Commit 5c9257b

Browse files
authored
fix(type): fix setSelectionRange (testing-library#373)
Closes testing-library#369 Closes testing-library#346
1 parent 44b9011 commit 5c9257b

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/type.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,19 @@ async function typeImpl(
6161
// The reason we have to do this at all is because it actually *is*
6262
// programmatically changed by fireEvent.input, so we have to simulate the
6363
// browser's default behavior
64-
if (currentValue() === newValue) {
64+
const value = currentValue()
65+
66+
if (value === newValue) {
6567
setSelectionRangeIfNecessary(
6668
currentElement(),
6769
newSelectionStart,
6870
newSelectionStart,
6971
)
72+
} else {
73+
// If the currentValue is different than the expected newValue and we *can*
74+
// change the selection range, than we should set it to the length of the
75+
// currentValue to ensure that the browser behavior is mimicked.
76+
setSelectionRangeIfNecessary(currentElement(), value.length, value.length)
7077
}
7178
}
7279

0 commit comments

Comments
 (0)