2018-10-24 23:53:16 +03:00
import React from 'react' ;
2018-12-03 21:17:53 +03:00
import { HashRouter as Router } from 'react-router-dom' ;
2018-10-24 23:53:16 +03:00
import { shallow , mount } from 'enzyme' ;
2018-12-03 21:17:53 +03:00
import { createMemoryHistory } from 'history'
2018-11-02 20:44:13 +03:00
import App from '../src/App' ;
import api from '../src/api' ;
2018-11-13 17:53:36 +03:00
import { API _LOGOUT } from '../src/endpoints' ;
2018-11-13 17:46:43 +03:00
2018-11-02 20:44:13 +03:00
import Dashboard from '../src/pages/Dashboard' ;
import Login from '../src/pages/Login' ;
2018-11-06 20:25:36 +03:00
import { asyncFlush } from '../jest.setup' ;
2018-10-24 23:53:16 +03:00
2018-11-03 00:47:59 +03:00
const DEFAULT _ACTIVE _GROUP = 'views_group' ;
const DEFAULT _ACTIVE _ITEM = 'views_group_dashboard' ;
2018-10-24 23:53:16 +03:00
describe ( '<App />' , ( ) => {
test ( 'renders without crashing' , ( ) => {
const appWrapper = shallow ( < App / > ) ;
expect ( appWrapper . length ) . toBe ( 1 ) ;
} ) ;
test ( 'renders login page when not authenticated' , ( ) => {
api . isAuthenticated = jest . fn ( ) ;
api . isAuthenticated . mockReturnValue ( false ) ;
2018-12-03 21:17:53 +03:00
const appWrapper = mount ( < Router > < App / > < / Router > ) ;
2018-10-24 23:53:16 +03:00
2018-10-25 04:15:08 +03:00
const login = appWrapper . find ( Login ) ;
expect ( login . length ) . toBe ( 1 ) ;
const dashboard = appWrapper . find ( Dashboard ) ;
expect ( dashboard . length ) . toBe ( 0 ) ;
2018-10-24 23:53:16 +03:00
} ) ;
test ( 'renders dashboard when authenticated' , ( ) => {
api . isAuthenticated = jest . fn ( ) ;
api . isAuthenticated . mockReturnValue ( true ) ;
2018-12-03 21:17:53 +03:00
const appWrapper = mount ( < Router > < App / > < / Router > ) ;
2018-10-24 23:53:16 +03:00
2018-10-25 04:15:08 +03:00
const dashboard = appWrapper . find ( Dashboard ) ;
expect ( dashboard . length ) . toBe ( 1 ) ;
const login = appWrapper . find ( Login ) ;
expect ( login . length ) . toBe ( 0 ) ;
2018-10-24 23:53:16 +03:00
} ) ;
2018-11-03 00:47:59 +03:00
test ( 'onNavSelect sets state.activeItem and state.activeGroup' , ( ) => {
2018-12-03 21:17:53 +03:00
const history = createMemoryHistory ( '/jobs' ) ;
const appWrapper = shallow ( < App.WrappedComponent history = { history } / > ) ;
appWrapper . instance ( ) . onNavSelect ( { groupId : 'bar' } ) ;
2018-11-03 00:47:59 +03:00
expect ( appWrapper . state ( ) . activeGroup ) . toBe ( 'bar' ) ;
} ) ;
test ( 'onNavToggle sets state.isNavOpen to opposite' , ( ) => {
2018-12-03 21:17:53 +03:00
const history = createMemoryHistory ( '/jobs' ) ;
const appWrapper = shallow ( < App.WrappedComponent history = { history } / > ) ;
2018-11-03 00:47:59 +03:00
expect ( appWrapper . state ( ) . isNavOpen ) . toBe ( true ) ;
appWrapper . instance ( ) . onNavToggle ( ) ;
expect ( appWrapper . state ( ) . isNavOpen ) . toBe ( false ) ;
} ) ;
test ( 'onLogoClick sets selected nav back to defaults' , ( ) => {
2018-12-03 21:17:53 +03:00
const history = createMemoryHistory ( '/jobs' ) ;
const appWrapper = shallow ( < App.WrappedComponent history = { history } / > ) ;
2018-11-03 00:47:59 +03:00
appWrapper . setState ( { activeGroup : 'foo' , activeItem : 'bar' } ) ;
expect ( appWrapper . state ( ) . activeItem ) . toBe ( 'bar' ) ;
expect ( appWrapper . state ( ) . activeGroup ) . toBe ( 'foo' ) ;
appWrapper . instance ( ) . onLogoClick ( ) ;
expect ( appWrapper . state ( ) . activeGroup ) . toBe ( DEFAULT _ACTIVE _GROUP ) ;
} ) ;
2018-11-06 20:25:36 +03:00
test ( 'api.logout called from logout button' , async ( ) => {
2018-11-13 17:53:36 +03:00
api . get = jest . fn ( ) . mockImplementation ( ( ) => Promise . resolve ( { } ) ) ;
2018-12-03 21:17:53 +03:00
let appWrapper = mount ( < Router > < App / > < / Router > ) ;
let logoutButton = appWrapper . find ( 'LogoutButton' ) ;
2018-11-03 00:47:59 +03:00
logoutButton . props ( ) . onDevLogout ( ) ;
2018-11-06 20:25:36 +03:00
appWrapper . setState ( { activeGroup : 'foo' , activeItem : 'bar' } ) ;
2018-12-03 21:17:53 +03:00
await asyncFlush ( ) ;
2018-11-13 17:53:36 +03:00
expect ( api . get ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( api . get ) . toHaveBeenCalledWith ( API _LOGOUT ) ;
2018-12-03 21:17:53 +03:00
console . log ( appWrapper . state ( ) ) ;
2018-11-06 20:25:36 +03:00
expect ( appWrapper . state ( ) . activeGroup ) . toBe ( DEFAULT _ACTIVE _GROUP ) ;
2018-11-03 00:47:59 +03:00
} ) ;
2018-12-03 21:17:53 +03:00
2018-10-30 22:07:45 +03:00
} ) ;