nav-menu.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*global wpNavMenu */
  2. ( function( QUnit, $ ) {
  3. QUnit.module( 'nav-menu' );
  4. var assert,
  5. eventsExpected = 3,
  6. eventsFired = 0;
  7. // Fail if we don't see the expected number of events triggered in 3 seconds.
  8. setTimeout( function( assert ) {
  9. // QUnit may load this file without running it, in which case `assert`
  10. // will never be set to `assertPassed` below.
  11. assert && assert.equal(
  12. eventsFired,
  13. eventsExpected,
  14. eventsExpected + ' wpNavMenu events should fire.'
  15. );
  16. }, 3000 );
  17. QUnit.test( 'Testing wpNavMenu event triggers.', function( assertPassed ) {
  18. assert = assertPassed;
  19. assert.expect( 3 );
  20. var testString = '<div>Hello World</div>';
  21. // Mock the internal function calls so the don't fail.
  22. $.fn.hideAdvancedMenuItemFields = function() {
  23. return {
  24. 'appendTo': function() { return true; },
  25. 'prependTo': function() { return true; }
  26. };
  27. };
  28. $.fn.extend( {
  29. 'childMenuItems': function() { return $(); },
  30. 'shiftDepthClass': function() { return $(); }
  31. } );
  32. // Set up the events we should test.
  33. var eventsToTest = [
  34. {
  35. 'event': 'addMenuItemToBottom',
  36. 'data': testString,
  37. 'expect': $( testString ),
  38. 'shouldTrigger': 'menu-item-added'
  39. },
  40. {
  41. 'event': 'addMenuItemToTop',
  42. 'data': testString,
  43. 'expect': $( testString ),
  44. 'shouldTrigger': 'menu-item-added'
  45. },
  46. {
  47. 'event': 'removeMenuItem',
  48. 'data': $( testString ),
  49. 'expect': $( testString ),
  50. 'shouldTrigger': 'menu-removing-item'
  51. }
  52. ];
  53. // Test each of the events.
  54. _.each( eventsToTest, function( theEvent ) {
  55. var done = assert.async();
  56. $( document ).on( theEvent.shouldTrigger, function( evt, passed ) {
  57. assert.equal(
  58. passed.html(),
  59. theEvent.expect.html(),
  60. 'The ' + theEvent.event + ' should trigger ' + theEvent.shouldTrigger + '.'
  61. );
  62. eventsFired++;
  63. done();
  64. } );
  65. wpNavMenu[ theEvent.event ]( theEvent.data );
  66. $( document ).off( theEvent.shouldTrigger );
  67. } );
  68. } );
  69. } )( window.QUnit, jQuery );