2022-07-10 00:03:21 +03:00
|
|
|
import { screen, waitFor } from '@testing-library/react';
|
2023-09-05 10:08:42 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2022-05-03 21:36:24 +03:00
|
|
|
import { createMemoryHistory } from 'history';
|
2024-12-09 13:52:58 +03:00
|
|
|
import { Router } from 'react-router';
|
2023-09-05 10:08:42 +03:00
|
|
|
import { MainHeaderFactory } from '../../src/common/MainHeader';
|
2023-09-30 11:20:28 +03:00
|
|
|
import { checkAccessibility } from '../__helpers__/accessibility';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
2022-02-08 00:17:57 +03:00
|
|
|
|
2021-09-19 12:17:56 +03:00
|
|
|
describe('<MainHeader />', () => {
|
2023-09-05 10:08:42 +03:00
|
|
|
const MainHeader = MainHeaderFactory(fromPartial({
|
2023-09-30 11:20:28 +03:00
|
|
|
// Fake this component as a li, as it gets rendered inside a ul
|
|
|
|
ServersDropdown: () => <li>ServersDropdown</li>,
|
2023-09-05 10:08:42 +03:00
|
|
|
}));
|
2022-05-03 21:36:24 +03:00
|
|
|
const setUp = (pathname = '') => {
|
|
|
|
const history = createMemoryHistory();
|
|
|
|
history.push(pathname);
|
2021-09-19 12:17:56 +03:00
|
|
|
|
2022-07-10 00:03:21 +03:00
|
|
|
return renderWithEvents(
|
2022-05-03 21:36:24 +03:00
|
|
|
<Router location={history.location} navigator={history}>
|
|
|
|
<MainHeader />
|
|
|
|
</Router>,
|
|
|
|
);
|
2021-09-19 12:17:56 +03:00
|
|
|
};
|
|
|
|
|
2023-09-30 11:20:28 +03:00
|
|
|
it('passes a11y checks', () => checkAccessibility(setUp()));
|
|
|
|
|
2021-09-20 23:00:34 +03:00
|
|
|
it('renders ServersDropdown', () => {
|
2022-05-03 21:36:24 +03:00
|
|
|
setUp();
|
|
|
|
expect(screen.getByText('ServersDropdown')).toBeInTheDocument();
|
2021-09-19 12:17:56 +03:00
|
|
|
});
|
|
|
|
|
2021-09-20 23:00:34 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
['/foo', false],
|
|
|
|
['/bar', false],
|
|
|
|
['/settings', true],
|
|
|
|
['/settings/foo', true],
|
|
|
|
['/settings/bar', true],
|
2021-09-20 23:00:34 +03:00
|
|
|
])('sets link to settings as active only when current path is settings', (currentPath, isActive) => {
|
2022-05-03 21:36:24 +03:00
|
|
|
setUp(currentPath);
|
2021-09-19 12:17:56 +03:00
|
|
|
|
2022-05-03 21:36:24 +03:00
|
|
|
if (isActive) {
|
|
|
|
expect(screen.getByText(/Settings$/).getAttribute('class')).toContain('active');
|
|
|
|
} else {
|
|
|
|
expect(screen.getByText(/Settings$/).getAttribute('class')).not.toContain('active');
|
|
|
|
}
|
2021-09-19 12:17:56 +03:00
|
|
|
});
|
|
|
|
|
2022-05-11 20:18:43 +03:00
|
|
|
it('renders expected class based on the nav bar state', async () => {
|
|
|
|
const { user } = setUp();
|
2022-05-03 21:36:24 +03:00
|
|
|
|
|
|
|
const toggle = screen.getByLabelText('Toggle navigation');
|
|
|
|
const icon = toggle.firstChild;
|
2021-09-19 12:17:56 +03:00
|
|
|
|
2022-05-03 21:36:24 +03:00
|
|
|
expect(icon).toHaveAttribute('class', expect.stringMatching(/main-header__toggle-icon$/));
|
2022-05-11 20:18:43 +03:00
|
|
|
await user.click(toggle);
|
2022-05-03 21:36:24 +03:00
|
|
|
expect(icon).toHaveAttribute(
|
|
|
|
'class',
|
|
|
|
expect.stringMatching(/main-header__toggle-icon main-header__toggle-icon--opened$/),
|
2021-09-19 12:17:56 +03:00
|
|
|
);
|
2022-05-11 20:18:43 +03:00
|
|
|
await user.click(toggle);
|
2022-05-03 21:36:24 +03:00
|
|
|
expect(icon).toHaveAttribute('class', expect.stringMatching(/main-header__toggle-icon$/));
|
2021-09-19 12:17:56 +03:00
|
|
|
});
|
|
|
|
|
2022-05-03 21:36:24 +03:00
|
|
|
it('opens Collapse when clicking toggle', async () => {
|
2022-05-11 20:18:43 +03:00
|
|
|
const { container, user } = setUp();
|
2022-05-03 21:36:24 +03:00
|
|
|
const collapse = container.querySelector('.collapse');
|
|
|
|
const toggle = screen.getByLabelText('Toggle navigation');
|
2021-09-19 12:17:56 +03:00
|
|
|
|
2022-05-03 21:36:24 +03:00
|
|
|
expect(collapse).not.toHaveAttribute('class', expect.stringContaining('show'));
|
2022-05-11 20:18:43 +03:00
|
|
|
await user.click(toggle);
|
2022-05-03 21:36:24 +03:00
|
|
|
await waitFor(() => expect(collapse).toHaveAttribute('class', expect.stringContaining('show')));
|
2021-09-19 12:17:56 +03:00
|
|
|
});
|
|
|
|
});
|