@@ -6,6 +6,8 @@ const { keyboard } = page;
66
77// Helper functions used in multiple tests
88const currentFocusID = ( ) => page . evaluate ( ( ) => document . activeElement . id ) ;
9+ const menuOpen = ( ) => page . waitForSelector ( '#menu' , { visible : true } ) ;
10+ const menuClosed = ( ) => page . waitForSelector ( '#menu' , { hidden : true } ) ;
911
1012// Tests
1113beforeEach ( async ( ) => {
@@ -21,31 +23,46 @@ it('has the correct page title', async () => {
2123it ( 'focuses on the first menu item when the enter key is pressed' , async ( ) => {
2224 await page . focus ( '#menu-button' ) ;
2325 await keyboard . down ( 'Enter' ) ;
26+ await menuOpen ( ) ;
2427
2528 expect ( await currentFocusID ( ) ) . toBe ( 'menu-item-1' ) ;
2629} ) ;
2730
2831it ( 'focuses on the menu button after pressing escape' , async ( ) => {
29- await page . focus ( '#menu-button' ) ;
30- await keyboard . down ( 'Enter' ) ;
32+ await page . click ( '#menu-button' ) ;
33+ await menuOpen ( ) ;
3134 await keyboard . down ( 'Escape' ) ;
35+ await menuClosed ( ) ;
3236
3337 expect ( await currentFocusID ( ) ) . toBe ( 'menu-button' ) ;
3438} ) ;
3539
3640it ( 'focuses on the next item in the tab order after pressing tab' , async ( ) => {
37- await page . focus ( '#menu-button' ) ;
38- await keyboard . down ( 'Enter' ) ;
41+ await page . click ( '#menu-button' ) ;
42+ await menuOpen ( ) ;
3943 await keyboard . down ( 'Tab' ) ;
44+ await menuClosed ( ) ;
4045
4146 expect ( await currentFocusID ( ) ) . toBe ( 'first-footer-link' ) ;
4247} ) ;
4348
4449it ( 'focuses on the previous item in the tab order after pressing shift-tab' , async ( ) => {
45- await page . focus ( '#menu-button' ) ;
46- await keyboard . down ( 'Enter' ) ;
50+ await page . click ( '#menu-button' ) ;
51+ await menuOpen ( ) ;
4752 await keyboard . down ( 'Shift' ) ;
4853 await keyboard . down ( 'Tab' ) ;
54+ await menuClosed ( ) ;
4955
5056 expect ( await currentFocusID ( ) ) . toBe ( 'menu-button' ) ;
5157} ) ;
58+
59+ it ( 'closes the menu if you click outside of it' , async ( ) => {
60+ await page . click ( '#menu-button' ) ;
61+ await menuOpen ( ) ;
62+ await page . click ( 'body' ) ;
63+ await menuClosed ( ) ;
64+
65+ // menuClosed() will time out if it doesn't actually close
66+
67+ expect ( true ) . toBe ( true ) ;
68+ } ) ;
0 commit comments