Skip to content

Commit

Permalink
fix: Update .type(' ') to not emit clicks when the keyup event has …
Browse files Browse the repository at this point in the history
…been prevented (#20156)

* fix: Update `.type(' ')` to not emit clicks when the keyup event has been prevented

* cleanup test for click event not firing while preventing default

Co-authored-by: Brian Mann <brian.mann86@gmail.com>
  • Loading branch information
tbiethman and brian-mann authored Feb 12, 2022
1 parent a1e6d5c commit 2c88f0c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/driver/cypress/fixtures/click-event-by-type.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@
target.addEventListener("click", () => {
updateLog("click");
});
target.addEventListener("keyup", () => {
target.addEventListener("keyup", (event) => {
updateLog("keyup");
});
});
</script>
</body>
</html>
</html>
92 changes: 77 additions & 15 deletions packages/driver/cypress/integration/commands/actions/type_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,7 @@ describe('src/cy/commands/actions/type - #type', () => {

targets.forEach((targetId) => {
it(`${targetId}`, () => {
cy.get(`#target-${targetId}`).focus()
cy.get(`#target-${targetId}`).type('{enter}')
cy.get(`#target-${targetId}`).focus().type('{enter}')

cy.get('li').eq(0).should('have.text', 'keydown')
cy.get('li').eq(1).should('have.text', 'keypress')
Expand All @@ -593,8 +592,7 @@ describe('src/cy/commands/actions/type - #type', () => {

targets.forEach((targetId) => {
it(`${targetId}`, () => {
cy.get(`#target-${targetId}`).focus()
cy.get(`#target-${targetId}`).type('{enter}')
cy.get(`#target-${targetId}`).focus().type('{enter}')

cy.get('li').eq(0).should('have.text', 'keydown')
cy.get('li').eq(1).should('have.text', 'keypress')
Expand All @@ -610,17 +608,30 @@ describe('src/cy/commands/actions/type - #type', () => {
})

const targets = [
'button-tag',
'input-button',
'input-image',
'input-reset',
'input-submit',
'#target-button-tag',
'#target-input-button',
'#target-input-image',
'#target-input-reset',
'#target-input-submit',
]

describe(`triggers with single space`, () => {
targets.forEach((targetId) => {
it(targetId, () => {
cy.get(`#target-${targetId}`).focus().type(' ')
targets.forEach((target) => {
it(target, () => {
const events = []

$(target).on('keydown keypress keyup click', (evt) => {
events.push(evt.type)
})

cy.get(target).focus().type(' ').then(() => {
expect(events).to.deep.eq([
'keydown',
'keypress',
'keyup',
'click',
])
})

cy.get('li').eq(0).should('have.text', 'keydown')
cy.get('li').eq(1).should('have.text', 'keypress')
Expand All @@ -630,10 +641,61 @@ describe('src/cy/commands/actions/type - #type', () => {
})
})

describe(`does not trigger if keyup prevented`, () => {
targets.forEach((target) => {
it(`${target} does not fire click event`, () => {
const events = []

$(target)
.on('keydown keypress keyup click', (evt) => {
events.push(evt.type)
})
.on('keyup', (evt) => {
evt.preventDefault()
})

cy.get(target).focus().type(' ').then(() => {
expect(events).to.deep.eq([
'keydown',
'keypress',
'keyup',
])
})

cy.get('li').should('have.length', 3)
cy.get('li').eq(0).should('have.text', 'keydown')
cy.get('li').eq(1).should('have.text', 'keypress')
cy.get('li').eq(2).should('have.text', 'keyup')
})
})
})

describe('triggers after other characters', () => {
targets.forEach((targetId) => {
it(targetId, () => {
cy.get(`#target-${targetId}`).focus().type('asd ')
targets.forEach((target) => {
it(target, () => {
const events = []

$(target).on('keydown keypress keyup click', (evt) => {
events.push(evt.type)
})

cy.get(target).focus().type('asd ').then(() => {
expect(events).to.deep.eq([
'keydown',
'keypress',
'keyup',
'keydown',
'keypress',
'keyup',
'keydown',
'keypress',
'keyup',
'keydown',
'keypress',
'keyup',
'click',
])
})

cy.get('li').eq(12).should('have.text', 'click')
})
Expand Down
4 changes: 3 additions & 1 deletion packages/driver/src/cy/commands/actions/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ export default function (Commands, Cypress, cy, state, config) {
// event.target is null when used with shadow DOM.
(event.target && $elements.isButtonLike(event.target)) &&
// When a space key is pressed for input radio elements, the click event is only fired when it's not checked.
!(event.target.tagName === 'INPUT' && event.target.type === 'radio' && event.target.checked === true)
!(event.target.tagName === 'INPUT' && event.target.type === 'radio' && event.target.checked === true) &&
// When event is prevented, the click event should not be emitted
!event.defaultPrevented
) {
fireClickEvent(event.target)
}
Expand Down

3 comments on commit 2c88f0c

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 2c88f0c Feb 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/9.5.0/linux-x64/circle-develop-2c88f0c58ec3c1bd8421a05259fc2ab893191f99/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 2c88f0c Feb 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/9.5.0/win32-x64/circle-develop-2c88f0c58ec3c1bd8421a05259fc2ab893191f99/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 2c88f0c Feb 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/9.5.0/darwin-x64/circle-develop-2c88f0c58ec3c1bd8421a05259fc2ab893191f99/cypress.tgz

Please sign in to comment.