Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced instances of user.type with user.keyboard #311

Merged
merged 2 commits into from
Jan 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 30 additions & 90 deletions src/use-dropdown-menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -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();
});
Expand Down Expand Up @@ -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');
});
Expand All @@ -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');
});
Expand All @@ -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');
});
Expand All @@ -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();

Expand All @@ -205,9 +193,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();

Expand All @@ -225,9 +211,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();

Expand All @@ -241,9 +225,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();

Expand All @@ -261,13 +243,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');
});
Expand All @@ -277,9 +255,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();

Expand All @@ -291,13 +267,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();
});
Expand Down Expand Up @@ -348,23 +320,17 @@ 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 () => {
const { user } = setup(<TestComponent />);

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();
});
Expand All @@ -374,27 +340,19 @@ 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 () => {
const { user } = setup(<TestComponent />);

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');
});
Expand All @@ -406,13 +364,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');
});
Expand All @@ -422,13 +376,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');
});
Expand All @@ -440,13 +390,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');
});
Expand All @@ -456,13 +402,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();
});
Expand All @@ -472,9 +414,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'));

Expand Down