customize-nav-menus.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* global wp */
  2. jQuery( 'body' ).on( 'load', function() {
  3. var api = wp.customize,
  4. primaryMenuId = 3,
  5. socialMenuId = 2;
  6. QUnit.module( 'Customize Nav Menus' );
  7. /**
  8. * Generate 20 IDs and verify they are all unique.
  9. */
  10. QUnit.test( 'generatePlaceholderAutoIncrementId generates unique IDs', function( assert ) {
  11. var testIterations = 20,
  12. ids = [ api.Menus.generatePlaceholderAutoIncrementId() ];
  13. while ( testIterations ) {
  14. var placeholderID = api.Menus.generatePlaceholderAutoIncrementId();
  15. assert.ok( -1 === ids.indexOf( placeholderID ) );
  16. ids.push( placeholderID );
  17. testIterations -= 1;
  18. }
  19. } );
  20. QUnit.test( 'it should parse _wpCustomizeMenusSettings.defaults into itself', function( assert ) {
  21. assert.deepEqual( window._wpCustomizeNavMenusSettings, api.Menus.data );
  22. } );
  23. QUnit.test( 'empty menus should have no Menu Item Controls', function( assert ) {
  24. assert.ok( 0 === wp.customize.Menus.getMenuControl( socialMenuId ).getMenuItemControls().length, 'empty menus' );
  25. } );
  26. QUnit.test( 'populated menus should have no Menu Item Controls', function( assert ) {
  27. assert.ok( 0 !== wp.customize.Menus.getMenuControl( primaryMenuId ).getMenuItemControls().length, 'non-empty menus' );
  28. } );
  29. // @todo Add tests for api.Menus.AvailableMenuItemsPanelView
  30. // (and api.Menus.AvailableItemCollection, api.Menus.AvailableItemModel).
  31. QUnit.test( 'there is a properly configured MenusPanel', function( assert ) {
  32. var panel, sections;
  33. panel = api.panel( 'nav_menus' );
  34. assert.ok( panel );
  35. assert.ok( panel.extended( api.Menus.MenusPanel ) );
  36. sections = panel.sections();
  37. assert.ok( 'menu_locations' === sections[0].id, 'first section is menu_locations' );
  38. assert.ok( sections[1].extended( api.Menus.MenuSection ), 'second section is MenuSection' );
  39. assert.ok( sections[ sections.length - 1 ].extended( api.Menus.NewMenuSection ), 'last section is NewMenuSection' );
  40. } );
  41. // @todo Add more tests for api.Menus.MenusPanel behaviors.
  42. QUnit.test( 'there an expected MenuSection for the primary menu', function( assert ) {
  43. var section, controls, lastControl;
  44. section = api.section( 'nav_menu[' + primaryMenuId + ']' );
  45. assert.ok( section, 'section exists' );
  46. assert.ok( section.extended( api.Menus.MenuSection ), 'section is a api.Menus.MenuSection' );
  47. assert.ok( section.deferred.initSortables, 'has section.deferred.initSortables' );
  48. assert.ok( section.active(), 'section active() is true' );
  49. assert.ok( section.active.set( false ).get(), 'section active() cannot be set false' );
  50. controls = section.controls();
  51. assert.ok( controls[0].extended( api.Menus.MenuNameControl ), 'first control in menu section is MenuNameControl' );
  52. assert.ok( controls[1].extended( api.Menus.MenuItemControl ), 'second control in menu section is MenuItemControl' );
  53. lastControl = controls[ controls.length - 1 ];
  54. assert.ok( lastControl.extended( api.Control ), 'last control in menu section is a base Control' );
  55. assert.ok( lastControl.params.templateId === 'nav-menu-delete-button', 'last control in menu section has a delete-button template' );
  56. } );
  57. // @todo Add more tests for api.Menus.MenuSection behaviors.
  58. QUnit.test( 'changing a MenuNameControl change the corresponding menu value', function( assert ) {
  59. var section, control;
  60. section = api.section( 'nav_menu[' + primaryMenuId + ']' );
  61. control = section.controls()[0];
  62. assert.ok( control.extended( api.Menus.MenuNameControl ), 'control is a MenuNameControl' );
  63. assert.equal( control.setting().name, 'Primary menu' );
  64. assert.ok( ! control.setting._dirty );
  65. control.container.find( 'input[type=text]:first' ).val( 'Main menu' ).trigger( 'change' );
  66. assert.equal( control.setting().name, 'Main menu' );
  67. assert.ok( control.setting._dirty );
  68. } );
  69. // @todo Add more tests for api.Menus.MenuNameControl
  70. QUnit.test( 'manipulating a MenuItemControl works', function( assert ) {
  71. var section, control, value;
  72. section = api.section( 'nav_menu[' + primaryMenuId + ']' );
  73. assert.ok( section );
  74. control = section.controls()[1];
  75. assert.ok( control.extended( api.Menus.MenuItemControl ), 'control is a MenuItemControl' );
  76. control.actuallyEmbed();
  77. control.container.find( '.edit-menu-item-title' ).val( 'Hello World' ).trigger( 'change' );
  78. assert.equal( control.setting().title, 'Hello World' );
  79. value = _.clone( control.setting() );
  80. value.title = 'Hola Mundo';
  81. assert.equal( control.container.find( '.edit-menu-item-title' ).val(), 'Hello World' );
  82. assert.equal( value.position, 1 );
  83. assert.equal( control.priority(), 1 );
  84. // @todo Test control.moveDown().
  85. } );
  86. // @todo Add more tests for api.Menus.MenuItemControl.
  87. // @todo Add tests for api.Menus.NewMenuSection.
  88. // @todo Add tests for api.Menus.MenuLocationControl.
  89. // @todo Add tests for api.Menus.MenuLocationsControl.
  90. // @todo Add tests for api.Menus.MenuAutoAddControl.
  91. // @todo Add tests for api.Menus.MenuControl.
  92. // @todo Add tests for api.Menus.applySavedData.
  93. // @todo Add tests for api.Menus.focusMenuItemControl.
  94. // @todo Add tests for api.Menus.createNavMenu.
  95. QUnit.test( 'api.Menus.getMenuControl() should return the expected control', function( assert ) {
  96. var control = api.Menus.getMenuControl( primaryMenuId );
  97. assert.ok( !! control, 'control is returned' );
  98. assert.ok( control.extended( api.Menus.MenuControl ), 'control is a MenuControl' );
  99. } );
  100. QUnit.test( 'api.Menus.getMenuItemControl() should return the expected control', function( assert ) {
  101. var control = api.Menus.getMenuItemControl( 2000 );
  102. assert.ok( !! control, 'control is returned' );
  103. assert.ok( control.extended( api.Menus.MenuItemControl ), 'control is a MenuItemControl' );
  104. } );
  105. } );