From 0b726c7754cfa594f2c16d051a638cf50c2f0d39 Mon Sep 17 00:00:00 2001 From: Paul Kim Date: Sun, 17 Mar 2024 11:57:14 -0400 Subject: [PATCH] Fix Modal Dialog References (#225) * Update data modal target on example * Fix controller names * Replace fixture file html * Actually test modal click * Test closing modal * Test escape key * Separate click and keyboard tests --- docs/modal.md | 2 +- index.html | 8 ++++---- package.json | 1 + src/modal.js | 1 - test/fixtures/index.html | 28 --------------------------- test/fixtures/modal.html | 17 +++++++++++++++++ test/modal_test.js | 41 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 64 insertions(+), 34 deletions(-) create mode 100644 test/fixtures/modal.html create mode 100644 test/modal_test.js diff --git a/docs/modal.md b/docs/modal.md index e7b6427..0ee8590 100644 --- a/docs/modal.md +++ b/docs/modal.md @@ -11,7 +11,7 @@ application.register('modal', Modal) ```html
- +

This modal dialog has a groovy backdrop!

diff --git a/index.html b/index.html index c615f0a..c3a8f28 100644 --- a/index.html +++ b/index.html @@ -179,12 +179,12 @@

Dropdowns

Modals

Modals use the <dialog> html element and can be closed with Esc or a button. See the dialog element MDN docs.

-
- +
+

This modal dialog has a groovy backdrop!

- +
- +
diff --git a/package.json b/package.json index f17415e..dfd14d6 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "devDependencies": { "@open-wc/testing": "^4.0.0", "@web/test-runner": "^0.18.0", + "@web/test-runner-commands": "^0.9.0", "esbuild": "^0.20.0" }, "peerDependencies": { diff --git a/src/modal.js b/src/modal.js index b7b1f6a..10b91d3 100644 --- a/src/modal.js +++ b/src/modal.js @@ -1,5 +1,4 @@ import { Controller } from "@hotwired/stimulus" -import { enter, leave } from "./transition" export default class extends Controller { static targets = ["dialog"] diff --git a/test/fixtures/index.html b/test/fixtures/index.html index d50f500..cbc9db0 100644 --- a/test/fixtures/index.html +++ b/test/fixtures/index.html @@ -87,34 +87,6 @@

Slideovers

-
-

Modals

- -
- - Open Modal - - - - -
-
-

Tabs

diff --git a/test/fixtures/modal.html b/test/fixtures/modal.html new file mode 100644 index 0000000..debef03 --- /dev/null +++ b/test/fixtures/modal.html @@ -0,0 +1,17 @@ +
+

Modals

+

Modals use the <dialog> html element and can be closed with Esc or a button. See the + dialog element MDN docs. +

+ +
+ +

This modal dialog has a groovy backdrop!

+ +
+ +
+
\ No newline at end of file diff --git a/test/modal_test.js b/test/modal_test.js new file mode 100644 index 0000000..4422f20 --- /dev/null +++ b/test/modal_test.js @@ -0,0 +1,41 @@ +import { fixture, expect } from '@open-wc/testing' +import { sendKeys } from '@web/test-runner-commands'; +import { fetchFixture } from './test_helpers' + +import { Application } from '@hotwired/stimulus' +import Modal from '../src/modal' + +describe('ModalController', () => { + describe('#default', () => { + beforeEach(async () => { + const html = await fetchFixture('modal.html') + await fixture(html) + const application = Application.start() + application.register('modal', Modal) + }) + + const openModalButton = document.querySelector("[data-action='modal#open']") + + it('clicks to open and close the modal', async () => { + const dialog = document.querySelector("dialog") + const openModalButton = document.querySelector("[data-action='modal#open']") + openModalButton.click() + expect(dialog.hasAttribute("open")).to.equal(true) + + const closeModalButton = document.querySelector("[data-action='modal#close']") + closeModalButton.click() + expect(dialog.hasAttribute("open")).to.equal(false) + }) + + it('uses the keyboard to open and close the modal', async () => { + const dialog = document.querySelector("dialog") + const openModalButton = document.querySelector("[data-action='modal#open']") + openModalButton.focus() + await sendKeys({ press: 'Space' }); + expect(dialog.hasAttribute("open")).to.equal(true) + + await sendKeys({ press: 'Escape' }); + expect(dialog.hasAttribute("open")).to.equal(false) + }) + }) +})