import { test, expect } from 'vitest';
import { render, screen } from '@testing-library/react';
import SequentialNavigation from './SequentialNavigation';
const NEXT_LINK_OBJECT = { title: 'Apply for or renew a Blue Badge?', href: '#next' }
const PREVIOUS_LINK_OBJECT = { title: 'Apply for or renew a Blue Badge?', href: '#prev' }
test('sequential navigation renders correctly', () => {
render(
{PREVIOUS_LINK_OBJECT.title}
{NEXT_LINK_OBJECT.title}
);
const sequentialNavigation = screen.getByRole('navigation');
const prevLink = screen.getAllByRole('link')[0];
const prevLinkWrapper = prevLink.parentElement;
const nextLink = screen.getAllByRole('link')[1];
const nextLinkWrapper = nextLink.parentElement;
expect(sequentialNavigation).toHaveClass('ds_sequential-nav');
expect(sequentialNavigation).toHaveAttribute('aria-label', 'Article navigation');
expect(prevLink).toHaveClass('ds_sequential-nav__button', 'ds_sequential-nav__button--left');
expect(prevLink).toHaveAttribute('href', PREVIOUS_LINK_OBJECT.href);
expect(prevLink.textContent).toEqual(PREVIOUS_LINK_OBJECT.title);
expect(prevLinkWrapper).toHaveClass('ds_sequential-nav__item', 'ds_sequential-nav__item--prev');
expect(prevLinkWrapper?.tagName).toEqual('DIV');
expect(prevLink.childNodes[0]).toHaveAttribute('data-label', 'Previous')
expect(nextLink).toHaveClass('ds_sequential-nav__button', 'ds_sequential-nav__button--right');
expect(nextLink).toHaveAttribute('href', NEXT_LINK_OBJECT.href);
expect(nextLink.textContent).toEqual(NEXT_LINK_OBJECT.title);
expect(nextLinkWrapper).toHaveClass('ds_sequential-nav__item', 'ds_sequential-nav__item--next');
expect(nextLinkWrapper?.tagName).toEqual('DIV');
expect(nextLink.childNodes[0]).toHaveAttribute('data-label', 'Next');
});
test('with custom aria label', () => {
const ARIA_LABEL = 'My label';
render(
);
const sequentialNavigation = screen.getByRole('navigation');
expect(sequentialNavigation).toHaveAttribute('aria-label', ARIA_LABEL);
});
test('sequential nav links with custom element', () => {
render(
(
)}>
{PREVIOUS_LINK_OBJECT.title}
);
const link = screen.queryByRole('link');
expect(link?.tagName).toEqual('SPAN');
expect(link?.textContent).toEqual(PREVIOUS_LINK_OBJECT.title);
});
test('custom text label on link', () => {
const TEXT_LABEL = 'foo';
render(
{PREVIOUS_LINK_OBJECT.title}
);
const prevLink = screen.getAllByRole('link')[0];
expect(prevLink.querySelector('span')?.dataset.label).toEqual(TEXT_LABEL);
});
test('passing additional props', () => {
render(
);
const sequentialNavigation = screen.getByRole('navigation');
expect(sequentialNavigation?.dataset.test).toEqual('foo');
});
test('passing additional CSS classes', () => {
render(
);
const sequentialNavigation = screen.getByRole('navigation');
expect(sequentialNavigation).toHaveClass('foo', 'ds_sequential-nav');
});