|
| 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 aria-label="More options" align={DropdownMenuAlignment.RIGHT}> |
| 9 | + <li role="menuitem">Item One</li> |
| 10 | + <li role="menuitem">Item Two</li> |
| 11 | + <li role="menuitem">Item Three</li> |
| 12 | + </DropdownMenu> |
| 13 | + ); |
| 14 | + }; |
| 15 | + |
| 16 | + it('should render the dropdown button', () => { |
| 17 | + renderDropdown(); |
| 18 | + |
| 19 | + const button = screen.getByRole('button', { name: 'More options' }); |
| 20 | + expect(button).toBeInTheDocument(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should open the dropdown menu when button is clicked', () => { |
| 24 | + renderDropdown(); |
| 25 | + |
| 26 | + const button = screen.getByRole('button', { name: 'More options' }); |
| 27 | + |
| 28 | + fireEvent.click(button); |
| 29 | + |
| 30 | + const menu = screen.getByRole('menu'); |
| 31 | + expect(menu).toBeInTheDocument(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should render menu items when opened', () => { |
| 35 | + renderDropdown(); |
| 36 | + |
| 37 | + const button = screen.getByRole('button', { name: 'More options' }); |
| 38 | + fireEvent.click(button); |
| 39 | + |
| 40 | + expect(screen.getByText('Item One')).toBeInTheDocument(); |
| 41 | + expect(screen.getByText('Item Two')).toBeInTheDocument(); |
| 42 | + expect(screen.getByText('Item Three')).toBeInTheDocument(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should close the menu after selecting an item', async () => { |
| 46 | + renderDropdown(); |
| 47 | + |
| 48 | + const button = screen.getByRole('button', { name: 'More options' }); |
| 49 | + fireEvent.click(button); |
| 50 | + |
| 51 | + const item = screen.getByText('Item One'); |
| 52 | + fireEvent.mouseUp(item); |
| 53 | + |
| 54 | + await waitFor(() => { |
| 55 | + expect(screen.queryByRole('menu')).not.toBeInTheDocument(); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should support custom alignment', () => { |
| 60 | + render( |
| 61 | + <DropdownMenu |
| 62 | + aria-label="Aligned dropdown" |
| 63 | + align={DropdownMenuAlignment.LEFT} |
| 64 | + > |
| 65 | + <li role="menuitem">Left Item</li> |
| 66 | + </DropdownMenu> |
| 67 | + ); |
| 68 | + |
| 69 | + const button = screen.getByRole('button', { name: 'Aligned dropdown' }); |
| 70 | + |
| 71 | + fireEvent.click(button); |
| 72 | + |
| 73 | + const menu = screen.getByRole('menu'); |
| 74 | + expect(menu).toBeInTheDocument(); |
| 75 | + }); |
| 76 | +}); |
0 commit comments