Update active nav item based on url

This commit is contained in:
Marliana Lara
2018-12-03 13:17:53 -05:00
parent 1e7ab9deed
commit 9341c4660c
3 changed files with 306 additions and 281 deletions

View File

@@ -1,5 +1,8 @@
import React from 'react';
import { HashRouter as Router } from 'react-router-dom';
import { shallow, mount } from 'enzyme';
import { createMemoryHistory } from 'history'
import App from '../src/App';
import api from '../src/api';
import { API_LOGOUT } from '../src/endpoints';
@@ -21,7 +24,7 @@ describe('<App />', () => {
api.isAuthenticated = jest.fn();
api.isAuthenticated.mockReturnValue(false);
const appWrapper = mount(<App />);
const appWrapper = mount(<Router><App /></Router>);
const login = appWrapper.find(Login);
expect(login.length).toBe(1);
@@ -33,7 +36,7 @@ describe('<App />', () => {
api.isAuthenticated = jest.fn();
api.isAuthenticated.mockReturnValue(true);
const appWrapper = mount(<App />);
const appWrapper = mount(<Router><App /></Router>);
const dashboard = appWrapper.find(Dashboard);
expect(dashboard.length).toBe(1);
@@ -42,39 +45,47 @@ describe('<App />', () => {
});
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');
const history = createMemoryHistory('/jobs');
const appWrapper = shallow(<App.WrappedComponent history={history} />);
appWrapper.instance().onNavSelect({ groupId: 'bar' });
expect(appWrapper.state().activeGroup).toBe('bar');
});
test('onNavToggle sets state.isNavOpen to opposite', () => {
const appWrapper = shallow(<App />);
const history = createMemoryHistory('/jobs');
const appWrapper = shallow(<App.WrappedComponent history={history} />);
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 />);
const history = createMemoryHistory('/jobs');
const appWrapper = shallow(<App.WrappedComponent history={history} />);
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.get = jest.fn().mockImplementation(() => Promise.resolve({}));
const appWrapper = mount(<App />);
const logoutButton = appWrapper.find('LogoutButton');
let appWrapper = mount(<Router><App /></Router>);
let logoutButton = appWrapper.find('LogoutButton');
logoutButton.props().onDevLogout();
appWrapper.setState({ activeGroup: 'foo', activeItem: 'bar' });
await asyncFlush();
expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(API_LOGOUT);
await asyncFlush();
expect(appWrapper.state().activeItem).toBe(DEFAULT_ACTIVE_ITEM);
console.log(appWrapper.state());
expect(appWrapper.state().activeGroup).toBe(DEFAULT_ACTIVE_GROUP);
});
});