Skip to content

Commit 1ddadbd

Browse files
authored
fix: leading number being trimmed on point type (testing-library#376)
Closes:testing-library#360
1 parent 50e330b commit 1ddadbd

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

src/__tests__/type.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -641,11 +641,11 @@ test('can type "-" into number inputs', () => {
641641
// https://github.com/testing-library/user-event/issues/336
642642
test('can type "." into number inputs', () => {
643643
const {element, getEventSnapshot} = setup('<input type="number" />')
644-
userEvent.type(element, '0.3')
645-
expect(element).toHaveValue(0.3)
644+
userEvent.type(element, '3.3')
645+
expect(element).toHaveValue(3.3)
646646

647647
expect(getEventSnapshot()).toMatchInlineSnapshot(`
648-
Events fired on: input[value=".3"]
648+
Events fired on: input[value="3.3"]
649649
650650
input[value=""] - pointerover
651651
input[value=""] - pointerenter
@@ -660,21 +660,21 @@ test('can type "." into number inputs', () => {
660660
input[value=""] - pointerup
661661
input[value=""] - mouseup: Left (0)
662662
input[value=""] - click: Left (0)
663-
input[value=""] - keydown: 0 (48)
664-
input[value=""] - keypress: 0 (48)
665-
input[value="0"] - input
666-
"{CURSOR}" -> "{CURSOR}0"
667-
input[value="0"] - keyup: 0 (48)
668-
input[value="0"] - keydown: . (46)
669-
input[value="0"] - keypress: . (46)
663+
input[value=""] - keydown: 3 (51)
664+
input[value=""] - keypress: 3 (51)
665+
input[value="3"] - input
666+
"{CURSOR}" -> "{CURSOR}3"
667+
input[value="3"] - keyup: 3 (51)
668+
input[value="3"] - keydown: . (46)
669+
input[value="3"] - keypress: . (46)
670670
input[value=""] - input
671-
"{CURSOR}0" -> "{CURSOR}"
671+
"{CURSOR}3" -> "{CURSOR}"
672672
input[value=""] - keyup: . (46)
673673
input[value=""] - keydown: 3 (51)
674674
input[value=""] - keypress: 3 (51)
675-
input[value=".3"] - input
676-
"{CURSOR}" -> "{CURSOR}.3"
677-
input[value=".3"] - keyup: 3 (51)
675+
input[value="3.3"] - input
676+
"{CURSOR}" -> "{CURSOR}3.3"
677+
input[value="3.3"] - keyup: 3 (51)
678678
`)
679679
})
680680

src/type.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,20 @@ async function typeImpl(
139139

140140
async function runCallbacks(callbacks) {
141141
const eventOverrides = {}
142-
let prevWasMinus, prevWasPeriod
142+
let prevWasMinus, prevWasPeriod, prevValue
143143
for (const callback of callbacks) {
144144
if (delay > 0) await wait(delay)
145145
if (!currentElement().disabled) {
146146
const returnValue = callback({
147147
prevWasMinus,
148148
prevWasPeriod,
149+
prevValue,
149150
eventOverrides,
150151
})
151152
Object.assign(eventOverrides, returnValue?.eventOverrides)
152153
prevWasMinus = returnValue?.prevWasMinus
153154
prevWasPeriod = returnValue?.prevWasPeriod
155+
prevValue = returnValue?.prevValue
154156
}
155157
}
156158
}
@@ -175,7 +177,12 @@ async function typeImpl(
175177

176178
function typeCharacter(
177179
char,
178-
{prevWasMinus = false, prevWasPeriod = false, eventOverrides},
180+
{
181+
prevWasMinus = false,
182+
prevWasPeriod = false,
183+
prevValue = '',
184+
eventOverrides,
185+
},
179186
) {
180187
const key = char // TODO: check if this also valid for characters with diacritic markers e.g. úé etc
181188
const keyCode = char.charCodeAt(0)
@@ -201,17 +208,18 @@ async function typeImpl(
201208
if (prevWasMinus) {
202209
newEntry = `-${char}`
203210
} else if (prevWasPeriod) {
204-
newEntry = `.${char}`
211+
newEntry = `${prevValue}.${char}`
205212
}
206213

207-
const {prevValue} = fireInputEventIfNeeded({
214+
const inputEvent = fireInputEventIfNeeded({
208215
...calculateNewValue(newEntry, currentElement()),
209216
eventOverrides: {
210217
data: key,
211218
inputType: 'insertText',
212219
...eventOverrides,
213220
},
214221
})
222+
prevValue = inputEvent.prevValue
215223

216224
// typing "-" into a number input will not actually update the value
217225
// so for the next character we type, the value should be set to
@@ -242,7 +250,11 @@ async function typeImpl(
242250
...eventOverrides,
243251
})
244252

245-
return {prevWasMinus: nextPrevWasMinus, prevWasPeriod: nextPrevWasPeriod}
253+
return {
254+
prevWasMinus: nextPrevWasMinus,
255+
prevWasPeriod: nextPrevWasPeriod,
256+
prevValue,
257+
}
246258
}
247259
}
248260

0 commit comments

Comments
 (0)