mirror of
https://github.com/ansible/awx.git
synced 2026-02-28 08:18:43 -03:30
Merge pull request #16 from jlmitch5/indexJsxTestsAndRestructure
finish unit test coverage
This commit is contained in:
@@ -1 +1,7 @@
|
|||||||
module.exports = 'test-file-stub';
|
const path = require('path');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
process (src, filename) {
|
||||||
|
return `module.exports=${JSON.stringify(path.basename(filename))};`;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|||||||
@@ -4,6 +4,10 @@ import App from '../src/App';
|
|||||||
import api from '../src/api';
|
import api from '../src/api';
|
||||||
import Dashboard from '../src/pages/Dashboard';
|
import Dashboard from '../src/pages/Dashboard';
|
||||||
import Login from '../src/pages/Login';
|
import Login from '../src/pages/Login';
|
||||||
|
import { asyncFlush } from '../jest.setup';
|
||||||
|
|
||||||
|
const DEFAULT_ACTIVE_GROUP = 'views_group';
|
||||||
|
const DEFAULT_ACTIVE_ITEM = 'views_group_dashboard';
|
||||||
|
|
||||||
describe('<App />', () => {
|
describe('<App />', () => {
|
||||||
test('renders without crashing', () => {
|
test('renders without crashing', () => {
|
||||||
@@ -34,4 +38,40 @@ describe('<App />', () => {
|
|||||||
const login = appWrapper.find(Login);
|
const login = appWrapper.find(Login);
|
||||||
expect(login.length).toBe(0);
|
expect(login.length).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('onNavSelect sets state.activeItem and state.activeGroup', () => {
|
||||||
|
const appWrapper = shallow(<App />);
|
||||||
|
appWrapper.instance().onNavSelect({ itemId: 'foo', groupId: 'bar' });
|
||||||
|
expect(appWrapper.state().activeItem).toBe('foo');
|
||||||
|
expect(appWrapper.state().activeGroup).toBe('bar');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('onNavToggle sets state.isNavOpen to opposite', () => {
|
||||||
|
const appWrapper = shallow(<App />);
|
||||||
|
expect(appWrapper.state().isNavOpen).toBe(true);
|
||||||
|
appWrapper.instance().onNavToggle();
|
||||||
|
expect(appWrapper.state().isNavOpen).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('onLogoClick sets selected nav back to defaults', () => {
|
||||||
|
const appWrapper = shallow(<App />);
|
||||||
|
appWrapper.setState({ activeGroup: 'foo', activeItem: 'bar' });
|
||||||
|
expect(appWrapper.state().activeItem).toBe('bar');
|
||||||
|
expect(appWrapper.state().activeGroup).toBe('foo');
|
||||||
|
appWrapper.instance().onLogoClick();
|
||||||
|
expect(appWrapper.state().activeItem).toBe(DEFAULT_ACTIVE_ITEM);
|
||||||
|
expect(appWrapper.state().activeGroup).toBe(DEFAULT_ACTIVE_GROUP);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('api.logout called from logout button', async () => {
|
||||||
|
api.logout = jest.fn().mockImplementation(() => Promise.resolve({}));
|
||||||
|
const appWrapper = mount(<App />);
|
||||||
|
const logoutButton = appWrapper.find('LogoutButton');
|
||||||
|
logoutButton.props().onDevLogout();
|
||||||
|
appWrapper.setState({ activeGroup: 'foo', activeItem: 'bar' });
|
||||||
|
expect(api.logout).toHaveBeenCalledTimes(1);
|
||||||
|
await asyncFlush();
|
||||||
|
expect(appWrapper.state().activeItem).toBe(DEFAULT_ACTIVE_ITEM);
|
||||||
|
expect(appWrapper.state().activeGroup).toBe(DEFAULT_ACTIVE_GROUP);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
15
__tests__/components/About.test.jsx
Normal file
15
__tests__/components/About.test.jsx
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { mount } from 'enzyme';
|
||||||
|
import About from '../../src/components/About';
|
||||||
|
|
||||||
|
let aboutWrapper;
|
||||||
|
let headerElem;
|
||||||
|
|
||||||
|
describe('<About />', () => {
|
||||||
|
test('initially renders without crashing', () => {
|
||||||
|
aboutWrapper = mount(<About />);
|
||||||
|
headerElem = aboutWrapper.find('h2');
|
||||||
|
expect(aboutWrapper.length).toBe(1);
|
||||||
|
expect(headerElem.length).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -4,7 +4,7 @@ import {
|
|||||||
Redirect
|
Redirect
|
||||||
} from 'react-router-dom';
|
} from 'react-router-dom';
|
||||||
import { shallow } from 'enzyme';
|
import { shallow } from 'enzyme';
|
||||||
import ConditionalRedirect from '../src/components/ConditionalRedirect';
|
import ConditionalRedirect from '../../src/components/ConditionalRedirect';
|
||||||
|
|
||||||
describe('<ConditionalRedirect />', () => {
|
describe('<ConditionalRedirect />', () => {
|
||||||
test('renders Redirect when shouldRedirect is passed truthy func', () => {
|
test('renders Redirect when shouldRedirect is passed truthy func', () => {
|
||||||
27
__tests__/components/LogoutButton.test.jsx
Normal file
27
__tests__/components/LogoutButton.test.jsx
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { mount } from 'enzyme';
|
||||||
|
import LogoutButton from '../../src/components/LogoutButton';
|
||||||
|
|
||||||
|
let buttonWrapper;
|
||||||
|
let buttonElem;
|
||||||
|
let userIconElem;
|
||||||
|
|
||||||
|
const findChildren = () => {
|
||||||
|
buttonElem = buttonWrapper.find('Button');
|
||||||
|
userIconElem = buttonWrapper.find('UserIcon');
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('<LogoutButton />', () => {
|
||||||
|
test('initially renders without crashing', () => {
|
||||||
|
const onDevLogout = jest.fn();
|
||||||
|
buttonWrapper = mount(<LogoutButton onDevLogout={onDevLogout} />);
|
||||||
|
findChildren();
|
||||||
|
expect(buttonWrapper.length).toBe(1);
|
||||||
|
expect(buttonElem.length).toBe(1);
|
||||||
|
expect(userIconElem.length).toBe(1);
|
||||||
|
buttonElem.simulate('keyDown', { keyCode: 40, which: 40 });
|
||||||
|
expect(onDevLogout).toHaveBeenCalledTimes(0);
|
||||||
|
buttonElem.simulate('keyDown', { keyCode: 13, which: 13 });
|
||||||
|
expect(onDevLogout).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
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');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -3,7 +3,6 @@ module.exports = {
|
|||||||
'src/**/*.{js,jsx}'
|
'src/**/*.{js,jsx}'
|
||||||
],
|
],
|
||||||
moduleNameMapper: {
|
moduleNameMapper: {
|
||||||
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '<rootDir>/__mocks__/fileMock.js',
|
|
||||||
'\\.(css|scss|less)$': '<rootDir>/__mocks__/styleMock.js'
|
'\\.(css|scss|less)$': '<rootDir>/__mocks__/styleMock.js'
|
||||||
},
|
},
|
||||||
setupTestFrameworkScriptFile: '<rootDir>/jest.setup.js',
|
setupTestFrameworkScriptFile: '<rootDir>/jest.setup.js',
|
||||||
@@ -13,7 +12,8 @@ module.exports = {
|
|||||||
testEnvironment: 'jsdom',
|
testEnvironment: 'jsdom',
|
||||||
testURL: 'http://127.0.0.1:3001',
|
testURL: 'http://127.0.0.1:3001',
|
||||||
transform: {
|
transform: {
|
||||||
'^.+\\.(js|jsx)$': 'babel-jest'
|
'^.+\\.(js|jsx)$': 'babel-jest',
|
||||||
|
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '<rootDir>/__mocks__/fileMock.js',
|
||||||
},
|
},
|
||||||
transformIgnorePatterns: [
|
transformIgnorePatterns: [
|
||||||
'[/\\\\]node_modules[/\\\\].+\\.(?!(axios)/)(js|jsx)$'
|
'[/\\\\]node_modules[/\\\\].+\\.(?!(axios)/)(js|jsx)$'
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import {
|
|||||||
import {
|
import {
|
||||||
BackgroundImage,
|
BackgroundImage,
|
||||||
BackgroundImageSrc,
|
BackgroundImageSrc,
|
||||||
Button,
|
|
||||||
ButtonVariant,
|
|
||||||
Nav,
|
Nav,
|
||||||
NavExpandable,
|
NavExpandable,
|
||||||
NavList,
|
NavList,
|
||||||
@@ -18,12 +16,12 @@ import {
|
|||||||
PageHeader,
|
PageHeader,
|
||||||
PageSidebar
|
PageSidebar
|
||||||
} from '@patternfly/react-core';
|
} from '@patternfly/react-core';
|
||||||
import { UserIcon } from '@patternfly/react-icons';
|
|
||||||
import { global_breakpoint_md as breakpointMd } from '@patternfly/react-tokens';
|
import { global_breakpoint_md as breakpointMd } from '@patternfly/react-tokens';
|
||||||
|
|
||||||
import api from './api';
|
import api from './api';
|
||||||
|
|
||||||
// import About from './components/About';
|
// import About from './components/About';
|
||||||
|
import LogoutButton from './components/LogoutButton';
|
||||||
import TowerLogo from './components/TowerLogo';
|
import TowerLogo from './components/TowerLogo';
|
||||||
import ConditionalRedirect from './components/ConditionalRedirect';
|
import ConditionalRedirect from './components/ConditionalRedirect';
|
||||||
|
|
||||||
@@ -112,7 +110,7 @@ class App extends React.Component {
|
|||||||
header={(
|
header={(
|
||||||
<PageHeader
|
<PageHeader
|
||||||
logo={<TowerLogo onClick={this.onLogoClick} />}
|
logo={<TowerLogo onClick={this.onLogoClick} />}
|
||||||
avatar={<Button id="button-logout" aria-label="Logout" variant={ButtonVariant.plain} onClick={this.onDevLogout} onKeyDown={event => { if (event.keycode === 13) { this.onDevLogout(); } }}><UserIcon /></Button>}
|
avatar={<LogoutButton onDevLogout={() => this.onDevLogout()} />}
|
||||||
showNavToggle
|
showNavToggle
|
||||||
onNavToggle={this.onNavToggle}
|
onNavToggle={this.onNavToggle}
|
||||||
/>
|
/>
|
||||||
|
|||||||
26
src/components/LogoutButton.jsx
Normal file
26
src/components/LogoutButton.jsx
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import {
|
||||||
|
Button,
|
||||||
|
ButtonVariant
|
||||||
|
} from '@patternfly/react-core';
|
||||||
|
|
||||||
|
import { UserIcon } from '@patternfly/react-icons';
|
||||||
|
|
||||||
|
const LogoutButton = ({ onDevLogout }) => (
|
||||||
|
<Button
|
||||||
|
id="button-logout"
|
||||||
|
aria-label="Logout"
|
||||||
|
variant={ButtonVariant.plain}
|
||||||
|
onClick={onDevLogout}
|
||||||
|
onKeyDown={event => {
|
||||||
|
if (event.keyCode === 13) {
|
||||||
|
onDevLogout();
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<UserIcon />
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
|
||||||
|
export default LogoutButton;
|
||||||
@@ -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