From ca402da1ad6d7f8cf2048f59a9364557f071db14 Mon Sep 17 00:00:00 2001 From: Cory Harper Date: Mon, 30 Jan 2023 15:55:05 -0600 Subject: [PATCH] replaced instances of `user.type` with `user.keyboard` --- src/use-dropdown-menu.test.tsx | 120 +++++++++------------------------ 1 file changed, 30 insertions(+), 90 deletions(-) diff --git a/src/use-dropdown-menu.test.tsx b/src/use-dropdown-menu.test.tsx index 182fac1..ce9718a 100644 --- a/src/use-dropdown-menu.test.tsx +++ b/src/use-dropdown-menu.test.tsx @@ -78,9 +78,7 @@ it('Moves the focus to the first menu item after pressing enter while focused on expect(screen.getByText('Primary')).toHaveFocus(); - await user.type(screen.getByText('Primary'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); expect(screen.getByText('1 Item')).toHaveFocus(); }); @@ -92,9 +90,7 @@ it('Moves the focus to the first menu item after pressing space while focused on expect(screen.getByText('Primary')).toHaveFocus(); - await user.type(screen.getByText('Primary'), '{ }', { - skipClick: true, - }); + await user.keyboard('{ }'); expect(screen.getByText('1 Item')).toHaveFocus(); }); @@ -140,9 +136,7 @@ it('Sets isOpen to true after pressing enter while focused on the menu button', expect(screen.getByText('Primary')).toHaveFocus(); - await user.type(screen.getByText('Primary'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); expect(screen.getByTestId('is-open-indicator')).toHaveTextContent('true'); }); @@ -152,9 +146,7 @@ it('Sets isOpen to false after pressing escape while focused on the menu button' await user.click(screen.getByText('Primary')); - await user.type(screen.getByText('Primary'), '{Escape}', { - skipClick: true, - }); + await user.keyboard('{Escape}'); expect(screen.getByTestId('is-open-indicator')).toHaveTextContent('false'); }); @@ -168,9 +160,7 @@ it('Sets isOpen to true after pressing space while focused on the menu button', expect(screen.getByText('Primary')).toHaveFocus(); - await user.type(screen.getByText('Primary'), '{ }', { - skipClick: true, - }); + await user.keyboard('{ }'); expect(screen.getByTestId('is-open-indicator')).toHaveTextContent('true'); }); @@ -189,9 +179,7 @@ it('Moves the focus to the next element in the menu after pressing the down arro await user.tab(); - await user.type(screen.getByText('Primary'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); expect(screen.getByText('1 Item')).toHaveFocus(); @@ -212,9 +200,7 @@ it('Moves the focus to the previous element in the menu after pressing the up ar await user.tab(); - await user.type(screen.getByText('Primary'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); expect(screen.getByText('1 Item')).toHaveFocus(); @@ -246,9 +232,7 @@ it('Wraps the focus to the last element when pressing the up arrow at the beginn await user.tab(); - await user.type(screen.getByText('Primary'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); expect(screen.getByText('1 Item')).toHaveFocus(); @@ -269,9 +253,7 @@ it('Wraps the focus to the first element when pressing the down arrow at the end await user.tab(); - await user.type(screen.getByText('Primary'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); expect(screen.getByText('1 Item')).toHaveFocus(); @@ -303,13 +285,9 @@ it('Sets isOpen to false after pressing escape while focused on a menu item', as await user.tab(); - await user.type(screen.getByText('Primary'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); - await user.type(screen.getByText('1 Item'), '{Escape}', { - skipClick: true, - }); + await user.keyboard('{Escape}'); expect(screen.getByTestId('is-open-indicator')).toHaveTextContent('false'); }); @@ -319,9 +297,7 @@ it('Sets isOpen to false after pressing tab while focused on a menu item', async await user.tab(); - await user.type(screen.getByText('Primary'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); await user.tab(); @@ -333,13 +309,9 @@ it('Moves the focus to the menu button after pressing escape while focused on a await user.tab(); - await user.type(screen.getByText('Primary'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); - await user.type(screen.getByText('1 Item'), '{Escape}', { - skipClick: true, - }); + await user.keyboard('{Escape}'); expect(screen.getByText('Primary')).toHaveFocus(); }); @@ -431,9 +403,7 @@ it('Ignores keys that buttons don’t need to handle', async () => { await user.tab(); - await user.type(screen.getByText('Primary'), 'Z', { - skipClick: true, - }); + await user.keyboard('Z'); }); it('Ignores keys that items don’t need to handle', async () => { @@ -441,13 +411,9 @@ it('Ignores keys that items don’t need to handle', async () => { await user.tab(); - await user.type(screen.getByText('Primary'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); - await user.type(screen.getByText('1 Item'), 'Z', { - skipClick: true, - }); + await user.keyboard('Z'); expect(screen.getByText('1 Item')).toHaveFocus(); }); @@ -457,13 +423,9 @@ it('Doesn’t crash when enter press occurs on a menu item', async () => { await user.tab(); - await user.type(screen.getByText('Primary'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); - user.type(screen.getByText('1 Item'), '{Enter}', { - skipClick: true, - }); + user.keyboard('{Enter}'); }); it('Closes the menu after pressing enter on a menu item with a click handler', async () => { @@ -471,13 +433,9 @@ it('Closes the menu after pressing enter on a menu item with a click handler', a await user.tab(); - await user.type(screen.getByText('Primary'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); - await user.type(screen.getByText('1 Item'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); expect(screen.getByTestId('is-open-indicator')).toHaveTextContent('false'); }); @@ -489,13 +447,9 @@ it('Activates the click handler of a menu item after pressing enter while focuse await user.tab(); - await user.type(screen.getByText('Primary'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); - await user.type(screen.getByText('1 Item'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); expect(console.log).toHaveBeenCalledWith('Item one clicked'); }); @@ -505,13 +459,9 @@ it('Closes the menu after pressing space on a menu item with a click handler', a await user.tab(); - await user.type(screen.getByText('Primary'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); - await user.type(screen.getByText('1 Item'), '{ }', { - skipClick: true, - }); + await user.keyboard('{ }'); expect(screen.getByTestId('is-open-indicator')).toHaveTextContent('false'); }); @@ -523,13 +473,9 @@ it('Activates the click handler of a menu item after pressing space while focuse await user.tab(); - await user.type(screen.getByText('Primary'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); - await user.type(screen.getByText('1 Item'), '{ }', { - skipClick: true, - }); + await user.keyboard('{ }'); expect(console.log).toHaveBeenCalledWith('Item one clicked'); }); @@ -539,13 +485,9 @@ it('Moves the focus to the menu item with a label that starts with the correspon await user.tab(); - await user.type(screen.getByText('Primary'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); - await user.type(screen.getByText('1 Item'), '3', { - skipClick: true, - }); + await user.keyboard('3'); expect(screen.getByText('3 Item')).toHaveFocus(); }); @@ -555,9 +497,7 @@ it('Moves the focus to the provided menu item when `moveFocus` is called', async await user.tab(); - await user.type(screen.getByText('Primary'), '{Enter}', { - skipClick: true, - }); + await user.keyboard('{Enter}'); await user.click(screen.getByTestId('focus-third-item'));