2021-09-19 12:17:56 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
2022-02-08 00:17:57 +03:00
|
|
|
import { useLocation } from 'react-router-dom';
|
2021-09-19 12:17:56 +03:00
|
|
|
import { Collapse, NavbarToggler, NavLink } from 'reactstrap';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import createMainHeader from '../../src/common/MainHeader';
|
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
jest.mock('react-router-dom', () => ({
|
|
|
|
...jest.requireActual('react-router-dom'),
|
|
|
|
useLocation: jest.fn().mockReturnValue({}),
|
|
|
|
}));
|
|
|
|
|
2021-09-19 12:17:56 +03:00
|
|
|
describe('<MainHeader />', () => {
|
|
|
|
const ServersDropdown = () => null;
|
|
|
|
const MainHeader = createMainHeader(ServersDropdown);
|
|
|
|
let wrapper: ShallowWrapper;
|
|
|
|
|
|
|
|
const createWrapper = (pathname = '') => {
|
2022-02-08 00:17:57 +03:00
|
|
|
(useLocation as any).mockReturnValue({ pathname });
|
2021-09-19 12:17:56 +03:00
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
wrapper = shallow(<MainHeader />);
|
2021-09-19 12:17:56 +03:00
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
afterEach(jest.clearAllMocks);
|
2021-09-19 12:17:56 +03:00
|
|
|
afterEach(() => wrapper?.unmount());
|
|
|
|
|
2021-09-20 23:00:34 +03:00
|
|
|
it('renders ServersDropdown', () => {
|
2021-09-19 12:17:56 +03:00
|
|
|
const wrapper = createWrapper();
|
|
|
|
|
|
|
|
expect(wrapper.find(ServersDropdown)).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
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) => {
|
2021-09-19 12:17:56 +03:00
|
|
|
const wrapper = createWrapper(currentPath);
|
|
|
|
const settingsLink = wrapper.find(NavLink);
|
|
|
|
|
|
|
|
expect(settingsLink.prop('active')).toEqual(isActive);
|
|
|
|
});
|
|
|
|
|
2021-09-20 23:00:34 +03:00
|
|
|
it('renders expected class based on the nav bar state', () => {
|
2021-09-19 12:17:56 +03:00
|
|
|
const wrapper = createWrapper();
|
|
|
|
|
|
|
|
expect(wrapper.find(NavbarToggler).find(FontAwesomeIcon).prop('className')).toEqual('main-header__toggle-icon');
|
|
|
|
wrapper.find(NavbarToggler).simulate('click');
|
|
|
|
expect(wrapper.find(NavbarToggler).find(FontAwesomeIcon).prop('className')).toEqual(
|
|
|
|
'main-header__toggle-icon main-header__toggle-icon--opened',
|
|
|
|
);
|
|
|
|
wrapper.find(NavbarToggler).simulate('click');
|
|
|
|
expect(wrapper.find(NavbarToggler).find(FontAwesomeIcon).prop('className')).toEqual('main-header__toggle-icon');
|
|
|
|
});
|
|
|
|
|
2021-09-20 23:00:34 +03:00
|
|
|
it('opens Collapse when clicking toggle', () => {
|
2021-09-19 12:17:56 +03:00
|
|
|
const wrapper = createWrapper();
|
|
|
|
|
|
|
|
expect(wrapper.find(Collapse).prop('isOpen')).toEqual(false);
|
|
|
|
wrapper.find(NavbarToggler).simulate('click');
|
|
|
|
expect(wrapper.find(Collapse).prop('isOpen')).toEqual(true);
|
|
|
|
wrapper.find(NavbarToggler).simulate('click');
|
|
|
|
expect(wrapper.find(Collapse).prop('isOpen')).toEqual(false);
|
|
|
|
});
|
|
|
|
});
|