|
| 1 | +/* global jest */ |
| 2 | + |
| 3 | +import { createDialog } from "src/decidim/a11y" |
| 4 | + |
| 5 | +describe("a11y dialog focus trap", () => { |
| 6 | + const dialogHtml = ` |
| 7 | + <button id="open-btn" data-dialog-open="testDialog">Open</button> |
| 8 | + <div id="test-dialog" data-dialog="testDialog"> |
| 9 | + <div data-dialog-container> |
| 10 | + <h2 id="dialog-title-testDialog" tabindex="-1" data-dialog-title>Test Dialog</h2> |
| 11 | + <a href="#">Link 1</a> |
| 12 | + <button>Button 1</button> |
| 13 | + <input type="text" /> |
| 14 | + <button>Button 2</button> |
| 15 | + <a href="#">Link 2</a> |
| 16 | + </div> |
| 17 | + <div data-dialog-actions> |
| 18 | + <button data-dialog-close="testDialog">Close</button> |
| 19 | + </div> |
| 20 | + </div> |
| 21 | + `; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + document.body.innerHTML = dialogHtml; |
| 25 | + window.Decidim = { |
| 26 | + currentDialogs: {} |
| 27 | + }; |
| 28 | + window.focusGuard = { |
| 29 | + trap: jest.fn(), |
| 30 | + disable: jest.fn() |
| 31 | + }; |
| 32 | + }); |
| 33 | + |
| 34 | + describe("keydown handler", () => { |
| 35 | + let dialogEl = null; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + const component = document.querySelector("[data-dialog]"); |
| 39 | + createDialog(component); |
| 40 | + dialogEl = document.querySelector("[data-dialog='testDialog']"); |
| 41 | + // Get the dialog from Decidim.currentDialogs and open it |
| 42 | + const dialog = window.Decidim.currentDialogs.testDialog; |
| 43 | + dialog.open(); |
| 44 | + }); |
| 45 | + |
| 46 | + it("adds keydown handler on open", () => { |
| 47 | + expect(dialogEl._focusTrapHandler).toBeDefined(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("handles Tab key", () => { |
| 51 | + const selectors = "a[href],button:not([disabled]),input:not([disabled]),select:not([disabled]),textarea:not([disabled]),[tabindex]:not([tabindex='-1'])"; |
| 52 | + const tabbableElements = Array.from(dialogEl.querySelectorAll(selectors)).filter( |
| 53 | + (el) => el.offsetParent || el.offsetParent === null |
| 54 | + ); |
| 55 | + tabbableElements[tabbableElements.length - 1].focus(); |
| 56 | + |
| 57 | + const event = new KeyboardEvent("keydown", { key: "Tab", bubbles: true }); |
| 58 | + const preventDefault = jest.fn(); |
| 59 | + event.preventDefault = preventDefault; |
| 60 | + |
| 61 | + dialogEl.dispatchEvent(event); |
| 62 | + |
| 63 | + expect(preventDefault).toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe("onClose cleanup", () => { |
| 68 | + it("removes the keydown handler on close", () => { |
| 69 | + const component = document.querySelector("[data-dialog]"); |
| 70 | + createDialog(component); |
| 71 | + const dialogEl = document.querySelector("[data-dialog='testDialog']"); |
| 72 | + |
| 73 | + const dialog = window.Decidim.currentDialogs.testDialog; |
| 74 | + dialog.open(); |
| 75 | + expect(dialogEl._focusTrapHandler).toBeDefined(); |
| 76 | + |
| 77 | + dialog.close(); |
| 78 | + expect(dialogEl._focusTrapHandler).toBeUndefined(); |
| 79 | + }); |
| 80 | + }); |
| 81 | +}); |
0 commit comments