|
| 1 | +import { expect } from '@esm-bundle/chai'; |
| 2 | +import { fixtureSync, nextRender } from '@vaadin/testing-helpers'; |
| 3 | +import { sendKeys } from '@web/test-runner-commands'; |
| 4 | +import './not-animated-styles.js'; |
| 5 | +import '../vaadin-context-menu.js'; |
| 6 | +import { getDeepActiveElement } from '@vaadin/a11y-base/src/focus-utils.js'; |
| 7 | +import { getMenuItems, outsideClick } from './helpers.js'; |
| 8 | + |
| 9 | +describe('a11y', () => { |
| 10 | + describe('focus restoration', () => { |
| 11 | + let contextMenu, contextMenuButton, beforeButton, afterButton; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + const wrapper = fixtureSync(` |
| 15 | + <div> |
| 16 | + <button>Before</button> |
| 17 | + <vaadin-context-menu open-on="click"> |
| 18 | + <button>Open context menu</button> |
| 19 | + </vaadin-context-menu> |
| 20 | + <button>After</button> |
| 21 | + `); |
| 22 | + [beforeButton, contextMenu, afterButton] = wrapper.children; |
| 23 | + contextMenu.items = [{ text: 'Item 0' }, { text: 'Item 1', children: [{ text: 'Item 1/0' }] }]; |
| 24 | + contextMenuButton = contextMenu.querySelector('button'); |
| 25 | + contextMenuButton.focus(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should move focus to the menu on open', async () => { |
| 29 | + contextMenuButton.click(); |
| 30 | + await nextRender(); |
| 31 | + const menuItem = getMenuItems(contextMenu)[0]; |
| 32 | + expect(getDeepActiveElement()).to.equal(menuItem); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should restore focus on outside click', async () => { |
| 36 | + contextMenuButton.click(); |
| 37 | + await nextRender(); |
| 38 | + outsideClick(); |
| 39 | + await nextRender(); |
| 40 | + expect(getDeepActiveElement()).to.equal(contextMenuButton); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should restore focus on outside click when a sub-menu is open', async () => { |
| 44 | + contextMenuButton.click(); |
| 45 | + await nextRender(); |
| 46 | + // Move focus to Item 1 |
| 47 | + await sendKeys({ press: 'ArrowDown' }); |
| 48 | + // Open Item 1 |
| 49 | + await sendKeys({ press: 'ArrowRight' }); |
| 50 | + await nextRender(); |
| 51 | + outsideClick(); |
| 52 | + await nextRender(); |
| 53 | + expect(getDeepActiveElement()).to.equal(contextMenuButton); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should restore focus on root menu item selection', async () => { |
| 57 | + contextMenuButton.click(); |
| 58 | + await nextRender(); |
| 59 | + // Select Item 0 |
| 60 | + await sendKeys({ press: 'Enter' }); |
| 61 | + await nextRender(); |
| 62 | + expect(getDeepActiveElement()).to.equal(contextMenuButton); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should restore focus on sub-menu item selection', async () => { |
| 66 | + contextMenuButton.click(); |
| 67 | + await nextRender(); |
| 68 | + // Move focus to Item 1 |
| 69 | + await sendKeys({ press: 'ArrowDown' }); |
| 70 | + // Open Item 1 |
| 71 | + await sendKeys({ press: 'ArrowRight' }); |
| 72 | + await nextRender(); |
| 73 | + // Select Item 1/1 |
| 74 | + await sendKeys({ press: 'Enter' }); |
| 75 | + await nextRender(); |
| 76 | + expect(getDeepActiveElement()).to.equal(contextMenuButton); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should move focus to the prev element outside the menu on Shift+Tab pressed inside', async () => { |
| 80 | + contextMenuButton.click(); |
| 81 | + await nextRender(); |
| 82 | + await sendKeys({ down: 'Shift' }); |
| 83 | + await sendKeys({ press: 'Tab' }); |
| 84 | + await sendKeys({ up: 'Shift' }); |
| 85 | + expect(getDeepActiveElement()).to.equal(beforeButton); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should move focus to the next element outside the menu on Tab pressed inside', async () => { |
| 89 | + contextMenuButton.click(); |
| 90 | + await nextRender(); |
| 91 | + await sendKeys({ press: 'Tab' }); |
| 92 | + expect(getDeepActiveElement()).to.equal(afterButton); |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments