Skip to content

Commit

Permalink
edge cases for typing into number inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
kuceb committed Dec 23, 2019
1 parent 37dd275 commit e27ea1c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/driver/src/cy/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const monthRe = /^\d{4}-(0\d|1[0-2])/
const weekRe = /^\d{4}-W(0[1-9]|[1-4]\d|5[0-3])/
const timeRe = /^([0-1]\d|2[0-3]):[0-5]\d(:[0-5]\d)?(\.[0-9]{1,3})?/
const dateTimeRe = /^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}/
const numberRe = /^-?(0|[1-9]\d*)(\.\d+)?(e-?(0|[1-9]\d*))?$/i
const numberRe = /^-?(\d+|\d+\.\d+|\.\d+)([eE][-+]?\d+)?$/i
const charsBetweenCurlyBracesRe = /({.+?})/

const INITIAL_MODIFIERS = {
Expand Down Expand Up @@ -260,7 +260,11 @@ const shouldUpdateValue = (el: HTMLElement, key: KeyDetails, options) => {

if (!(numberRe.test(potentialValue))) {
debug('skipping inserting value since number input would be invalid', key.text, potentialValue)
options.prevVal = needsValue + key.text
if (key.text.match(/[-+eE\d\.]/)) {
options.prevVal = needsValue + key.text
} else {
options.prevVal = ''
}

return
}
Expand Down Expand Up @@ -477,7 +481,9 @@ function _getEndIndex (str, substr) {
// Simulated default actions for select few keys.
const simulatedDefaultKeyMap: { [key: string]: SimulatedDefault } = {
Enter: (el, key, options) => {
$selection.replaceSelectionContents(el, '\n')
if (!$elements.isInput(el)) {
$selection.replaceSelectionContents(el, '\n')
}

options.onEnterPressed()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,20 @@ describe('src/cy/commands/actions/type', () => {
expect(blurred).to.be.calledOnce
})
})

// https://github.com/cypress-io/cypress/issues/5997
it('type=number can accept values with commas (,)', () => {
cy.get('#number-without-value')
.type('1,000')
.should('have.value', '1000')
})

// https://github.com/cypress-io/cypress/issues/5968
it('type=number can include {enter}', () => {
cy.get('#number-without-value')
.type('100{enter}')
.should('have.value', '100')
})
})

describe('input[type=email]', () => {
Expand Down

0 comments on commit e27ea1c

Please sign in to comment.