2022-06-11 10:14:51 +03:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
import { render, screen } from '@testing-library/react';
|
|
|
|
import { MemoryRouter } from 'react-router-dom';
|
2022-02-14 21:58:20 +03:00
|
|
|
import { NavPillItem, NavPills } from '../../src/utils/NavPills';
|
|
|
|
|
|
|
|
describe('<NavPills />', () => {
|
2022-06-11 10:14:51 +03:00
|
|
|
let originalError: typeof console.error;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
originalError = console.error;
|
|
|
|
console.error = () => {}; // Suppress errors logged during this test
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
console.error = originalError;
|
|
|
|
});
|
|
|
|
|
2022-02-14 21:58:20 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
['Foo'],
|
|
|
|
[<span key="1">Hi!</span>],
|
|
|
|
[[<NavPillItem key="1" to="" />, <span key="2">Hi!</span>]],
|
2022-02-14 21:58:20 +03:00
|
|
|
])('throws error when any of the children is not a NavPillItem', (children) => {
|
|
|
|
expect.assertions(1);
|
2022-06-11 10:14:51 +03:00
|
|
|
expect(() => render(<NavPills>{children}</NavPills>)).toThrow(
|
|
|
|
'Only NavPillItem children are allowed inside NavPills.',
|
|
|
|
);
|
2022-02-14 21:58:20 +03:00
|
|
|
});
|
|
|
|
|
2022-02-14 22:28:28 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[undefined],
|
|
|
|
[true],
|
|
|
|
[false],
|
2022-02-14 22:28:28 +03:00
|
|
|
])('renders provided items', (fill) => {
|
2022-06-11 10:14:51 +03:00
|
|
|
const { container } = render(
|
|
|
|
<MemoryRouter>
|
|
|
|
<NavPills fill={fill}>
|
|
|
|
<NavPillItem to="1">1</NavPillItem>
|
|
|
|
<NavPillItem to="2">2</NavPillItem>
|
|
|
|
<NavPillItem to="3">3</NavPillItem>
|
|
|
|
</NavPills>
|
|
|
|
</MemoryRouter>,
|
2022-02-14 21:58:20 +03:00
|
|
|
);
|
|
|
|
|
2022-06-11 10:14:51 +03:00
|
|
|
const links = screen.getAllByRole('link');
|
|
|
|
expect(links).toHaveLength(3);
|
|
|
|
links.forEach((link, index) => {
|
|
|
|
expect(link).toHaveTextContent(`${index + 1}`);
|
|
|
|
expect(link).toHaveAttribute('href', `/${index + 1}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (fill) {
|
|
|
|
expect(container.querySelector('.nav')).toHaveClass('nav-fill');
|
|
|
|
} else {
|
|
|
|
expect(container.querySelector('.nav')).not.toHaveClass('nav-fill');
|
|
|
|
}
|
2022-02-14 21:58:20 +03:00
|
|
|
});
|
|
|
|
});
|