Skip to content

Commit

Permalink
Add additional tests for web-components
Browse files Browse the repository at this point in the history
  • Loading branch information
KittyGiraudel committed Oct 3, 2023
1 parent 12e21f4 commit 2ff9c65
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
29 changes: 29 additions & 0 deletions cypress/e2e/webComponents.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { shouldBeHidden, shouldBeVisible } from './utils.js'

describe('Web Components', () => {
beforeEach(() => cy.visit('/web-components'))

it('should focus the dialog container on open', () => {
cy.get('my-dialog').shadow().find('[data-show]').click()
cy.get('my-dialog').then(shouldBeVisible)
cy.get('my-dialog').should('have.focus')
})

it('should close with ESC', () => {
cy.get('my-dialog').shadow().find('[data-show]').click()
cy.realPress('Escape')
cy.get('my-dialog').then(shouldBeHidden)
})

it('should close with close button', () => {
cy.get('my-dialog').shadow().find('[data-show]').click()
cy.get('my-dialog').shadow().find('.dialog-close').click()
cy.get('my-dialog').then(shouldBeHidden)
})

it('should restore focus to the previously focused element', () => {
cy.get('my-dialog').shadow().find('[data-show]').click()
cy.get('my-dialog').shadow().find('.dialog-close').click()
cy.get('my-dialog').shadow().find('[data-show]').should('have.focus')
})
})
49 changes: 49 additions & 0 deletions cypress/fixtures/web-components.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!doctype html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>Tests — Web Components</title>
<link rel="stylesheet" href="../styles.css" />
</head>

<body>
<main>
<h1>Tests — Web Components</h1>

<my-dialog>
<template>
<button type="button" data-show>Open the dialog</button>
<div class="dialog" id="my-dialog" aria-labelledby="my-dialog-title" aria-hidden="true">
<div class="dialog-overlay" data-a11y-dialog-hide></div>
<div class="dialog-content" role="document">
<button data-a11y-dialog-hide class="dialog-close" aria-label="Close this dialog window">&times;</button>
<h1 id="my-dialog-title">Your dialog title</h1>
</div>
</div>
<link rel="stylesheet" href="../styles.css" />
</template>
</my-dialog>
</main>

<script src="./a11y-dialog.js"></script>
<script>
class MyDialog extends HTMLElement {
connectedCallback() {
const shadow = this.attachShadow({ mode: 'open' });
const template = this.querySelector('template');
shadow.appendChild(template.content.cloneNode(true));

const container = this.shadowRoot.querySelector("#my-dialog");
const dialog = new A11yDialog(container);

const trigger = this.shadowRoot.querySelector('[data-show]');
trigger.addEventListener('click', () => dialog.show())
}
}

customElements.define("my-dialog", MyDialog);
</script>
</body>

</html>

0 comments on commit 2ff9c65

Please sign in to comment.