-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
complete keyboard menu navigation tests
- Loading branch information
Showing
7 changed files
with
202 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
incubator/aria-navigator/tests/menu/navigation/keyboard/submenu/arrow-left.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { Menu } from '../../../../../src'; | ||
import { settled } from '../../../../utils'; | ||
import { createRefactorMenu, getItems } from '../../../-shared'; | ||
|
||
describe('Menu Navigation', () => { | ||
describe('submenu navigation', () => { | ||
describe('close with `ArrowLeft`', async () => { | ||
const { refactorMenu, shareMenu } = createRefactorMenu(); | ||
|
||
const menu = new Menu(refactorMenu); | ||
const share = new Menu(shareMenu); | ||
const codeItem = share.items[0]; | ||
|
||
expect(shareMenu.matches(':popover-open')).toBeFalsy(); | ||
|
||
const { fourthItem } = getItems(menu); | ||
|
||
refactorMenu.dispatchEvent(new FocusEvent('focusin')); | ||
refactorMenu.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' })); | ||
refactorMenu.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' })); | ||
refactorMenu.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' })); | ||
|
||
refactorMenu.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowRight' })); | ||
await settled(); | ||
|
||
expect(shareMenu.matches(':popover-open')).toBeTruthy(); | ||
expect(codeItem.getAttribute('tabindex')).toBe('0'); | ||
expect(document.activeElement).toBe(codeItem); | ||
|
||
it('use `ArrowLeft` to close submenu', () => { | ||
// await settled(); | ||
shareMenu.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowLeft' })); | ||
expect(shareMenu.matches(':popover-open')).toBeFalsy(); | ||
}); | ||
|
||
it('has focus moved to the trigger of the submenu', async () => { | ||
await settled(); | ||
expect(document.activeElement).toBe(fourthItem); | ||
}); | ||
}); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
incubator/aria-navigator/tests/menu/navigation/keyboard/submenu/arrow-right.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { Menu } from '../../../../../src'; | ||
import { settled } from '../../../../utils'; | ||
import { createRefactorMenu, getItems } from '../../../-shared'; | ||
|
||
describe('Menu Navigation', () => { | ||
describe('submenu navigation', () => { | ||
describe('open with `ArrowRight`', () => { | ||
const { refactorMenu, shareMenu } = createRefactorMenu(); | ||
|
||
const menu = new Menu(refactorMenu); | ||
|
||
expect(shareMenu.matches(':popover-open')).toBeFalsy(); | ||
|
||
const { fourthItem } = getItems(menu); | ||
|
||
refactorMenu.dispatchEvent(new FocusEvent('focusin')); | ||
|
||
it('use `ArrowRight` to open submenu', () => { | ||
refactorMenu.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' })); | ||
refactorMenu.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' })); | ||
refactorMenu.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' })); | ||
|
||
expect(fourthItem.getAttribute('tabindex')).toBe('0'); | ||
|
||
refactorMenu.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowRight' })); | ||
|
||
expect(shareMenu.matches(':popover-open')).toBeTruthy(); | ||
}); | ||
|
||
it('has focus on the first item of the submenu', async () => { | ||
const share = new Menu(shareMenu); | ||
const firstItem = share.items[0]; | ||
|
||
await settled(); | ||
|
||
expect(firstItem.getAttribute('tabindex')).toBe('0'); | ||
expect(document.activeElement).toBe(firstItem); | ||
}); | ||
}); | ||
}); | ||
}); |
65 changes: 65 additions & 0 deletions
65
incubator/aria-navigator/tests/menu/navigation/keyboard/submenu/enter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { Menu } from '../../../../../src'; | ||
import { settled } from '../../../../utils'; | ||
import { createRefactorMenu, getItems } from '../../../-shared'; | ||
|
||
describe('Menu Navigation', () => { | ||
describe('submenu navigation', () => { | ||
describe('open with `Enter`', () => { | ||
const { refactorMenu, shareMenu } = createRefactorMenu(); | ||
|
||
const menu = new Menu(refactorMenu); | ||
|
||
expect(shareMenu.matches(':popover-open')).toBeFalsy(); | ||
|
||
const { fourthItem } = getItems(menu); | ||
|
||
refactorMenu.dispatchEvent(new FocusEvent('focusin')); | ||
|
||
it('use `Enter` to open submenu', () => { | ||
refactorMenu.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' })); | ||
refactorMenu.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' })); | ||
refactorMenu.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' })); | ||
|
||
expect(fourthItem.getAttribute('tabindex')).toBe('0'); | ||
|
||
// Apparently, it is not really possible to trigger native behavior | ||
// The keyboard event is the same as the native recorded one, | ||
// though the native one has an `isTrusted` property set to true, which is | ||
// not simulatable. | ||
// | ||
// My hunch is, it needs to be a trusted event in order to trigger the popover | ||
// | ||
// @see https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted | ||
|
||
// fourthItem.dispatchEvent( | ||
// new KeyboardEvent('keydown', { | ||
// key: 'Enter', | ||
// code: 'Enter', | ||
// which: 13, | ||
// keyCode: 13, | ||
// view: window, | ||
// bubbles: true | ||
// }) | ||
// ); | ||
|
||
// `click()` invokes the popover and is the `Enter` simulation | ||
// the pointer tests checking for hover anyway, so seems good here | ||
fourthItem.click(); | ||
|
||
expect(shareMenu.matches(':popover-open')).toBeTruthy(); | ||
}); | ||
|
||
it('has focus on the first item of the submenu', async () => { | ||
const share = new Menu(shareMenu); | ||
const firstItem = share.items[0]; | ||
|
||
await settled(); | ||
|
||
expect(firstItem.getAttribute('tabindex')).toBe('0'); | ||
expect(document.activeElement).toBe(firstItem); | ||
}); | ||
}); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
incubator/aria-navigator/tests/menu/navigation/keyboard/submenu/escape.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { Menu } from '../../../../../src'; | ||
import { settled } from '../../../../utils'; | ||
import { createRefactorMenu, getItems } from '../../../-shared'; | ||
|
||
describe('Menu Navigation', () => { | ||
describe('submenu navigation', () => { | ||
describe('close with `Escape`', async () => { | ||
const { refactorMenu, shareMenu } = createRefactorMenu(); | ||
|
||
const menu = new Menu(refactorMenu); | ||
const share = new Menu(shareMenu); | ||
const codeItem = share.items[0]; | ||
|
||
expect(shareMenu.matches(':popover-open')).toBeFalsy(); | ||
|
||
const { fourthItem } = getItems(menu); | ||
|
||
refactorMenu.dispatchEvent(new FocusEvent('focusin')); | ||
refactorMenu.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' })); | ||
refactorMenu.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' })); | ||
refactorMenu.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' })); | ||
|
||
refactorMenu.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowRight' })); | ||
await settled(); | ||
|
||
expect(shareMenu.matches(':popover-open')).toBeTruthy(); | ||
expect(codeItem.getAttribute('tabindex')).toBe('0'); | ||
expect(document.activeElement).toBe(codeItem); | ||
|
||
it('use `Escape` to close submenu', () => { | ||
// this is our "Escape" *cough* *cough* | ||
// see enter.test.ts for more on this | ||
codeItem.click(); | ||
|
||
shareMenu.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowLeft' })); | ||
expect(shareMenu.matches(':popover-open')).toBeFalsy(); | ||
}); | ||
|
||
it('has focus moved to the trigger of the submenu', async () => { | ||
await settled(); | ||
expect(document.activeElement).toBe(fourthItem); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export async function settled() { | ||
await new Promise((resolve) => window.setTimeout(resolve, 1)); | ||
} |