/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';
/**
* Internal dependencies
*/
import { Breadcrumbs } from '..';
jest.mock( '@wordpress/route', () => ( {
Link: ( { to, children }: { to: string; children: React.ReactNode } ) => (
{ children }
),
} ) );
describe( 'Breadcrumbs', () => {
describe( 'validation', () => {
it( 'should throw when a preceding item is missing `to`', () => {
expect( () =>
render(
)
).toThrow( /item "Home" is missing a `to` prop/ );
expect( console ).toHaveErrored();
} );
it( 'should throw for the first preceding item missing `to`', () => {
expect( () =>
render(
)
).toThrow( /item "Home" is missing a `to` prop/ );
expect( console ).toHaveErrored();
} );
it( 'should not throw when all preceding items have `to`', () => {
expect( () =>
render(
)
).not.toThrow();
} );
it( 'should not throw when there is only one item without `to`', () => {
expect( () =>
render( )
).not.toThrow();
} );
it( 'should not throw when items is empty', () => {
expect( () =>
render( )
).not.toThrow();
} );
} );
describe( 'rendering', () => {
it( 'should render nothing when items is empty', () => {
const { container } = render( );
expect( container ).toBeEmptyDOMElement();
} );
it( 'should render the last item as an h1 when it has no `to`', () => {
render(
);
expect(
screen.getByRole( 'heading', { level: 1 } )
).toHaveTextContent( 'Current Page' );
} );
it( 'should render the last item as a link when it has `to`', () => {
render(
);
expect(
screen.queryByRole( 'heading', { level: 1 } )
).not.toBeInTheDocument();
const links = screen.getAllByRole( 'link' );
expect( links ).toHaveLength( 2 );
expect( links[ 1 ] ).toHaveTextContent( 'Settings' );
expect( links[ 1 ] ).toHaveAttribute( 'href', '/settings' );
} );
it( 'should render preceding items as links', () => {
render(
);
const links = screen.getAllByRole( 'link' );
expect( links ).toHaveLength( 2 );
expect( links[ 0 ] ).toHaveTextContent( 'Home' );
expect( links[ 0 ] ).toHaveAttribute( 'href', '/' );
expect( links[ 1 ] ).toHaveTextContent( 'Settings' );
expect( links[ 1 ] ).toHaveAttribute( 'href', '/settings' );
} );
it( 'should never render preceding items as headings', () => {
render(
);
const headings = screen.getAllByRole( 'heading', { level: 1 } );
expect( headings ).toHaveLength( 1 );
expect( headings[ 0 ] ).toHaveTextContent( 'General' );
} );
it( 'should render a single item without `to` as an h1', () => {
render( );
expect(
screen.getByRole( 'heading', { level: 1 } )
).toHaveTextContent( 'Dashboard' );
expect( screen.queryByRole( 'link' ) ).not.toBeInTheDocument();
} );
it( 'should render inside a nav with an accessible label', () => {
render( );
expect(
screen.getByRole( 'navigation', { name: 'Breadcrumbs' } )
).toBeInTheDocument();
} );
} );
} );