Skip to content

Commit

Permalink
Add a test for the escape event
Browse files Browse the repository at this point in the history
  • Loading branch information
KittyGiraudel committed Aug 9, 2024
1 parent fed40ec commit c716af5
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions cypress/e2e/state.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,52 @@ describe('State', { testIsolation: false }, () => {
cy.get('.dialog').then(shouldBeHidden)
})

it('should not close when pressing ESC if the event is captured and canceled', () => {
const onEscapeWithoutDefaultPrevention = event => {
expect(event.detail.target.hasAttribute('data-a11y-dialog-hide'))
}
const onEscapeWithDefaultPrevention = event => {
event.preventDefault()
expect(event.detail.target.hasAttribute('data-a11y-dialog-hide'))
}

// Listen to ESC events, but do not prevent them
cy.get('[data-a11y-dialog]').then($node =>
$node[0].addEventListener('escape', onEscapeWithoutDefaultPrevention)
)

// Open the dialog and expect it to be visible
cy.get('[data-a11y-dialog-show="my-dialog"]').click()
cy.get('.dialog').then(shouldBeVisible)

// Press ESC and expect it to close
cy.realPress('Escape')
cy.get('.dialog').then(shouldBeHidden)

// Start canceling escape events
cy.get('[data-a11y-dialog]').then($node =>{
$node[0].removeEventListener('escape', onEscapeWithoutDefaultPrevention)
$node[0].addEventListener('escape', onEscapeWithDefaultPrevention)
})

// Open the dialog and expect it to be visible
cy.get('[data-a11y-dialog-show="my-dialog"]').click()
cy.get('.dialog').then(shouldBeVisible)

// Press ESC and expect it to still be visible
cy.realPress('Escape')
cy.get('.dialog').then(shouldBeVisible)

// Stop canceling ESC events
cy.get('[data-a11y-dialog]').then($node =>{
$node[0].removeEventListener('escape', onEscapeWithDefaultPrevention)
})

// Press ESC and expect it to be closed
cy.realPress('Escape')
cy.get('.dialog').then(shouldBeHidden)
})

it('should close when clicking the backdrop', () => {
cy.get('[data-a11y-dialog-show="my-dialog"]').click()
cy.get('.dialog-overlay').click({ force: true })
Expand Down

0 comments on commit c716af5

Please sign in to comment.