|
1 | 1 | // Imports |
2 | 2 | import React, { useState } from 'react'; |
3 | | -import useDropdownMenu from './use-dropdown-menu'; |
| 3 | +import useDropdownMenu, { DropdownMenuOptions } from './use-dropdown-menu'; |
4 | 4 | import { fireEvent, render, screen } from '@testing-library/react'; |
5 | 5 | import userEvent from '@testing-library/user-event'; |
6 | 6 |
|
7 | 7 | // A mock component for testing the Hook |
8 | | -const TestComponent: React.FC = () => { |
| 8 | +interface Props { |
| 9 | + options?: DropdownMenuOptions; |
| 10 | +} |
| 11 | + |
| 12 | +const TestComponent: React.FC<Props> = ({ options }) => { |
9 | 13 | const [itemCount, setItemCount] = useState(4); |
10 | | - const { buttonProps, itemProps, isOpen, setIsOpen } = useDropdownMenu(itemCount); |
| 14 | + const { buttonProps, itemProps, isOpen, setIsOpen } = useDropdownMenu(itemCount, options); |
11 | 15 |
|
12 | 16 | const clickHandlers: (() => void)[] = [(): void => console.log('Item one clicked'), (): void => setIsOpen(false)]; |
13 | 17 |
|
@@ -79,20 +83,28 @@ it('Moves the focus to the first menu item after pressing space while focused on |
79 | 83 | expect(screen.getByText('1 Item')).toHaveFocus(); |
80 | 84 | }); |
81 | 85 |
|
82 | | -it('Moves the focus to the first menu item after clicking the menu to open it, then pressing tab while focused on the menu button', () => { |
| 86 | +it('Moves the focus to the first menu item after clicking the menu to open it', () => { |
83 | 87 | render(<TestComponent />); |
84 | 88 |
|
85 | 89 | userEvent.click(screen.getByText('Primary')); |
86 | 90 |
|
| 91 | + expect(screen.getByText('1 Item')).toHaveFocus(); |
| 92 | +}); |
| 93 | + |
| 94 | +it('Moves the focus to the first menu item after clicking the menu to open it, then pressing tab while focused on the menu button, if `disableFocusFirstItemOnClick` is specified', () => { |
| 95 | + render(<TestComponent options={{ disableFocusFirstItemOnClick: true }} />); |
| 96 | + |
| 97 | + userEvent.click(screen.getByText('Primary')); |
| 98 | + |
87 | 99 | expect(screen.getByText('Primary')).toHaveFocus(); |
88 | 100 |
|
89 | 101 | userEvent.tab(); |
90 | 102 |
|
91 | 103 | expect(screen.getByText('1 Item')).toHaveFocus(); |
92 | 104 | }); |
93 | 105 |
|
94 | | -it('Moves the focus to the first menu item after clicking the menu to open it, then pressing arrow down while focused on the menu button', () => { |
95 | | - render(<TestComponent />); |
| 106 | +it('Moves the focus to the first menu item after clicking the menu to open it, then pressing arrow down while focused on the menu button, if `disableFocusFirstItemOnClick` is specified', () => { |
| 107 | + render(<TestComponent options={{ disableFocusFirstItemOnClick: true }} />); |
96 | 108 |
|
97 | 109 | userEvent.click(screen.getByText('Primary')); |
98 | 110 |
|
|
0 commit comments