/tests/unit/controlgroup/controlgroup_core.js

https://github.com/jakeboone02/jquery-mobile · JavaScript · 174 lines · 126 code · 36 blank · 12 comment · 6 complexity · e4e82fa78434ec88b20feba0e390bb58 MD5 · raw file

  1. /*
  2. * mobile controlgroup unit tests
  3. */
  4. // delay the start to prevent the tests running before enhancement has completed
  5. $.testHelper.delayStart();
  6. (function($){
  7. module( 'vertical controlgroup, no refresh' , {
  8. setup: function() {
  9. this.vcontrolgroup = $( "#vertical-controlgroup" );
  10. }
  11. });
  12. test( "Pre-rendered controlgroup option-setting works", function() {
  13. var grp = $( "#pre-rendered" );
  14. grp.controlgroup( "option", "type", "horizontal" );
  15. deepEqual( grp.hasClass( "ui-controlgroup-horizontal" ), true, "After setting type to horizontal, the class ui-controlgroup-horizontal has been added." );
  16. deepEqual( grp.hasClass( "ui-controlgroup-vertical" ), false, "After setting type to horizontal, the class ui-controlgroup-vertical has been removed." );
  17. });
  18. test( "vertical controlgroup classes", function() {
  19. var buttons = this.vcontrolgroup.find( ".ui-btn" ),
  20. middlebuttons = buttons.filter(function(index) {
  21. return index > 0 && index < (length-1);
  22. }),
  23. length = buttons.length;
  24. ok( buttons.first().hasClass( "ui-first-child" ), "first button should have class 'ui-first-child'" );
  25. ok( !middlebuttons.hasClass( "ui-first-child" ), "middle buttons should not have class 'ui-first-child'" );
  26. ok( !middlebuttons.hasClass( "ui-last-child" ), "middle buttons should not have class 'ui-last-child'" );
  27. ok( buttons.last().hasClass( "ui-last-child"), "last button should have class 'ui-last-child'" );
  28. });
  29. module( 'vertical controlgroup, refresh', {
  30. setup: function() {
  31. this.vcontrolgroup = $( "#vertical-controlgroup" );
  32. this.vcontrolgroup.find( ".ui-btn" ).show();
  33. this.vcontrolgroup.controlgroup( "refresh" );
  34. }
  35. });
  36. test( "vertical controlgroup after first button was hidden", function() {
  37. //https://github.com/jquery/jquery-mobile/issues/1929
  38. //We hide the first button and refresh
  39. this.vcontrolgroup.find( ".ui-btn" ).first().hide();
  40. this.vcontrolgroup.controlgroup( "refresh" );
  41. var buttons = this.vcontrolgroup.find( ".ui-btn" ).filter( ":visible" ),
  42. middlebuttons = buttons.filter(function(index) { return index > 0 && index < (length-1)}),
  43. length = buttons.length;
  44. ok( buttons.first().hasClass( "ui-first-child" ), "first visible button should have class 'ui-first-child'" );
  45. ok( !middlebuttons.hasClass( "ui-first-child" ), "middle buttons should not have class 'ui-first-child'" );
  46. ok( !middlebuttons.hasClass( "ui-last-child" ), "middle buttons should not have class 'ui-last-child'" );
  47. ok( buttons.last().hasClass( "ui-last-child"), "last visible button should have class 'ui-last-child'" );
  48. });
  49. test( "vertical controlgroup after last button was hidden", function() {
  50. //https://github.com/jquery/jquery-mobile/issues/1929
  51. //We hide the last button and refresh
  52. this.vcontrolgroup.find( ".ui-btn" ).last().hide();
  53. this.vcontrolgroup.controlgroup( "refresh" );
  54. var buttons = this.vcontrolgroup.find( ".ui-btn" ).filter( ":visible" ),
  55. middlebuttons = buttons.filter(function(index) { return index > 0 && index < (length-1)}),
  56. length = buttons.length;
  57. ok( buttons.first().hasClass( "ui-first-child" ), "first visible button should have class 'ui-first-child'" );
  58. ok( !middlebuttons.hasClass( "ui-first-child" ), "middle buttons should not have class 'ui-first-child'" );
  59. ok( !middlebuttons.hasClass( "ui-last-child" ), "middle buttons should not have class 'ui-last-child'" );
  60. ok( buttons.last().hasClass( "ui-last-child"), "last visible button should have class 'ui-last-child'" );
  61. });
  62. module( 'horizontal controlgroup, no refresh', {
  63. setup: function() {
  64. this.hcontrolgroup = $( "#horizontal-controlgroup" );
  65. }
  66. });
  67. test( "horizontal controlgroup classes", function() {
  68. var buttons = this.hcontrolgroup.find( ".ui-btn" ),
  69. middlebuttons = buttons.filter(function(index) { return index > 0 && index < (length-1)}),
  70. length = buttons.length;
  71. ok( buttons.first().hasClass( "ui-first-child" ), "first button should have class 'ui-first-child'" );
  72. ok( !middlebuttons.hasClass( "ui-first-child" ), "middle buttons should not have class 'ui-first-child'" );
  73. ok( !middlebuttons.hasClass( "ui-last-child" ), "middle buttons should not have class 'ui-last-child'" );
  74. ok( buttons.last().hasClass( "ui-last-child"), "last button should have class 'ui-last-child'" );
  75. });
  76. module( 'horizontal controlgroup, refresh', {
  77. setup: function() {
  78. this.hcontrolgroup = $( "#horizontal-controlgroup" );
  79. this.hcontrolgroup.find( ".ui-btn" ).show();
  80. this.hcontrolgroup.controlgroup( "refresh" );
  81. }
  82. });
  83. test( "horizontal controlgroup after first button was hidden", function() {
  84. //We hide the first button and refresh
  85. this.hcontrolgroup.find( ".ui-btn" ).first().hide();
  86. this.hcontrolgroup.controlgroup( "refresh" );
  87. var buttons = this.hcontrolgroup.find( ".ui-btn" ).filter( ":visible" ),
  88. middlebuttons = buttons.filter(function(index) { return index > 0 && index < (length-1)}),
  89. length = buttons.length;
  90. ok( buttons.first().hasClass( "ui-first-child" ), "first visible button should have class 'ui-first-child'" );
  91. ok( !middlebuttons.hasClass( "ui-first-child" ), "middle buttons should not have class 'ui-first-child'" );
  92. ok( !middlebuttons.hasClass( "ui-last-child" ), "middle buttons should not have class 'ui-last-child'" );
  93. ok( buttons.last().hasClass( "ui-last-child"), "last visible button should have class 'ui-last-child'" );
  94. });
  95. test( "horizontal controlgroup after last button was hidden", function() {
  96. //We hide the last button and refresh
  97. this.hcontrolgroup.find( ".ui-btn" ).last().hide();
  98. this.hcontrolgroup.controlgroup( "refresh" );
  99. var buttons = this.hcontrolgroup.find( ".ui-btn" ).filter( ":visible" ),
  100. middlebuttons = buttons.filter(function(index) { return index > 0 && index < (length-1)}),
  101. length = buttons.length;
  102. ok( buttons.first().hasClass( "ui-first-child" ), "first visible button should have class 'ui-first-child'" );
  103. ok( !middlebuttons.hasClass( "ui-first-child" ), "middle buttons should not have class 'ui-first-child'" );
  104. ok( !middlebuttons.hasClass( "ui-last-child" ), "middle buttons should not have class 'ui-last-child'" );
  105. ok( buttons.last().hasClass( "ui-last-child"), "last visible button should have class 'ui-last-child'" );
  106. });
  107. test( "controlgroups will create when inside a container that receives a 'create' event", function(){
  108. ok( !$("#enhancetest").appendTo(".ui-page-active").find(".ui-controlgroup").length, "did not have enhancements applied" );
  109. ok( $("#enhancetest").trigger("create").find(".ui-controlgroup").length, "enhancements applied" );
  110. });
  111. test( "controlgroups in ignored containers aren't enhanced", function() {
  112. var $unenhancedFieldSet = $( "#unenhanced-fieldset" ),
  113. $enhancedFieldSet = $( "#enhanced-fieldset" );
  114. $.mobile.ignoreContentEnabled = true;
  115. // attempt to enhance the controlgroup
  116. $unenhancedFieldSet.parent().trigger("create");
  117. deepEqual( $unenhancedFieldSet.length, 1, "the fieldset test fixtures exist" );
  118. ok( !$unenhancedFieldSet.is(".ui-controlgroup"), "there is no control group" );
  119. // attempt to enhance the controlgroup
  120. $enhancedFieldSet.parent().trigger("create");
  121. deepEqual( $enhancedFieldSet.length, 1, "the fieldset test fixtures exist" );
  122. ok( $enhancedFieldSet.is(".ui-controlgroup"), "there is a control group" );
  123. $.mobile.ignoreContentEnabled = false;
  124. });
  125. test( "calling .controlgroup() again is the same as calling .controlgroup( 'refresh' )", function() {
  126. var btn1 = $( "<a href='#' data-nstest-role='button'>Option 3</a>" ),
  127. btn2 = $( "<a href='#' data-nstest-role='button'>Option 4</a>" ),
  128. grp = $( "#test-reinstantiate" );
  129. grp
  130. .controlgroup( "container" )
  131. .prepend( btn1 )
  132. .append( btn2 )
  133. .trigger( "create" )
  134. .controlgroup();
  135. ok( btn1.hasClass( "ui-first-child" ), "The prepended button has class ui-first-child" );
  136. ok( btn2.hasClass( "ui-last-child" ), "The appended button has class ui-last-child" );
  137. });
  138. })(jQuery);