fix logout in App.jsx and update test

This commit is contained in:
John Mitchell 2018-11-06 12:25:36 -05:00
parent 7b099578c8
commit df57b144c4
No known key found for this signature in database
GPG Key ID: FE6A9B5BD4EB5C94
2 changed files with 15 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import App from '../src/App';
import api from '../src/api';
import Dashboard from '../src/pages/Dashboard';
import Login from '../src/pages/Login';
import { asyncFlush } from '../jest.setup';
const DEFAULT_ACTIVE_GROUP = 'views_group';
const DEFAULT_ACTIVE_ITEM = 'views_group_dashboard';
@ -62,11 +63,15 @@ describe('<App />', () => {
expect(appWrapper.state().activeGroup).toBe(DEFAULT_ACTIVE_GROUP);
});
test('api.logout called from logout button', () => {
api.logout = jest.fn();
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);
});
});

View File

@ -76,6 +76,13 @@ class App extends React.Component {
this.setState({ activeGroup: 'views_group', activeItem: 'views_group_dashboard' });
}
onDevLogout = () => {
api.logout()
.then(() => {
this.setState({ activeGroup: 'views_group', activeItem: 'views_group_dashboard' });
});
}
render () {
const { activeItem, activeGroup, isNavOpen } = this.state;
const { logo, loginInfo } = this.props;
@ -103,7 +110,7 @@ class App extends React.Component {
header={(
<PageHeader
logo={<TowerLogo onClick={this.onLogoClick} />}
avatar={<LogoutButton onDevLogout={api.logout} />}
avatar={<LogoutButton onDevLogout={() => this.onDevLogout()} />}
showNavToggle
onNavToggle={this.onNavToggle}
/>