2018-10-24 23:53:16 +03:00
import React from 'react' ;
2019-01-07 15:15:10 +03:00
import { MemoryRouter } from 'react-router-dom' ;
2019-01-03 03:49:34 +03:00
import { I18nProvider } from '@lingui/react' ;
2019-01-02 09:35:34 +03:00
2019-01-03 03:49:34 +03:00
import { mount , shallow } from 'enzyme' ;
2019-01-02 09:11:13 +03:00
import { asyncFlush } from '../jest.setup' ;
2019-01-03 03:49:34 +03:00
import App from '../src/App' ;
2019-01-02 09:11:13 +03:00
2018-10-24 23:53:16 +03:00
describe ( '<App />' , ( ) => {
2019-01-03 03:49:34 +03:00
test ( 'expected content is rendered' , ( ) => {
const appWrapper = mount (
2019-01-07 15:15:10 +03:00
< MemoryRouter >
2019-01-03 03:49:34 +03:00
< I18nProvider >
< App
routeGroups = { [
{
2019-01-03 17:40:48 +03:00
groupTitle : 'Group One' ,
2019-01-03 03:49:34 +03:00
groupId : 'group_one' ,
routes : [
{ title : 'Foo' , path : '/foo' } ,
{ title : 'Bar' , path : '/bar' } ,
] ,
} ,
{
2019-01-03 17:40:48 +03:00
groupTitle : 'Group Two' ,
2019-01-03 03:49:34 +03:00
groupId : 'group_two' ,
routes : [
{ title : 'Fiz' , path : '/fiz' } ,
]
}
] }
render = { ( { routeGroups } ) => (
routeGroups . map ( ( { groupId } ) => ( < div key = { groupId } id = { groupId } / > ) )
) }
/ >
< / I18nProvider >
2019-01-07 15:15:10 +03:00
< / MemoryRouter >
2019-01-03 03:49:34 +03:00
) ;
// page components
2018-10-24 23:53:16 +03:00
expect ( appWrapper . length ) . toBe ( 1 ) ;
2019-01-03 03:49:34 +03:00
expect ( appWrapper . find ( 'PageHeader' ) . length ) . toBe ( 1 ) ;
expect ( appWrapper . find ( 'PageSidebar' ) . length ) . toBe ( 1 ) ;
// sidebar groups and route links
expect ( appWrapper . find ( 'NavExpandableGroup' ) . length ) . toBe ( 2 ) ;
expect ( appWrapper . find ( 'a[href="/#/foo"]' ) . length ) . toBe ( 1 ) ;
expect ( appWrapper . find ( 'a[href="/#/bar"]' ) . length ) . toBe ( 1 ) ;
expect ( appWrapper . find ( 'a[href="/#/fiz"]' ) . length ) . toBe ( 1 ) ;
// inline render
expect ( appWrapper . find ( '#group_one' ) . length ) . toBe ( 1 ) ;
expect ( appWrapper . find ( '#group_two' ) . length ) . toBe ( 1 ) ;
2018-10-24 23:53:16 +03:00
} ) ;
2019-01-07 15:15:10 +03:00
test ( 'opening the about modal renders prefetched config data' , async ( done ) => {
const ansible _version = '111' ;
const version = '222' ;
2019-01-24 21:05:36 +03:00
const getConfig = jest . fn ( ( ) => Promise . resolve ( { data : { ansible _version , version } } ) ) ;
2019-01-07 15:15:10 +03:00
const api = { getConfig } ;
const wrapper = mount (
< MemoryRouter >
< I18nProvider >
2019-01-24 21:05:36 +03:00
< App api = { api } / >
2019-01-07 15:15:10 +03:00
< / I18nProvider >
< / MemoryRouter >
) ;
await asyncFlush ( ) ;
expect ( getConfig ) . toHaveBeenCalledTimes ( 1 ) ;
// open about modal
const aboutDropdown = 'Dropdown QuestionCircleIcon' ;
const aboutButton = 'DropdownItem li button' ;
const aboutModalContent = 'AboutModalBoxContent' ;
const aboutModalClose = 'button[aria-label="Close Dialog"]' ;
expect ( wrapper . find ( aboutModalContent ) ) . toHaveLength ( 0 ) ;
wrapper . find ( aboutDropdown ) . simulate ( 'click' ) ;
wrapper . find ( aboutButton ) . simulate ( 'click' ) ;
wrapper . update ( ) ;
// check about modal content
const content = wrapper . find ( aboutModalContent ) ;
expect ( content ) . toHaveLength ( 1 ) ;
expect ( content . find ( 'dd' ) . text ( ) ) . toContain ( ansible _version ) ;
expect ( content . find ( 'pre' ) . text ( ) ) . toContain ( ` < Tower ${ version } > ` ) ;
// close about modal
wrapper . find ( aboutModalClose ) . simulate ( 'click' ) ;
expect ( wrapper . find ( aboutModalContent ) ) . toHaveLength ( 0 ) ;
done ( ) ;
} ) ;
2018-11-03 00:47:59 +03:00
test ( 'onNavToggle sets state.isNavOpen to opposite' , ( ) => {
2019-01-02 09:11:13 +03:00
const appWrapper = shallow ( < App / > ) ;
2019-01-03 03:49:34 +03:00
const { onNavToggle } = appWrapper . instance ( ) ;
[ true , false , true , false , true ] . forEach ( expected => {
expect ( appWrapper . state ( ) . isNavOpen ) . toBe ( expected ) ;
onNavToggle ( ) ;
} ) ;
2018-11-03 00:47:59 +03:00
} ) ;
2019-01-03 17:40:48 +03:00
test ( 'onLogout makes expected call to api client' , async ( done ) => {
2019-01-03 03:49:34 +03:00
const logout = jest . fn ( ( ) => Promise . resolve ( ) ) ;
const api = { logout } ;
2019-01-03 17:40:48 +03:00
const appWrapper = shallow ( < App api = { api } / > ) ;
2019-01-03 03:49:34 +03:00
2019-01-03 17:40:48 +03:00
appWrapper . instance ( ) . onLogout ( ) ;
2019-01-02 09:11:13 +03:00
await asyncFlush ( ) ;
2019-01-03 03:49:34 +03:00
expect ( api . logout ) . toHaveBeenCalledTimes ( 1 ) ;
done ( ) ;
2018-11-03 00:47:59 +03:00
} ) ;
2018-10-30 22:07:45 +03:00
} ) ;