-
Notifications
You must be signed in to change notification settings - Fork 4k
/
dropdown-menu.test.js
143 lines (112 loc) · 4.67 KB
/
dropdown-menu.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* WordPress dependencies
*/
import { createNewPost, pressKeyTimes } from '@wordpress/e2e-test-utils';
const moreMenuButtonSelector = '.components-button[aria-label="Options"]';
const moreMenuDropdownSelector =
'.components-dropdown-menu__menu[aria-label="Options"]';
const menuItemsSelector = [ 'menuitem', 'menuitemcheckbox', 'menuitemradio' ]
.map( ( role ) => `${ moreMenuDropdownSelector } [role="${ role }"]` )
.join( ',' );
describe( 'Dropdown Menu', () => {
beforeEach( async () => {
await createNewPost();
} );
it( 'allows navigation through each item using arrow keys', async () => {
await page.click( moreMenuButtonSelector );
const menuItems = await page.$$( menuItemsSelector );
// Catch any issues with the selector, which could cause a false positive test result.
expect( menuItems.length ).toBeGreaterThan( 0 );
let activeElementText = await page.evaluate(
() => document.activeElement.textContent
);
const [ firstMenuItem ] = menuItems;
const firstMenuItemText = await firstMenuItem.evaluate(
( element ) => element.textContent
);
// Expect the first menu item to be focused.
expect( activeElementText ).toBeDefined();
expect( activeElementText ).toBe( firstMenuItemText );
// Arrow down to the last item.
await pressKeyTimes( 'ArrowDown', menuItems.length - 1 );
activeElementText = await page.evaluate(
() => document.activeElement.textContent
);
const [ lastMenuItem ] = menuItems.slice( -1 );
const lastMenuItemText = await lastMenuItem.evaluate(
( element ) => element.textContent
);
// Expect the last menu item to be focused.
expect( activeElementText ).toBeDefined();
expect( activeElementText ).toBe( lastMenuItemText );
// Arrow back up to the first item.
await pressKeyTimes( 'ArrowUp', menuItems.length - 1 );
activeElementText = await page.evaluate(
() => document.activeElement.textContent
);
// Expect the first menu item to be focused again.
expect( activeElementText ).toBeDefined();
expect( activeElementText ).toBe( firstMenuItemText );
} );
it( 'loops to the beginning and end when navigating past the boundaries of the menu', async () => {
await page.click( moreMenuButtonSelector );
const menuItems = await page.$$( menuItemsSelector );
// Catch any issues with the selector, which could cause a false positive test result.
expect( menuItems.length ).toBeGreaterThan( 0 );
let activeElementText = await page.evaluate(
() => document.activeElement.textContent
);
const [ firstMenuItem ] = menuItems;
const firstMenuItemText = await firstMenuItem.evaluate(
( element ) => element.textContent
);
// Expect the first menu item to be focused.
expect( activeElementText ).toBeDefined();
expect( activeElementText ).toBe( firstMenuItemText );
// Arrow up to the last item.
await page.keyboard.press( 'ArrowUp' );
activeElementText = await page.evaluate(
() => document.activeElement.textContent
);
const [ lastMenuItem ] = menuItems.slice( -1 );
const lastMenuItemText = await lastMenuItem.evaluate(
( element ) => element.textContent
);
// Expect the last menu item to be focused.
expect( activeElementText ).toBeDefined();
expect( activeElementText ).toBe( lastMenuItemText );
// Arrow back down to the first item.
await page.keyboard.press( 'ArrowDown' );
activeElementText = await page.evaluate(
() => document.activeElement.textContent
);
// Expect the first menu item to be focused again.
expect( activeElementText ).toBeDefined();
expect( activeElementText ).toBe( firstMenuItemText );
} );
it( 'ignores arrow key navigation that is orthogonal to the orientation of the menu, but stays open', async () => {
await page.click( moreMenuButtonSelector );
const menuItems = await page.$$( menuItemsSelector );
// Catch any issues with the selector, which could cause a false positive test result.
expect( menuItems.length ).toBeGreaterThan( 0 );
let activeElementText = await page.evaluate(
() => document.activeElement.textContent
);
const [ firstMenuItem ] = menuItems;
const firstMenuItemText = await firstMenuItem.evaluate(
( element ) => element.textContent
);
// Expect the first menu item to be focused.
expect( activeElementText ).toBeDefined();
expect( activeElementText ).toBe( firstMenuItemText );
// Press left and right keys an arbitrary (but > 1) number of times.
await pressKeyTimes( 'ArrowLeft', 5 );
await pressKeyTimes( 'ArrowRight', 5 );
activeElementText = await page.evaluate(
() => document.activeElement.textContent
);
// Expect the first menu item to still be focused.
expect( activeElementText ).toBeDefined();
expect( activeElementText ).toBe( firstMenuItemText );
} );
} );