|
| 1 | +import { fireEvent, render } from '@testing-library/react' |
| 2 | +import { describe, expect, it, vi } from 'vitest' |
| 3 | +import Dropdown from './Dropdown' |
| 4 | +import styles from './Dropdown.module.css' |
| 5 | + |
| 6 | +describe('Dropdown Component', () => { |
| 7 | + it('renders dropdown with its children', () => { |
| 8 | + const { container: { children: [ div ] }, queryByText } = render( |
| 9 | + <Dropdown><div>Child 1</div><div>Child 2</div></Dropdown> |
| 10 | + ) |
| 11 | + expect(div?.classList).not.toContain(styles.open) |
| 12 | + expect(queryByText('Child 1')).toBeDefined() |
| 13 | + expect(queryByText('Child 2')).toBeDefined() |
| 14 | + expect(div?.classList).not.toContain(styles.dropdownLeft) |
| 15 | + }) |
| 16 | + |
| 17 | + it('toggles dropdown content on button click', () => { |
| 18 | + const { container: { children: [ div ] }, getByRole } = render( |
| 19 | + <Dropdown label='go'><div>Child 1</div><div>Child 2</div></Dropdown> |
| 20 | + ) |
| 21 | + |
| 22 | + const dropdownButton = getByRole('button') |
| 23 | + fireEvent.click(dropdownButton) |
| 24 | + |
| 25 | + // Check if dropdown content appears |
| 26 | + expect(div?.classList).toContain(styles.open) |
| 27 | + |
| 28 | + // Click again to close |
| 29 | + fireEvent.click(dropdownButton) |
| 30 | + expect(div?.classList).not.toContain(styles.open) |
| 31 | + }) |
| 32 | + |
| 33 | + it('closes dropdown when clicking outside', () => { |
| 34 | + const { container: { children: [ div ] }, getByRole } = render( |
| 35 | + <Dropdown><div>Child 1</div><div>Child 2</div></Dropdown> |
| 36 | + ) |
| 37 | + |
| 38 | + const dropdownButton = getByRole('button') |
| 39 | + fireEvent.click(dropdownButton) // open dropdown |
| 40 | + expect(div?.classList).toContain(styles.open) |
| 41 | + |
| 42 | + // Simulate a click outside |
| 43 | + fireEvent.mouseDown(document) |
| 44 | + expect(div?.classList).not.toContain(styles.open) |
| 45 | + }) |
| 46 | + |
| 47 | + it('does not close dropdown when clicking inside', () => { |
| 48 | + const { container: { children: [ div ] }, getByRole, getByText } = render( |
| 49 | + <Dropdown><div>Child 1</div><div>Child 2</div></Dropdown> |
| 50 | + ) |
| 51 | + |
| 52 | + const dropdownButton = getByRole('button') |
| 53 | + fireEvent.click(dropdownButton) // open dropdown |
| 54 | + expect(div?.classList).toContain(styles.open) |
| 55 | + |
| 56 | + const dropdownContent = getByText('Child 1').parentElement |
| 57 | + if (!dropdownContent) throw new Error('Dropdown content not found') |
| 58 | + fireEvent.mouseDown(dropdownContent) |
| 59 | + expect(div?.classList).toContain(styles.open) |
| 60 | + }) |
| 61 | + |
| 62 | + it('closes dropdown on escape key press', () => { |
| 63 | + const { container: { children: [ div ] }, getByRole } = render( |
| 64 | + <Dropdown><div>Child 1</div><div>Child 2</div></Dropdown> |
| 65 | + ) |
| 66 | + |
| 67 | + const dropdownButton = getByRole('button') |
| 68 | + fireEvent.click(dropdownButton) // open dropdown |
| 69 | + expect(div?.classList).toContain(styles.open) |
| 70 | + |
| 71 | + // Press escape key |
| 72 | + fireEvent.keyDown(document, { key: 'Escape', code: 'Escape' }) |
| 73 | + expect(div?.classList).not.toContain(styles.open) |
| 74 | + }) |
| 75 | + |
| 76 | + it('adds dropdownLeft class when align is left', () => { |
| 77 | + const { container: { children: [ div ] } } = render( |
| 78 | + <Dropdown align='left'><div>Child 1</div><div>Child 2</div></Dropdown> |
| 79 | + ) |
| 80 | + expect(div?.classList).toContain(styles.dropdownLeft) |
| 81 | + }) |
| 82 | + |
| 83 | + it('cleans up event listeners on unmount', () => { |
| 84 | + const { unmount } = render(<Dropdown><div>Dropdown Content</div></Dropdown>) |
| 85 | + |
| 86 | + // Mock function to replace the actual document event listener |
| 87 | + const mockRemoveEventListener = vi.spyOn(document, 'removeEventListener') |
| 88 | + |
| 89 | + // Unmount the component |
| 90 | + unmount() |
| 91 | + |
| 92 | + // Check if the event listener was removed |
| 93 | + expect(mockRemoveEventListener).toHaveBeenCalledWith('click', expect.any(Function)) |
| 94 | + expect(mockRemoveEventListener).toHaveBeenCalledWith('keydown', expect.any(Function)) |
| 95 | + expect(mockRemoveEventListener).toHaveBeenCalledWith('mousedown', expect.any(Function)) |
| 96 | + }) |
| 97 | +}) |
0 commit comments