Skip to content

Commit

Permalink
test: switch to user-event for clicks in OptionsMenu test
Browse files Browse the repository at this point in the history
  • Loading branch information
anastasialanz committed Sep 17, 2024
1 parent e4d12af commit ff1b1e5
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions packages/react/src/components/OptionsMenu/OptionsMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,23 @@ test('should support menuRef prop', () => {
expect(menuRef.current).toEqual(screen.getByRole('menu'));
});

test('should toggle menu on trigger clicks', () => {
test('should toggle menu on trigger clicks', async () => {
const user = userEvent.setup();
render(
<OptionsMenu trigger={trigger}>
<li className="foo">option 1</li>
</OptionsMenu>
);

fireEvent.click(screen.getByRole('button'));
expect(screen.getByRole('button')).toHaveAttribute('aria-expanded', 'true');
const button = screen.getByRole('button');

await user.click(button);
expect(button).toHaveAttribute('aria-expanded', 'true');
expect(screen.getByRole('menu')).toHaveAttribute('aria-expanded', 'true');

fireEvent.click(screen.getByRole('button'));
expect(screen.getByRole('button')).toHaveAttribute('aria-expanded', 'false');
await user.click(button);

expect(button).toHaveAttribute('aria-expanded', 'false');
expect(screen.getByRole('menu')).toHaveAttribute('aria-expanded', 'false');
});

Expand All @@ -114,8 +118,10 @@ test('should click trigger with down key on trigger', () => {
</OptionsMenu>
);

fireEvent.keyDown(screen.getByRole('button'), { keyCode: 40 });
expect(screen.getByRole('button')).toHaveAttribute('aria-expanded', 'true');
const button = screen.getByRole('button');

fireEvent.keyDown(button, { keyCode: 40 });
expect(button).toHaveAttribute('aria-expanded', 'true');
});

test('should focus trigger on close', async () => {
Expand All @@ -134,7 +140,8 @@ test('should focus trigger on close', async () => {
expect(button).toHaveFocus();
});

test('should call onClose when closed', () => {
test('should call onClose when closed', async () => {
const user = userEvent.setup();
const onClose = jest.fn();

render(
Expand All @@ -145,7 +152,7 @@ test('should call onClose when closed', () => {

const option = screen.getByRole('menuitem');

fireEvent.click(option);
await user.click(option);

expect(onClose).toBeCalled();
});
Expand Down

0 comments on commit ff1b1e5

Please sign in to comment.