Skip to content

Commit 2c88f0c

Browse files
fix: Update .type(' ') to not emit clicks when the keyup event has 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 <[email protected]>
1 parent a1e6d5c commit 2c88f0c

File tree

3 files changed

+82
-18
lines changed

3 files changed

+82
-18
lines changed

packages/driver/cypress/fixtures/click-event-by-type.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@
6666
target.addEventListener("click", () => {
6767
updateLog("click");
6868
});
69-
target.addEventListener("keyup", () => {
69+
target.addEventListener("keyup", (event) => {
7070
updateLog("keyup");
7171
});
7272
});
7373
</script>
7474
</body>
75-
</html>
75+
</html>

packages/driver/cypress/integration/commands/actions/type_spec.js

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,7 @@ describe('src/cy/commands/actions/type - #type', () => {
574574

575575
targets.forEach((targetId) => {
576576
it(`${targetId}`, () => {
577-
cy.get(`#target-${targetId}`).focus()
578-
cy.get(`#target-${targetId}`).type('{enter}')
577+
cy.get(`#target-${targetId}`).focus().type('{enter}')
579578

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

594593
targets.forEach((targetId) => {
595594
it(`${targetId}`, () => {
596-
cy.get(`#target-${targetId}`).focus()
597-
cy.get(`#target-${targetId}`).type('{enter}')
595+
cy.get(`#target-${targetId}`).focus().type('{enter}')
598596

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

612610
const targets = [
613-
'button-tag',
614-
'input-button',
615-
'input-image',
616-
'input-reset',
617-
'input-submit',
611+
'#target-button-tag',
612+
'#target-input-button',
613+
'#target-input-image',
614+
'#target-input-reset',
615+
'#target-input-submit',
618616
]
619617

620618
describe(`triggers with single space`, () => {
621-
targets.forEach((targetId) => {
622-
it(targetId, () => {
623-
cy.get(`#target-${targetId}`).focus().type(' ')
619+
targets.forEach((target) => {
620+
it(target, () => {
621+
const events = []
622+
623+
$(target).on('keydown keypress keyup click', (evt) => {
624+
events.push(evt.type)
625+
})
626+
627+
cy.get(target).focus().type(' ').then(() => {
628+
expect(events).to.deep.eq([
629+
'keydown',
630+
'keypress',
631+
'keyup',
632+
'click',
633+
])
634+
})
624635

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

644+
describe(`does not trigger if keyup prevented`, () => {
645+
targets.forEach((target) => {
646+
it(`${target} does not fire click event`, () => {
647+
const events = []
648+
649+
$(target)
650+
.on('keydown keypress keyup click', (evt) => {
651+
events.push(evt.type)
652+
})
653+
.on('keyup', (evt) => {
654+
evt.preventDefault()
655+
})
656+
657+
cy.get(target).focus().type(' ').then(() => {
658+
expect(events).to.deep.eq([
659+
'keydown',
660+
'keypress',
661+
'keyup',
662+
])
663+
})
664+
665+
cy.get('li').should('have.length', 3)
666+
cy.get('li').eq(0).should('have.text', 'keydown')
667+
cy.get('li').eq(1).should('have.text', 'keypress')
668+
cy.get('li').eq(2).should('have.text', 'keyup')
669+
})
670+
})
671+
})
672+
633673
describe('triggers after other characters', () => {
634-
targets.forEach((targetId) => {
635-
it(targetId, () => {
636-
cy.get(`#target-${targetId}`).focus().type('asd ')
674+
targets.forEach((target) => {
675+
it(target, () => {
676+
const events = []
677+
678+
$(target).on('keydown keypress keyup click', (evt) => {
679+
events.push(evt.type)
680+
})
681+
682+
cy.get(target).focus().type('asd ').then(() => {
683+
expect(events).to.deep.eq([
684+
'keydown',
685+
'keypress',
686+
'keyup',
687+
'keydown',
688+
'keypress',
689+
'keyup',
690+
'keydown',
691+
'keypress',
692+
'keyup',
693+
'keydown',
694+
'keypress',
695+
'keyup',
696+
'click',
697+
])
698+
})
637699

638700
cy.get('li').eq(12).should('have.text', 'click')
639701
})

packages/driver/src/cy/commands/actions/type.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,9 @@ export default function (Commands, Cypress, cy, state, config) {
342342
// event.target is null when used with shadow DOM.
343343
(event.target && $elements.isButtonLike(event.target)) &&
344344
// When a space key is pressed for input radio elements, the click event is only fired when it's not checked.
345-
!(event.target.tagName === 'INPUT' && event.target.type === 'radio' && event.target.checked === true)
345+
!(event.target.tagName === 'INPUT' && event.target.type === 'radio' && event.target.checked === true) &&
346+
// When event is prevented, the click event should not be emitted
347+
!event.defaultPrevented
346348
) {
347349
fireClickEvent(event.target)
348350
}

0 commit comments

Comments
 (0)