Skip to content
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

Fix leading number being trimmed on point type #376

Merged
merged 1 commit into from Jun 21, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/__tests__/type.js
Expand Up @@ -641,11 +641,11 @@ test('can type "-" into number inputs', () => {
// https://github.com/testing-library/user-event/issues/336
test('can type "." into number inputs', () => {
const {element, getEventSnapshot} = setup('<input type="number" />')
userEvent.type(element, '0.3')
expect(element).toHaveValue(0.3)
userEvent.type(element, '3.3')
expect(element).toHaveValue(3.3)

expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: input[value=".3"]
Events fired on: input[value="3.3"]

input[value=""] - pointerover
input[value=""] - pointerenter
Expand All @@ -660,21 +660,21 @@ test('can type "." into number inputs', () => {
input[value=""] - pointerup
input[value=""] - mouseup: Left (0)
input[value=""] - click: Left (0)
input[value=""] - keydown: 0 (48)
input[value=""] - keypress: 0 (48)
input[value="0"] - input
"{CURSOR}" -> "{CURSOR}0"
input[value="0"] - keyup: 0 (48)
input[value="0"] - keydown: . (46)
input[value="0"] - keypress: . (46)
input[value=""] - keydown: 3 (51)
input[value=""] - keypress: 3 (51)
input[value="3"] - input
"{CURSOR}" -> "{CURSOR}3"
input[value="3"] - keyup: 3 (51)
input[value="3"] - keydown: . (46)
input[value="3"] - keypress: . (46)
input[value=""] - input
"{CURSOR}0" -> "{CURSOR}"
"{CURSOR}3" -> "{CURSOR}"
input[value=""] - keyup: . (46)
input[value=""] - keydown: 3 (51)
input[value=""] - keypress: 3 (51)
input[value=".3"] - input
"{CURSOR}" -> "{CURSOR}.3"
input[value=".3"] - keyup: 3 (51)
input[value="3.3"] - input
"{CURSOR}" -> "{CURSOR}3.3"
input[value="3.3"] - keyup: 3 (51)
`)
})

Expand Down
22 changes: 17 additions & 5 deletions src/type.js
Expand Up @@ -132,18 +132,20 @@ async function typeImpl(

async function runCallbacks(callbacks) {
const eventOverrides = {}
let prevWasMinus, prevWasPeriod
let prevWasMinus, prevWasPeriod, prevValue
for (const callback of callbacks) {
if (delay > 0) await wait(delay)
if (!currentElement().disabled) {
const returnValue = callback({
prevWasMinus,
prevWasPeriod,
prevValue,
eventOverrides,
})
Object.assign(eventOverrides, returnValue?.eventOverrides)
prevWasMinus = returnValue?.prevWasMinus
prevWasPeriod = returnValue?.prevWasPeriod
prevValue = returnValue?.prevValue
}
}
}
Expand All @@ -168,7 +170,12 @@ async function typeImpl(

function typeCharacter(
char,
{prevWasMinus = false, prevWasPeriod = false, eventOverrides},
{
prevWasMinus = false,
prevWasPeriod = false,
prevValue = '',
eventOverrides,
},
) {
const key = char // TODO: check if this also valid for characters with diacritic markers e.g. úé etc
const keyCode = char.charCodeAt(0)
Expand All @@ -194,17 +201,18 @@ async function typeImpl(
if (prevWasMinus) {
newEntry = `-${char}`
} else if (prevWasPeriod) {
newEntry = `.${char}`
newEntry = `${prevValue}.${char}`
}

const {prevValue} = fireInputEventIfNeeded({
const inputEvent = fireInputEventIfNeeded({
...calculateNewValue(newEntry, currentElement()),
eventOverrides: {
data: key,
inputType: 'insertText',
...eventOverrides,
},
})
prevValue = inputEvent.prevValue

// typing "-" into a number input will not actually update the value
// so for the next character we type, the value should be set to
Expand Down Expand Up @@ -235,7 +243,11 @@ async function typeImpl(
...eventOverrides,
})

return {prevWasMinus: nextPrevWasMinus, prevWasPeriod: nextPrevWasPeriod}
return {
prevWasMinus: nextPrevWasMinus,
prevWasPeriod: nextPrevWasPeriod,
prevValue,
}
}
}

Expand Down