|
| 1 | +import { render, fireEvent } from '@testing-library/react' |
| 2 | +import { AccordionItem, Accordion } from '.' |
| 3 | +import '@testing-library/jest-dom/extend-expect' |
| 4 | + |
| 5 | +describe('AccordionItem Component', () => { |
| 6 | + it('renders the AccordionItem with the correct title', () => { |
| 7 | + const { getByText } = render( |
| 8 | + <AccordionItem title="Test Title" content="Test Content" />, |
| 9 | + ) |
| 10 | + expect(getByText('Test Title')).toBeInTheDocument() |
| 11 | + }) |
| 12 | + |
| 13 | + it('does not show content initially', () => { |
| 14 | + const { getByText } = render( |
| 15 | + <AccordionItem title="Title" content="Hidden Content" />, |
| 16 | + ) |
| 17 | + const contentContainer = getByText('Hidden Content').parentElement |
| 18 | + expect(contentContainer).toHaveClass('max-h-0', 'opacity-0') |
| 19 | + }) |
| 20 | + |
| 21 | + it('toggles content visibility when button is clicked', () => { |
| 22 | + const { getByText, getByRole } = render( |
| 23 | + <AccordionItem title="Toggle Title" content="Toggle Content" />, |
| 24 | + ) |
| 25 | + const button = getByRole('button') |
| 26 | + const contentContainer = getByText('Toggle Content').parentElement |
| 27 | + |
| 28 | + // Content initially hidden |
| 29 | + expect(button).toHaveAttribute('aria-expanded', 'false') |
| 30 | + expect(contentContainer).toHaveClass('max-h-0', 'opacity-0') |
| 31 | + |
| 32 | + // Click to expand |
| 33 | + fireEvent.click(button) |
| 34 | + expect(button).toHaveAttribute('aria-expanded', 'true') |
| 35 | + expect(contentContainer).toHaveClass('max-h-40', 'opacity-100') |
| 36 | + |
| 37 | + // Click to collapse |
| 38 | + fireEvent.click(button) |
| 39 | + expect(button).toHaveAttribute('aria-expanded', 'false') |
| 40 | + expect(contentContainer).toHaveClass('max-h-0', 'opacity-0') |
| 41 | + }) |
| 42 | +}) |
| 43 | + |
| 44 | +describe('Accordion Component', () => { |
| 45 | + it('renders multiple AccordionItem components', () => { |
| 46 | + const items = [ |
| 47 | + { title: 'Item 1', content: 'Content 1' }, |
| 48 | + { title: 'Item 2', content: 'Content 2' }, |
| 49 | + ] |
| 50 | + const { getByText } = render(<Accordion items={items} />) |
| 51 | + |
| 52 | + expect(getByText('Item 1')).toBeInTheDocument() |
| 53 | + expect(getByText('Item 2')).toBeInTheDocument() |
| 54 | + expect(getByText('Content 1').parentElement).toHaveClass('max-h-0') |
| 55 | + expect(getByText('Content 2').parentElement).toHaveClass('max-h-0') |
| 56 | + }) |
| 57 | +}) |
0 commit comments