|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import { Details } from './Details'; |
| 5 | + |
| 6 | +describe('Details', () => { |
| 7 | + // =========================== |
| 8 | + // Rendering |
| 9 | + // =========================== |
| 10 | + |
| 11 | + it('renders summary label', () => { |
| 12 | + render(<Details summary="Meer informatie">Inhoud</Details>); |
| 13 | + expect(screen.getByText('Meer informatie')).toBeInTheDocument(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('renders children content', () => { |
| 17 | + render(<Details summary="Label">Aanvullende informatie</Details>); |
| 18 | + expect(screen.getByText('Aanvullende informatie')).toBeInTheDocument(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('renders as a <details> element', () => { |
| 22 | + const { container } = render(<Details summary="Label">Inhoud</Details>); |
| 23 | + expect(container.firstChild?.nodeName).toBe('DETAILS'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('renders a <summary> element inside details', () => { |
| 27 | + const { container } = render(<Details summary="Label">Inhoud</Details>); |
| 28 | + expect(container.querySelector('summary')).toBeInTheDocument(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('wraps children in dsn-details__content div', () => { |
| 32 | + render( |
| 33 | + <Details summary="Label"> |
| 34 | + <p>Inhoud</p> |
| 35 | + </Details> |
| 36 | + ); |
| 37 | + const content = document.querySelector('.dsn-details__content'); |
| 38 | + expect(content).toBeInTheDocument(); |
| 39 | + expect(content?.querySelector('p')).toBeInTheDocument(); |
| 40 | + }); |
| 41 | + |
| 42 | + // =========================== |
| 43 | + // Classes |
| 44 | + // =========================== |
| 45 | + |
| 46 | + it('always has base dsn-details class', () => { |
| 47 | + const { container } = render(<Details summary="Label">Inhoud</Details>); |
| 48 | + expect(container.firstChild).toHaveClass('dsn-details'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('applies custom className', () => { |
| 52 | + const { container } = render( |
| 53 | + <Details summary="Label" className="custom-details"> |
| 54 | + Inhoud |
| 55 | + </Details> |
| 56 | + ); |
| 57 | + expect(container.firstChild).toHaveClass('dsn-details'); |
| 58 | + expect(container.firstChild).toHaveClass('custom-details'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('applies dsn-details__summary class to summary element', () => { |
| 62 | + const { container } = render(<Details summary="Label">Inhoud</Details>); |
| 63 | + expect(container.querySelector('summary')).toHaveClass( |
| 64 | + 'dsn-details__summary' |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('applies dsn-details__summary-label class to label span', () => { |
| 69 | + const { container } = render(<Details summary="Label">Inhoud</Details>); |
| 70 | + expect( |
| 71 | + container.querySelector('.dsn-details__summary-label') |
| 72 | + ).toBeInTheDocument(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('applies dsn-details__icon class to chevron icon', () => { |
| 76 | + const { container } = render(<Details summary="Label">Inhoud</Details>); |
| 77 | + expect(container.querySelector('.dsn-details__icon')).toBeInTheDocument(); |
| 78 | + }); |
| 79 | + |
| 80 | + // =========================== |
| 81 | + // Open state |
| 82 | + // =========================== |
| 83 | + |
| 84 | + it('is closed by default', () => { |
| 85 | + const { container } = render(<Details summary="Label">Inhoud</Details>); |
| 86 | + expect(container.firstChild).not.toHaveAttribute('open'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('opens when defaultOpen is true', () => { |
| 90 | + const { container } = render( |
| 91 | + <Details summary="Label" defaultOpen> |
| 92 | + Inhoud |
| 93 | + </Details> |
| 94 | + ); |
| 95 | + expect(container.firstChild).toHaveAttribute('open'); |
| 96 | + }); |
| 97 | + |
| 98 | + // =========================== |
| 99 | + // onToggle callback |
| 100 | + // =========================== |
| 101 | + |
| 102 | + it('calls onToggle when toggled', async () => { |
| 103 | + const onToggle = vi.fn(); |
| 104 | + render( |
| 105 | + <Details summary="Label" onToggle={onToggle}> |
| 106 | + Inhoud |
| 107 | + </Details> |
| 108 | + ); |
| 109 | + const summary = screen.getByText('Label').closest('summary')!; |
| 110 | + await userEvent.click(summary); |
| 111 | + expect(onToggle).toHaveBeenCalledWith(true); |
| 112 | + }); |
| 113 | + |
| 114 | + it('calls onToggle with false when closed after being open', async () => { |
| 115 | + const onToggle = vi.fn(); |
| 116 | + render( |
| 117 | + <Details summary="Label" defaultOpen onToggle={onToggle}> |
| 118 | + Inhoud |
| 119 | + </Details> |
| 120 | + ); |
| 121 | + const summary = screen.getByText('Label').closest('summary')!; |
| 122 | + await userEvent.click(summary); |
| 123 | + expect(onToggle).toHaveBeenCalledWith(false); |
| 124 | + }); |
| 125 | + |
| 126 | + it('does not throw when onToggle is not provided', async () => { |
| 127 | + render(<Details summary="Label">Inhoud</Details>); |
| 128 | + const summary = screen.getByText('Label').closest('summary')!; |
| 129 | + await expect(userEvent.click(summary)).resolves.not.toThrow(); |
| 130 | + }); |
| 131 | + |
| 132 | + // =========================== |
| 133 | + // Ref + HTML attributes |
| 134 | + // =========================== |
| 135 | + |
| 136 | + it('forwards ref to the details element', () => { |
| 137 | + const ref = { current: null as HTMLDetailsElement | null }; |
| 138 | + render( |
| 139 | + <Details ref={ref} summary="Label"> |
| 140 | + Inhoud |
| 141 | + </Details> |
| 142 | + ); |
| 143 | + expect(ref.current).toBeInstanceOf(HTMLElement); |
| 144 | + expect(ref.current?.tagName).toBe('DETAILS'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('spreads additional HTML attributes', () => { |
| 148 | + render( |
| 149 | + <Details summary="Label" id="details-1" data-testid="my-details"> |
| 150 | + Inhoud |
| 151 | + </Details> |
| 152 | + ); |
| 153 | + const el = screen.getByTestId('my-details'); |
| 154 | + expect(el).toHaveAttribute('id', 'details-1'); |
| 155 | + }); |
| 156 | + |
| 157 | + // =========================== |
| 158 | + // Accessibility |
| 159 | + // =========================== |
| 160 | + |
| 161 | + it('chevron icon has aria-hidden', () => { |
| 162 | + const { container } = render(<Details summary="Label">Inhoud</Details>); |
| 163 | + const icon = container.querySelector('.dsn-details__icon'); |
| 164 | + expect(icon).toHaveAttribute('aria-hidden', 'true'); |
| 165 | + }); |
| 166 | + |
| 167 | + it('summary label text is the accessible name', () => { |
| 168 | + render(<Details summary="Meer informatie">Inhoud</Details>); |
| 169 | + // The summary element should be discoverable by its visible text |
| 170 | + expect( |
| 171 | + screen.getByText('Meer informatie').closest('summary') |
| 172 | + ).toBeInTheDocument(); |
| 173 | + }); |
| 174 | +}); |
0 commit comments