From 16b76f4d99807f2466abe9c9ab25da8f26c0a293 Mon Sep 17 00:00:00 2001 From: Ben Kucera <14625260+Bkucera@users.noreply.github.com> Date: Mon, 23 Dec 2019 12:11:12 -0500 Subject: [PATCH] fix: edge cases during cy.type in number input (#6033) - regressions introduced in `3.8.0` --- packages/driver/src/cy/keyboard.ts | 12 +++++++++--- .../integration/commands/actions/type_spec.js | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/driver/src/cy/keyboard.ts b/packages/driver/src/cy/keyboard.ts index aca683bba940..478c53a134a9 100644 --- a/packages/driver/src/cy/keyboard.ts +++ b/packages/driver/src/cy/keyboard.ts @@ -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 = { @@ -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 } @@ -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() }, diff --git a/packages/driver/test/cypress/integration/commands/actions/type_spec.js b/packages/driver/test/cypress/integration/commands/actions/type_spec.js index 3546a38b547f..76d6f9013054 100644 --- a/packages/driver/test/cypress/integration/commands/actions/type_spec.js +++ b/packages/driver/test/cypress/integration/commands/actions/type_spec.js @@ -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]', () => {