|
| 1 | +import { renderWithTheme } from '@ion/components/utils/test-utils'; |
| 2 | +import { screen } from '@testing-library/dom'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import { DropdownItemProps, IonDropdownItem } from './dropdownItem'; |
| 5 | + |
| 6 | +const sut = (props: DropdownItemProps) => { |
| 7 | + renderWithTheme(<IonDropdownItem {...props} />); |
| 8 | +}; |
| 9 | + |
| 10 | +describe('DropdownItem', () => { |
| 11 | + describe('Default', () => { |
| 12 | + it('should render a label', () => { |
| 13 | + const label = 'Item'; |
| 14 | + sut({ label }); |
| 15 | + expect(screen.getByText(label)).toBeInTheDocument(); |
| 16 | + }); |
| 17 | + it('should render an icon', () => { |
| 18 | + const icon = 'access'; |
| 19 | + sut({ label: 'Item', icon }); |
| 20 | + expect(screen.getByTestId('ion-icon-access')).toBeInTheDocument(); |
| 21 | + }); |
| 22 | + it('should emit a click', async () => { |
| 23 | + const onClick = jest.fn(); |
| 24 | + sut({ label: 'Item', onClick }); |
| 25 | + await userEvent.click(screen.getByText('Item')); |
| 26 | + expect(onClick).toHaveBeenCalled(); |
| 27 | + }); |
| 28 | + }); |
| 29 | + describe('Selected', () => { |
| 30 | + it('should render a selected item', () => { |
| 31 | + sut({ label: 'Item', selected: true }); |
| 32 | + expect(screen.getByTestId('ion-icon-check')).toBeInTheDocument(); |
| 33 | + }); |
| 34 | + it('should show a close icon when hovering', async () => { |
| 35 | + sut({ label: 'Item', selected: true }); |
| 36 | + expect(screen.getByTestId('ion-icon-check')).toBeInTheDocument(); |
| 37 | + await userEvent.hover(screen.getByText('Item')); |
| 38 | + expect(screen.getByTestId('ion-icon-close')).toBeInTheDocument(); |
| 39 | + }); |
| 40 | + it('should reset to check icon when unhovering', async () => { |
| 41 | + sut({ label: 'Item', selected: true }); |
| 42 | + expect(screen.getByTestId('ion-icon-check')).toBeInTheDocument(); |
| 43 | + await userEvent.hover(screen.getByText('Item')); |
| 44 | + expect(screen.getByTestId('ion-icon-close')).toBeInTheDocument(); |
| 45 | + await userEvent.unhover(screen.getByText('Item')); |
| 46 | + expect(screen.getByTestId('ion-icon-check')).toBeInTheDocument(); |
| 47 | + }); |
| 48 | + }); |
| 49 | + describe('Disabled', () => { |
| 50 | + it('should not emit a click', async () => { |
| 51 | + const onClick = jest.fn(); |
| 52 | + sut({ label: 'Item', disabled: true, onClick }); |
| 53 | + await userEvent.click(screen.getByText('Item')); |
| 54 | + expect(onClick).not.toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + it('should render check icon when selected', () => { |
| 57 | + sut({ label: 'Item', selected: true, disabled: true }); |
| 58 | + expect(screen.getByTestId('ion-icon-check')).toBeInTheDocument(); |
| 59 | + }); |
| 60 | + it('should not render close icon when hovering', async () => { |
| 61 | + sut({ label: 'Item', selected: true, disabled: true }); |
| 62 | + await userEvent.hover(screen.getByText('Item')); |
| 63 | + expect(screen.queryByTestId('ion-icon-close')).not.toBeInTheDocument(); |
| 64 | + }); |
| 65 | + }); |
| 66 | +}); |
0 commit comments