mirror of
https://github.com/ansible/awx.git
synced 2026-05-07 09:27:36 -02:30
update App.jsx and improve coverage
abstract LogoutButton to component
This commit is contained in:
@@ -5,6 +5,9 @@ import api from '../src/api';
|
||||
import Dashboard from '../src/pages/Dashboard';
|
||||
import Login from '../src/pages/Login';
|
||||
|
||||
const DEFAULT_ACTIVE_GROUP = 'views_group';
|
||||
const DEFAULT_ACTIVE_ITEM = 'views_group_dashboard';
|
||||
|
||||
describe('<App />', () => {
|
||||
test('renders without crashing', () => {
|
||||
const appWrapper = shallow(<App />);
|
||||
@@ -34,4 +37,36 @@ describe('<App />', () => {
|
||||
const login = appWrapper.find(Login);
|
||||
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', () => {
|
||||
api.logout = jest.fn();
|
||||
const appWrapper = mount(<App />);
|
||||
const logoutButton = appWrapper.find('LogoutButton');
|
||||
logoutButton.props().onDevLogout();
|
||||
expect(api.logout).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user