|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, fireEvent, waitFor } from '../../test-utils'; |
| 3 | +import { DropdownMenu, DropdownMenuAlignment } from './DropdownMenu'; |
| 4 | + |
| 5 | +describe('DropdownMenu', () => { |
| 6 | + const renderDropdown = () => { |
| 7 | + render( |
| 8 | + <DropdownMenu |
| 9 | + aria-label="More options" |
| 10 | + align={DropdownMenuAlignment.RIGHT} |
| 11 | + > |
| 12 | + <li role="menuitem">Item One</li> |
| 13 | + <li role="menuitem">Item Two</li> |
| 14 | + <li role="menuitem">Item Three</li> |
| 15 | + </DropdownMenu> |
| 16 | + ); |
| 17 | + }; |
| 18 | + |
| 19 | + it('should render the dropdown button', () => { |
| 20 | + renderDropdown(); |
| 21 | + |
| 22 | + const button = screen.getByRole('button', { name: 'More options' }); |
| 23 | + expect(button).toBeInTheDocument(); |
| 24 | + |
| 25 | + expect(screen.queryByRole('menu')).not.toBeInTheDocument(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should open dropdown and render menu items when button is clicked', () => { |
| 29 | + renderDropdown(); |
| 30 | + |
| 31 | + const button = screen.getByRole('button', { name: 'More options' }); |
| 32 | + |
| 33 | + fireEvent.click(button); |
| 34 | + |
| 35 | + expect(screen.getByRole('menu')).toBeInTheDocument(); |
| 36 | + |
| 37 | + expect(screen.getByText('Item One')).toBeInTheDocument(); |
| 38 | + expect(screen.getByText('Item Two')).toBeInTheDocument(); |
| 39 | + expect(screen.getByText('Item Three')).toBeInTheDocument(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should close the menu after selecting an item', async () => { |
| 43 | + renderDropdown(); |
| 44 | + |
| 45 | + const button = screen.getByRole('button', { name: 'More options' }); |
| 46 | + fireEvent.click(button); |
| 47 | + |
| 48 | + const item = screen.getByText('Item One'); |
| 49 | + fireEvent.mouseUp(item); |
| 50 | + |
| 51 | + await waitFor(() => { |
| 52 | + expect(screen.queryByRole('menu')).not.toBeInTheDocument(); |
| 53 | + }); |
| 54 | + }); |
| 55 | +}); |
0 commit comments