import test from 'ava' ; import { MenuLike , Submenu } from '../../source/menu-like' ; import { MenuMiddleware } from '../../source/menu-middleware' ; import { ButtonAction } from '../../source/action-hive' ; const EMPTY_MENU = { listSubmenus : ( ) => new Set ( ) , renderActionHandlers : ( ) => new Set ( ) , renderBody : ( ) => 'whatever' , renderKeyboard : ( ) => [ ] , } ; test ( 'empty tree' , t => { const tree = new MenuMiddleware ( '/' , EMPTY_MENU ) . tree ( ) ; t . is ( tree , `Menu Tree
menu                          /
` ) ; } ) ; test ( 'action' , t => { const action = { trigger : /^\/what$/ , doFunction : ( ) => { throw new Error ( 'dont call me' ) ; } , } ; const menu = { listSubmenus : ( ) => new Set ( [ ] ) , renderActionHandlers : ( ) => new Set ( [ action ] ) , renderBody : ( ) => 'whatever' , renderKeyboard : ( ) => [ ] , } ; const tree = new MenuMiddleware ( '/' , menu ) . tree ( ) ; t . is ( tree , `Menu Tree
menu                          /
  action                      /what
` ) ; } ) ; test ( 'submenu' , t => { const submenu = { action : /submenu\// , hide : ( ) => false , menu : EMPTY_MENU , } ; const menu = { listSubmenus : ( ) => new Set ( [ submenu ] ) , renderActionHandlers : ( ) => new Set ( ) , renderBody : ( ) => 'whatever' , renderKeyboard : ( ) => [ ] , } ; const tree = new MenuMiddleware ( '/' , menu ) . tree ( ) ; t . is ( tree , `Menu Tree
menu                          /
  menu                        /submenu/
` ) ; } ) ; test ( 'subsubmenu' , t => { const subsubmenu = { action : /deep\// , hide : ( ) => false , menu : EMPTY_MENU , } ; const submenuMenu = { listSubmenus : ( ) => new Set ( [ subsubmenu ] ) , renderActionHandlers : ( ) => new Set ( ) , renderBody : ( ) => 'submenu' , renderKeyboard : ( ) => [ ] , } ; const submenu = { action : /submenu\// , hide : ( ) => false , menu : submenuMenu , } ; const menu = { listSubmenus : ( ) => new Set ( [ submenu ] ) , renderActionHandlers : ( ) => new Set ( ) , renderBody : ( ) => 'whatever' , renderKeyboard : ( ) => [ ] , } ; const tree = new MenuMiddleware ( '/' , menu ) . tree ( ) ; t . is ( tree , `Menu Tree
menu                          /
  menu                        /submenu/
    menu                      /submenu/deep/
` ) ; } ) ; test ( 'action in submenu' , t => { const action = { trigger : /^\/submenu\/what$/ , doFunction : ( ) => { throw new Error ( 'dont call me' ) ; } , } ; const submenuMenu = { listSubmenus : ( ) => new Set ( ) , renderActionHandlers : ( ) => new Set ( [ action ] ) , renderBody : ( ) => 'submenu' , renderKeyboard : ( ) => [ ] , } ; const submenu = { action : /submenu\// , hide : ( ) => false , menu : submenuMenu , } ; const menu = { listSubmenus : ( ) => new Set ( [ submenu ] ) , renderActionHandlers : ( ) => new Set ( ) , renderBody : ( ) => 'whatever' , renderKeyboard : ( ) => [ ] , } ; const tree = new MenuMiddleware ( '/' , menu ) . tree ( ) ; t . is ( tree , `Menu Tree
menu                          /
  menu                        /submenu/
    action                    /submenu/what
` ) ; } ) ;