Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KittyGiraudel committed Feb 14, 2021
1 parent 2df5d00 commit f625b52
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 32 deletions.
5 changes: 4 additions & 1 deletion cypress.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"video": false,
"baseUrl": "http://localhost:5000"
"baseUrl": "http://localhost:5000",
"ignoreTestFiles": [
"**/utils.js"
]
}
11 changes: 5 additions & 6 deletions cypress/integration/alertDialog.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { shouldBeHidden, shouldBeVisible } from './utils'

describe('Alert Dialog', () => {
before(() => cy.visit('/tests/alert-dialog'))

it('should prevent closing the dialog with escape', () => {
cy.get('[data-a11y-dialog-show="my-accessible-dialog"]').first().click()
cy.get('.dialog').should('not.have.attr', 'aria-hidden')
cy.get('.dialog-content').should('be.visible')
cy.get('.dialog').then(shouldBeVisible)
cy.get('body').trigger('keydown', { keyCode: 27, which: 27 })
cy.get('.dialog').should('not.have.attr', 'aria-hidden')
cy.get('.dialog-content').should('be.visible')
cy.get('.dialog').then(shouldBeVisible)
})

it('should prevent closing by clicking the backdrop', () => {
cy.get('.dialog-overlay').click({ force: true })
cy.get('.dialog').should('not.have.attr', 'aria-hidden')
cy.get('.dialog-content').should('be.visible')
cy.get('.dialog').then(shouldBeVisible)
})
})
5 changes: 3 additions & 2 deletions cypress/integration/dialogElement.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { shouldBeVisible } from './utils'

describe('<dialog> element', () => {
before(() => cy.visit('/tests/dialog-element'))

it('should handle the `open` attribute', () => {
cy.get('.dialog').should('not.have.attr', 'aria-hidden')
cy.get('dialog').should('be.visible')
cy.get('.dialog').then(shouldBeVisible)
cy.get('.dialog-close').click()
cy.get('dialog').should('not.have.attr', 'open')
})
Expand Down
11 changes: 5 additions & 6 deletions cypress/integration/instance.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { shouldBeHidden, shouldBeVisible } from './utils'

describe('Instance', () => {
before(() => cy.visit('/tests/instance'))

Expand All @@ -12,8 +14,7 @@ describe('Instance', () => {

it('should expose a show method on the instance', () => {
cy.window().its('instance').invoke('show')
cy.get('.dialog').should('not.have.attr', 'aria-hidden')
cy.get('.dialog-content').should('be.visible')
cy.get('.dialog').then(shouldBeVisible)
})

it('should expose status on the instance', () => {
Expand All @@ -22,8 +23,7 @@ describe('Instance', () => {

it('should expose a hide method on the instance', () => {
cy.window().its('instance').invoke('hide')
cy.get('.dialog').should('have.attr', 'aria-hidden', 'true')
cy.get('.dialog-overlay').should('not.be.visible')
cy.get('.dialog').then(shouldBeHidden)
cy.window().its('instance').its('shown').should('eq', false)
})

Expand Down Expand Up @@ -70,7 +70,6 @@ describe('Instance', () => {

it('should be possible to handle dialog destroy', () => {
cy.get('[data-a11y-dialog-show="my-accessible-dialog"]').click()
cy.get('.dialog').should('have.attr', 'aria-hidden', 'true')
cy.get('.dialog-overlay').should('not.be.visible')
cy.get('.dialog').then(shouldBeHidden)
})
})
32 changes: 15 additions & 17 deletions cypress/integration/state.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { shouldBeHidden, shouldBeVisible } from './utils'

describe('State', () => {
before(() => cy.visit('/tests/base'))

it('should hide the dialog by default', () => {
cy.get('.dialog').should('have.attr', 'aria-hidden', 'true')
cy.get('.dialog-overlay').should('not.be.visible')
cy.get('.dialog-content').should('not.be.visible')
cy.get('.dialog').then(shouldBeHidden)
})

it('should open when clicking an opener', () => {
cy.get('[data-a11y-dialog-show="my-accessible-dialog"]').first().click()
cy.get('.dialog').should('not.have.attr', 'aria-hidden')
cy.get('.dialog-content').should('be.visible')
cy.get('.dialog-content').should('have.attr', 'open')
it('should open the dialog when clicking an opener', () => {
cy.get('[data-a11y-dialog-show="something-else"]').click()
cy.get('.dialog').then(shouldBeHidden)

cy.get('[data-a11y-dialog-show="my-accessible-dialog"]').click()
cy.get('.dialog').then(shouldBeVisible)
})

it('should toggle aria-hidden on siblings/targets', () => {
it('should toggle the `aria-hidden` attribute on siblings/targets', () => {
cy.get('main').should('have.attr', 'aria-hidden', 'true')
cy.get('#pre-hidden-sibling').should(
'have.attr',
Expand All @@ -25,25 +26,22 @@ describe('State', () => {

it('should close when clicking a closer', () => {
cy.get('.dialog-close').click()
cy.get('.dialog').should('have.attr', 'aria-hidden', 'true')
cy.get('.dialog-content').should('not.be.visible')
cy.get('.dialog').then(shouldBeHidden)
cy.get('#pre-hidden-sibling').should(
'not.have.attr',
'data-a11y-dialog-original-aria-hidden'
)
})

it('should close when pressing ESC', () => {
cy.get('[data-a11y-dialog-show="my-accessible-dialog"]').first().click()
cy.get('[data-a11y-dialog-show="my-accessible-dialog"]').click()
cy.get('body').trigger('keydown', { keyCode: 27, which: 27 })
cy.get('.dialog').should('have.attr', 'aria-hidden', 'true')
cy.get('.dialog-content').should('not.be.visible')
cy.get('.dialog').then(shouldBeHidden)
})

it('should close when clicking the backdrop', () => {
cy.get('[data-a11y-dialog-show="my-accessible-dialog"]').first().click()
cy.get('[data-a11y-dialog-show="my-accessible-dialog"]').click()
cy.get('.dialog-overlay').click({ force: true })
cy.get('.dialog').should('have.attr', 'aria-hidden', 'true')
cy.get('.dialog-content').should('not.be.visible')
cy.get('.dialog').then(shouldBeHidden)
})
})
31 changes: 31 additions & 0 deletions cypress/integration/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export function shouldBeVisible($subject) {
Cypress.log({
displayName: 'visible',
name: 'shouldBeVisible',
message: 'The dialog should be visible',
consoleProps: () => ({ $el: $subject }),
})

cy.wrap($subject, { log: false }).should('not.have.attr', 'aria-hidden')
cy.wrap($subject, { log: false })
.find('.dialog-content', { log: false })
.should('be.visible')
.and('have.attr', 'open')
}

export function shouldBeHidden($subject) {
Cypress.log({
displayName: 'hidden',
name: 'shouldBeHidden',
message: 'The dialog should be hidden',
consoleProps: () => ({ $el: $subject }),
})

cy.wrap($subject, { log: false })
.should('have.attr', 'aria-hidden', 'true')
.find('.dialog-overlay', { log: false })
.should('not.be.visible')
cy.wrap($subject, { log: false })
.find('.dialog-content', { log: false })
.should('not.be.visible')
}
1 change: 1 addition & 0 deletions example/tests/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<main class="main" id="main" aria-label="Content">
<h1>Tests — Base</h1>
<button class="link-like" data-a11y-dialog-show="my-accessible-dialog">Open the dialog window</button>
<button class="link-like" data-a11y-dialog-show="something-else">Open the dialog window</button>
</main>

<div class="dialog" id="my-accessible-dialog" data-a11y-dialog>
Expand Down

0 comments on commit f625b52

Please sign in to comment.