|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import Link from 'next/link'; |
| 3 | +import { Breadcrumbs } from './index'; |
| 4 | +import '@testing-library/jest-dom'; |
| 5 | + |
| 6 | +describe('Breadcrumb navigation', () => { |
| 7 | + const props = { |
| 8 | + label: 'Kruimelpad', |
| 9 | + links: [ |
| 10 | + { |
| 11 | + href: 'https://www.utrecht.nl/', |
| 12 | + label: 'Home', |
| 13 | + current: false, |
| 14 | + rel: 'home', |
| 15 | + }, |
| 16 | + { |
| 17 | + href: '/', |
| 18 | + label: 'Online loket', |
| 19 | + current: true, |
| 20 | + }, |
| 21 | + ], |
| 22 | + backLink: { |
| 23 | + href: '/', |
| 24 | + label: 'Online loket', |
| 25 | + current: false, |
| 26 | + }, |
| 27 | + Link: Link, |
| 28 | + }; |
| 29 | + |
| 30 | + it('renders a navigation landmark', () => { |
| 31 | + render(<Breadcrumbs {...props} />); |
| 32 | + |
| 33 | + const navigation = screen.getByRole('navigation', { name: 'Kruimelpad' }); |
| 34 | + |
| 35 | + expect(navigation).toBeInTheDocument(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('renders links', () => { |
| 39 | + render(<Breadcrumbs {...props} />); |
| 40 | + |
| 41 | + const links = screen.getAllByRole('link'); |
| 42 | + |
| 43 | + expect(links.length).toBe(props.links.length); |
| 44 | + }); |
| 45 | + |
| 46 | + it('renders a link', () => { |
| 47 | + render(<Breadcrumbs {...props} />); |
| 48 | + |
| 49 | + const link = screen.getByRole('link', { name: 'Home' }); |
| 50 | + |
| 51 | + expect(link).toBeInTheDocument(); |
| 52 | + expect(link).toHaveAttribute('href', 'https://www.utrecht.nl/'); |
| 53 | + expect(link).toHaveAttribute('rel', 'home'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('renders a current link', () => { |
| 57 | + render(<Breadcrumbs {...props} />); |
| 58 | + |
| 59 | + const link = screen.getByRole('link', { name: 'Online loket', current: 'page' }); |
| 60 | + |
| 61 | + expect(link).toBeInTheDocument(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('renders no separator for one link', () => { |
| 65 | + const { container } = render(<Breadcrumbs {...props} links={props.links.slice(0, 1)} />); |
| 66 | + |
| 67 | + const separator = container.querySelector('.utrecht-breadcrumb-nav__separator'); |
| 68 | + |
| 69 | + expect(separator).not.toBeInTheDocument(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('renders separators between items', () => { |
| 73 | + const { container } = render(<Breadcrumbs {...props} />); |
| 74 | + |
| 75 | + const separators = container.querySelectorAll( |
| 76 | + '.utrecht-breadcrumb-nav__item + .utrecht-breadcrumb-nav__separator + .utrecht-breadcrumb-nav__item', |
| 77 | + ); |
| 78 | + |
| 79 | + expect(separators.length).toBe(props.links.length - 1); |
| 80 | + }); |
| 81 | + |
| 82 | + it('renders a custom Link component', () => { |
| 83 | + const { container } = render( |
| 84 | + <Breadcrumbs {...props} Link={({ props }) => <a {...props} className="custom-link" />} />, |
| 85 | + ); |
| 86 | + |
| 87 | + const links = container.querySelectorAll('.custom-link'); |
| 88 | + |
| 89 | + expect(links.length).toBe(props.links.length); |
| 90 | + }); |
| 91 | + |
| 92 | + // TODO: Test small screen breadcrumb |
| 93 | +}); |
0 commit comments