import { createMemoryHistory } from 'history' ; import history from './history' ; describe ( 'services/history' , ( ) => { const fallbackRoute = '/web' ; let browserHistory ; beforeEach ( ( ) => { history . init ( createMemoryHistory ( ) ) ; browserHistory = history . original ( ) ; jest . spyOn ( browserHistory , 'push' ) ; jest . spyOn ( history , 'getRoutes' ) ; jest . spyOn ( history , '_pageRefresh' ) . mockImplementation ( ) ; } ) ; afterEach ( ( ) => { jest . clearAllMocks ( ) ; } ) ; describe ( 'canPush' , ( ) => { const push = actual => ( { andExpect ( expected ) { history . push ( actual ) ; expect ( browserHistory . push ) . toHaveBeenCalledWith ( expected ) ; } , } ) ; it ( 'should push if allowed else fallback to default route' , ( ) => { jest . spyOn ( history , 'getRoutes' ) . mockReturnValue ( [ '/valid' , '/' , '/test/:param' , '/test/:param/:optional?' , '/web/cluster/:siteId/node/:serverId/:login/:sid?' , ] ) ; push ( 'invalid' ) . andExpect ( fallbackRoute ) ; push ( '.' ) . andExpect ( fallbackRoute ) ; push ( '/valid/test' ) . andExpect ( fallbackRoute ) ; push ( '@#4' ) . andExpect ( fallbackRoute ) ; push ( '/valid' ) . andExpect ( '/valid' ) ; push ( '' ) . andExpect ( '' ) ; push ( '/' ) . andExpect ( '/' ) ; push ( '/test/param1' ) . andExpect ( '/test/param1' ) ; push ( '/test/param1/param2' ) . andExpect ( '/test/param1/param2' ) ; push ( '/web/cluster/one/node/xxx/root/yyyy/xxx/unknown' ) . andExpect ( fallbackRoute ) ; push ( '/web/cluster/one/node/xxx/root' ) . andExpect ( '/web/cluster/one/node/xxx/root' ) ; push ( '/web/cluster/one/node/xxx/root/yyy' ) . andExpect ( '/web/cluster/one/node/xxx/root/yyy' ) ; } ) ; it ( 'should refresh a page if called withRefresh=true' , ( ) => { let route = '/' ; jest . spyOn ( history , 'getRoutes' ) . mockReturnValue ( [ route ] ) ; history . push ( route , true ) ; expect ( history . _pageRefresh ) . toHaveBeenCalledWith ( route ) ; } ) ; } ) ; describe ( 'goToLogin()' , ( ) => { it ( 'should navigate to login with URL that has redirect parameter with current location' , ( ) => { jest . spyOn ( history , 'getRoutes' ) . mockReturnValue ( [ '/web/login' , '/current-location' ] ) ; history . original ( ) . location . pathname = '/current-location' ; history . goToLogin ( true ) ; const expected = '/web/login?redirect_uri=http://localhost/current-location' ; expect ( history . _pageRefresh ) . toHaveBeenCalledWith ( expected ) ; } ) ; } ) ; } ) ;