Add tests for Breadcrumbs (#53514)

This commit is contained in:
Ashley Harrison 2022-08-10 12:56:25 +01:00 committed by GitHub
parent e51c80f8dd
commit 26e81cad21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,43 @@
import { render, screen, within } from '@testing-library/react';
import React from 'react';
import { Breadcrumbs } from './Breadcrumbs';
import { Breadcrumb } from './types';
const mockBreadcrumbs: Breadcrumb[] = [
{ text: 'Home', href: '/home', icon: 'home-alt' },
{ text: 'First', href: '/first' },
{ text: 'Second', href: '/second' },
];
describe('Breadcrumbs', () => {
it('should render without error', () => {
expect(() => render(<Breadcrumbs breadcrumbs={[]} />)).not.toThrow();
});
it('should render a <nav> element', () => {
render(<Breadcrumbs breadcrumbs={[]} />);
expect(screen.getByRole('navigation')).toBeInTheDocument();
});
it('should render links for each breadcrumb except the last', () => {
render(<Breadcrumbs breadcrumbs={mockBreadcrumbs} />);
const nav = screen.getByRole('navigation');
const links = within(nav).getAllByRole('link');
expect(links.length).toEqual(2);
const homeLink = within(nav).getByRole('link', { name: 'Home' });
expect(homeLink).toBeInTheDocument();
expect(homeLink).toHaveAttribute('href', '/home');
const firstLink = within(nav).getByRole('link', { name: 'First' });
expect(firstLink).toBeInTheDocument();
expect(firstLink).toHaveAttribute('href', '/first');
});
it('should render the last breadcrumb as text and set the correct aria-current attribute', () => {
render(<Breadcrumbs breadcrumbs={mockBreadcrumbs} />);
const nav = screen.getByRole('navigation');
expect(within(nav).queryByRole('link', { name: 'Second' })).not.toBeInTheDocument();
expect(within(nav).getByText('Second')).toBeInTheDocument();
expect(within(nav).getByText('Second')).toHaveAttribute('aria-current', 'page');
});
});