|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import { Alert } from './Alert'; |
| 4 | + |
| 5 | +describe('Alert', () => { |
| 6 | + // =========================== |
| 7 | + // Rendering |
| 8 | + // =========================== |
| 9 | + |
| 10 | + it('renders heading', () => { |
| 11 | + render(<Alert heading="Uw aanvraag wordt verwerkt" />); |
| 12 | + expect(screen.getByText('Uw aanvraag wordt verwerkt')).toBeInTheDocument(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('renders children content', () => { |
| 16 | + render( |
| 17 | + <Alert heading="Bericht"> |
| 18 | + <p>Dit kan enkele minuten duren.</p> |
| 19 | + </Alert> |
| 20 | + ); |
| 21 | + expect( |
| 22 | + screen.getByText('Dit kan enkele minuten duren.') |
| 23 | + ).toBeInTheDocument(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('renders without children', () => { |
| 27 | + render(<Alert heading="Alleen heading" />); |
| 28 | + expect(screen.getByText('Alleen heading')).toBeInTheDocument(); |
| 29 | + expect(screen.queryByRole('region')).not.toBeInTheDocument(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('renders as a <div> element', () => { |
| 33 | + render(<Alert heading="Test" />); |
| 34 | + const alert = screen.getByRole('alert'); |
| 35 | + expect(alert.tagName).toBe('DIV'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('has role="alert"', () => { |
| 39 | + render(<Alert heading="Test" />); |
| 40 | + expect(screen.getByRole('alert')).toBeInTheDocument(); |
| 41 | + }); |
| 42 | + |
| 43 | + // =========================== |
| 44 | + // Classes |
| 45 | + // =========================== |
| 46 | + |
| 47 | + it('always has base dsn-alert class', () => { |
| 48 | + render(<Alert heading="Test" />); |
| 49 | + expect(screen.getByRole('alert')).toHaveClass('dsn-alert'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('does not add variant modifier class for info variant (default)', () => { |
| 53 | + render(<Alert heading="Test" />); |
| 54 | + const el = screen.getByRole('alert'); |
| 55 | + expect(el).not.toHaveClass('dsn-alert--info'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('does not add variant modifier class for explicit info variant', () => { |
| 59 | + render(<Alert variant="info" heading="Test" />); |
| 60 | + const el = screen.getByRole('alert'); |
| 61 | + expect(el).not.toHaveClass('dsn-alert--info'); |
| 62 | + }); |
| 63 | + |
| 64 | + it.each(['positive', 'negative', 'warning'] as const)( |
| 65 | + 'applies variant modifier class for %s variant', |
| 66 | + (variant) => { |
| 67 | + render(<Alert variant={variant} heading="Test" />); |
| 68 | + expect(screen.getByRole('alert')).toHaveClass(`dsn-alert--${variant}`); |
| 69 | + } |
| 70 | + ); |
| 71 | + |
| 72 | + it('applies custom className', () => { |
| 73 | + render(<Alert heading="Test" className="custom-alert" />); |
| 74 | + const el = screen.getByRole('alert'); |
| 75 | + expect(el).toHaveClass('dsn-alert'); |
| 76 | + expect(el).toHaveClass('custom-alert'); |
| 77 | + }); |
| 78 | + |
| 79 | + // =========================== |
| 80 | + // Heading |
| 81 | + // =========================== |
| 82 | + |
| 83 | + it('renders heading as h2 by default', () => { |
| 84 | + render(<Alert heading="Standaard heading" />); |
| 85 | + expect( |
| 86 | + screen.getByRole('heading', { level: 2, name: 'Standaard heading' }) |
| 87 | + ).toBeInTheDocument(); |
| 88 | + }); |
| 89 | + |
| 90 | + it.each([1, 2, 3, 4, 5, 6] as const)( |
| 91 | + 'renders heading at level %i when headingLevel=%i', |
| 92 | + (level) => { |
| 93 | + render(<Alert heading="Heading" headingLevel={level} />); |
| 94 | + expect( |
| 95 | + screen.getByRole('heading', { level, name: 'Heading' }) |
| 96 | + ).toBeInTheDocument(); |
| 97 | + } |
| 98 | + ); |
| 99 | + |
| 100 | + it('applies dsn-alert__heading class to heading', () => { |
| 101 | + render(<Alert heading="Heading" />); |
| 102 | + const heading = screen.getByRole('heading'); |
| 103 | + expect(heading).toHaveClass('dsn-alert__heading'); |
| 104 | + }); |
| 105 | + |
| 106 | + // =========================== |
| 107 | + // Icon |
| 108 | + // =========================== |
| 109 | + |
| 110 | + it('renders preferred icon by default (info-circle for info)', () => { |
| 111 | + render(<Alert variant="info" heading="Info" />); |
| 112 | + const iconSpan = document.querySelector('.dsn-alert__icon'); |
| 113 | + expect(iconSpan).toBeInTheDocument(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('renders preferred icon for each variant', () => { |
| 117 | + const variants = ['info', 'positive', 'negative', 'warning'] as const; |
| 118 | + for (const variant of variants) { |
| 119 | + const { unmount } = render(<Alert variant={variant} heading="Test" />); |
| 120 | + expect(document.querySelector('.dsn-alert__icon')).toBeInTheDocument(); |
| 121 | + unmount(); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + it('renders no icon when iconStart={null}', () => { |
| 126 | + render(<Alert heading="Zonder icoon" iconStart={null} />); |
| 127 | + expect(document.querySelector('.dsn-alert__icon')).not.toBeInTheDocument(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('adds dsn-alert--no-icon class when iconStart={null}', () => { |
| 131 | + render(<Alert heading="Zonder icoon" iconStart={null} />); |
| 132 | + expect(screen.getByRole('alert')).toHaveClass('dsn-alert--no-icon'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('does not add dsn-alert--no-icon class when icon is shown', () => { |
| 136 | + render(<Alert heading="Met icoon" />); |
| 137 | + expect(screen.getByRole('alert')).not.toHaveClass('dsn-alert--no-icon'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('renders custom icon when iconStart is provided as ReactNode', () => { |
| 141 | + render( |
| 142 | + <Alert |
| 143 | + heading="Custom icoon" |
| 144 | + iconStart={<svg data-testid="custom-icon" />} |
| 145 | + /> |
| 146 | + ); |
| 147 | + expect(screen.getByTestId('custom-icon')).toBeInTheDocument(); |
| 148 | + const iconSpan = document.querySelector('.dsn-alert__icon'); |
| 149 | + expect(iconSpan).toBeInTheDocument(); |
| 150 | + }); |
| 151 | + |
| 152 | + // =========================== |
| 153 | + // Content |
| 154 | + // =========================== |
| 155 | + |
| 156 | + it('wraps children in dsn-alert__content div', () => { |
| 157 | + render( |
| 158 | + <Alert heading="Test"> |
| 159 | + <p>Inhoud</p> |
| 160 | + </Alert> |
| 161 | + ); |
| 162 | + const content = document.querySelector('.dsn-alert__content'); |
| 163 | + expect(content).toBeInTheDocument(); |
| 164 | + expect(content?.querySelector('p')).toBeInTheDocument(); |
| 165 | + }); |
| 166 | + |
| 167 | + it('does not render dsn-alert__content when no children', () => { |
| 168 | + render(<Alert heading="Test" />); |
| 169 | + expect( |
| 170 | + document.querySelector('.dsn-alert__content') |
| 171 | + ).not.toBeInTheDocument(); |
| 172 | + }); |
| 173 | + |
| 174 | + // =========================== |
| 175 | + // Ref + HTML attributes |
| 176 | + // =========================== |
| 177 | + |
| 178 | + it('forwards ref to the div element', () => { |
| 179 | + const ref = { current: null as HTMLDivElement | null }; |
| 180 | + render(<Alert ref={ref} heading="Test" />); |
| 181 | + expect(ref.current).toBeInstanceOf(HTMLDivElement); |
| 182 | + }); |
| 183 | + |
| 184 | + it('spreads additional HTML attributes', () => { |
| 185 | + render(<Alert heading="Test" id="alert-1" data-testid="my-alert" />); |
| 186 | + const el = screen.getByTestId('my-alert'); |
| 187 | + expect(el).toHaveAttribute('id', 'alert-1'); |
| 188 | + }); |
| 189 | + |
| 190 | + // =========================== |
| 191 | + // Content examples |
| 192 | + // =========================== |
| 193 | + |
| 194 | + it('renders a validation error list as children', () => { |
| 195 | + render( |
| 196 | + <Alert variant="negative" heading="Er zijn fouten opgetreden"> |
| 197 | + <ul> |
| 198 | + <li>Veld 1 is verplicht</li> |
| 199 | + <li>Veld 2 is ongeldig</li> |
| 200 | + </ul> |
| 201 | + </Alert> |
| 202 | + ); |
| 203 | + expect(screen.getByText('Er zijn fouten opgetreden')).toBeInTheDocument(); |
| 204 | + expect(screen.getByText('Veld 1 is verplicht')).toBeInTheDocument(); |
| 205 | + expect(screen.getByText('Veld 2 is ongeldig')).toBeInTheDocument(); |
| 206 | + }); |
| 207 | + |
| 208 | + it('renders success message correctly', () => { |
| 209 | + render( |
| 210 | + <Alert variant="positive" heading="Gelukt"> |
| 211 | + Uw gegevens zijn opgeslagen. |
| 212 | + </Alert> |
| 213 | + ); |
| 214 | + expect(screen.getByRole('heading', { name: 'Gelukt' })).toBeInTheDocument(); |
| 215 | + expect( |
| 216 | + screen.getByText('Uw gegevens zijn opgeslagen.') |
| 217 | + ).toBeInTheDocument(); |
| 218 | + }); |
| 219 | +}); |
0 commit comments