mirror of
https://github.com/ansible/awx.git
synced 2026-05-17 14:27:42 -02:30
add TowerLogo tests
This commit is contained in:
58
__tests__/components/TowerLogo.test.jsx
Normal file
58
__tests__/components/TowerLogo.test.jsx
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { MemoryRouter } from 'react-router-dom';
|
||||||
|
import { mount } from 'enzyme';
|
||||||
|
import TowerLogo from '../../src/components/TowerLogo';
|
||||||
|
|
||||||
|
let logoWrapper;
|
||||||
|
let towerLogoElem;
|
||||||
|
let brandElem;
|
||||||
|
|
||||||
|
const findChildren = () => {
|
||||||
|
towerLogoElem = logoWrapper.find('TowerLogo');
|
||||||
|
brandElem = logoWrapper.find('Brand');
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('<TowerLogo />', () => {
|
||||||
|
test('initially renders without crashing', () => {
|
||||||
|
logoWrapper = mount(<MemoryRouter><TowerLogo /></MemoryRouter>);
|
||||||
|
findChildren();
|
||||||
|
expect(logoWrapper.length).toBe(1);
|
||||||
|
expect(towerLogoElem.length).toBe(1);
|
||||||
|
expect(brandElem.length).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('adds navigation to route history on click', () => {
|
||||||
|
const onLogoClick = jest.fn();
|
||||||
|
logoWrapper = mount(<MemoryRouter><TowerLogo onClick={onLogoClick} /></MemoryRouter>);
|
||||||
|
findChildren();
|
||||||
|
expect(towerLogoElem.props().history.length).toBe(1);
|
||||||
|
logoWrapper.simulate('click');
|
||||||
|
expect(towerLogoElem.props().history.length).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('gracefully handles not being passed click handler', () => {
|
||||||
|
logoWrapper = mount(<MemoryRouter><TowerLogo /></MemoryRouter>);
|
||||||
|
findChildren();
|
||||||
|
expect(towerLogoElem.props().history.length).toBe(1);
|
||||||
|
logoWrapper.simulate('click');
|
||||||
|
expect(towerLogoElem.props().history.length).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('handles mouse over and out state.hover changes', () => {
|
||||||
|
const onLogoClick = jest.fn();
|
||||||
|
logoWrapper = mount(<MemoryRouter><TowerLogo onClick={onLogoClick} /></MemoryRouter>);
|
||||||
|
findChildren();
|
||||||
|
findChildren();
|
||||||
|
expect(brandElem.props().src).toBe('tower-logo-header.svg');
|
||||||
|
brandElem.props().onMouseOver();
|
||||||
|
expect(towerLogoElem.state().hover).toBe(true);
|
||||||
|
logoWrapper.update();
|
||||||
|
findChildren();
|
||||||
|
expect(brandElem.props().src).toBe('tower-logo-header-hover.svg');
|
||||||
|
brandElem.props().onMouseOut();
|
||||||
|
expect(towerLogoElem.state().hover).toBe(false);
|
||||||
|
logoWrapper.update();
|
||||||
|
findChildren();
|
||||||
|
expect(brandElem.props().src).toBe('tower-logo-header.svg');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -13,13 +13,13 @@ class TowerLogo extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onClick = () => {
|
onClick = () => {
|
||||||
if (!this.props.onClick) return;
|
const { history, onClick: handleClick } = this.props;
|
||||||
|
|
||||||
const { history } = this.props;
|
if (!handleClick) return;
|
||||||
|
|
||||||
history.push('/');
|
history.push('/');
|
||||||
|
|
||||||
this.props.onClick();
|
handleClick();
|
||||||
};
|
};
|
||||||
|
|
||||||
onHover = () => {
|
onHover = () => {
|
||||||
@@ -30,10 +30,11 @@ class TowerLogo extends Component {
|
|||||||
|
|
||||||
render () {
|
render () {
|
||||||
const { hover } = this.state;
|
const { hover } = this.state;
|
||||||
|
const { onClick: handleClick } = this.props;
|
||||||
|
|
||||||
let src = TowerLogoHeader;
|
let src = TowerLogoHeader;
|
||||||
|
|
||||||
if (hover && this.props.onClick) {
|
if (hover && handleClick) {
|
||||||
src = TowerLogoHeaderHover;
|
src = TowerLogoHeaderHover;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user