|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import { SkipLink } from './SkipLink'; |
| 4 | + |
| 5 | +describe('SkipLink', () => { |
| 6 | + it('renders children', () => { |
| 7 | + render(<SkipLink href="#main">Ga direct naar de hoofdinhoud</SkipLink>); |
| 8 | + expect( |
| 9 | + screen.getByRole('link', { name: 'Ga direct naar de hoofdinhoud' }) |
| 10 | + ).toBeInTheDocument(); |
| 11 | + }); |
| 12 | + |
| 13 | + it('renders as an anchor element', () => { |
| 14 | + render(<SkipLink href="#main">Skip</SkipLink>); |
| 15 | + expect(screen.getByRole('link').tagName).toBe('A'); |
| 16 | + }); |
| 17 | + |
| 18 | + it('sets href on the anchor', () => { |
| 19 | + render(<SkipLink href="#main-content">Skip</SkipLink>); |
| 20 | + expect(screen.getByRole('link')).toHaveAttribute('href', '#main-content'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('applies base dsn-skip-link class', () => { |
| 24 | + render(<SkipLink href="#main">Skip</SkipLink>); |
| 25 | + expect(screen.getByRole('link')).toHaveClass('dsn-skip-link'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('applies custom className', () => { |
| 29 | + render( |
| 30 | + <SkipLink href="#main" className="custom"> |
| 31 | + Skip |
| 32 | + </SkipLink> |
| 33 | + ); |
| 34 | + expect(screen.getByRole('link')).toHaveClass('dsn-skip-link', 'custom'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('uses default children text when no children provided', () => { |
| 38 | + render(<SkipLink href="#main" />); |
| 39 | + expect( |
| 40 | + screen.getByRole('link', { name: 'Ga direct naar de hoofdinhoud' }) |
| 41 | + ).toBeInTheDocument(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('renders custom children text', () => { |
| 45 | + render(<SkipLink href="#main">Sla navigatie over</SkipLink>); |
| 46 | + expect( |
| 47 | + screen.getByRole('link', { name: 'Sla navigatie over' }) |
| 48 | + ).toBeInTheDocument(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('forwards ref', () => { |
| 52 | + const ref = { current: null as HTMLAnchorElement | null }; |
| 53 | + render( |
| 54 | + <SkipLink ref={ref} href="#main"> |
| 55 | + Skip |
| 56 | + </SkipLink> |
| 57 | + ); |
| 58 | + expect(ref.current).toBeInstanceOf(HTMLAnchorElement); |
| 59 | + }); |
| 60 | + |
| 61 | + it('passes through additional anchor attributes', () => { |
| 62 | + render( |
| 63 | + <SkipLink href="#main" data-testid="skip-link"> |
| 64 | + Skip |
| 65 | + </SkipLink> |
| 66 | + ); |
| 67 | + expect(screen.getByTestId('skip-link')).toBeInTheDocument(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('passes lang attribute', () => { |
| 71 | + render( |
| 72 | + <SkipLink href="#main" lang="en"> |
| 73 | + Skip to main content |
| 74 | + </SkipLink> |
| 75 | + ); |
| 76 | + expect(screen.getByRole('link')).toHaveAttribute('lang', 'en'); |
| 77 | + }); |
| 78 | +}); |
0 commit comments