mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Add tests for Breadcrumbs (#53514)
This commit is contained in:
parent
e51c80f8dd
commit
26e81cad21
43
public/app/core/components/Breadcrumbs/Breadcrumbs.test.tsx
Normal file
43
public/app/core/components/Breadcrumbs/Breadcrumbs.test.tsx
Normal 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');
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user