shlink-web-client/test/common/MainHeader.test.tsx

67 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-09-19 12:17:56 +03:00
import { shallow, ShallowWrapper } from 'enzyme';
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';
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 = '') => {
(useLocation as any).mockReturnValue({ pathname });
2021-09-19 12:17:56 +03:00
wrapper = shallow(<MainHeader />);
2021-09-19 12:17:56 +03:00
return wrapper;
};
afterEach(jest.clearAllMocks);
2021-09-19 12:17:56 +03:00
afterEach(() => wrapper?.unmount());
it('renders ServersDropdown', () => {
2021-09-19 12:17:56 +03:00
const wrapper = createWrapper();
expect(wrapper.find(ServersDropdown)).toHaveLength(1);
});
it.each([
2021-09-19 12:17:56 +03:00
[ '/foo', false ],
[ '/bar', false ],
[ '/settings', true ],
])('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);
});
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');
});
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);
});
});