/tests/integration/table/columntoggle_option_core.js

https://github.com/gabrielschulhof/jquery-mobile · JavaScript · 190 lines · 145 code · 44 blank · 1 comment · 6 complexity · 65f290efca4177dee265c95e73a3d47a MD5 · raw file

  1. define( [ "qunit", "jquery" ], function( QUnit, $ ) {
  2. // These tests run on both prerendered and non-prerendered tables
  3. $.mobile.ns = "nstest-";
  4. QUnit.module( "Columntoggle options" );
  5. function getSwatchClasses( element, prefix ) {
  6. var index,
  7. list = element.className.split( " " ),
  8. resultList = [],
  9. regex = new RegExp( "^" + prefix + "([a-z]|inherit)$" );
  10. for ( index in list ) {
  11. if ( list[ index ].match( regex ) ) {
  12. resultList.push( list[ index ] );
  13. }
  14. }
  15. return resultList;
  16. }
  17. function testClasses( assert, table, element, prefix, startClass, optionName, newValue, endClass ) {
  18. var swatchClasses;
  19. swatchClasses = getSwatchClasses( element, prefix );
  20. assert.deepEqual( swatchClasses.length, startClass ? 1 : 0,
  21. "Element has " + ( startClass ? "one" : "no" ) + " swatch class at first" );
  22. if ( startClass ) {
  23. assert.deepEqual( swatchClasses[ 0 ], startClass,
  24. "Element initial swatch class is '" + startClass + "'" );
  25. }
  26. table.table( "option", optionName, newValue );
  27. swatchClasses = getSwatchClasses( element, prefix );
  28. assert.deepEqual( swatchClasses.length, endClass ? 1 : 0,
  29. "Element has " + ( endClass ? "one" : "no" ) + " swatch after setting '" +
  30. optionName + "' to '" + newValue + "'" );
  31. if ( endClass ) {
  32. assert.deepEqual( swatchClasses[ 0 ], endClass,
  33. "Element swatch class after setting option is '" + endClass + "'" );
  34. }
  35. }
  36. QUnit.test( "Default columnButtonTheme", function( assert ) {
  37. testClasses( assert,
  38. $( "#columntoggle-option-test-blank" ),
  39. $( "#columntoggle-option-test-blank-button" )[ 0 ],
  40. "ui-button-", "ui-button-inherit", "columnButtonTheme", "b", "ui-button-b" );
  41. } );
  42. QUnit.test( "Explicit columnButtonTheme", function( assert ) {
  43. testClasses( assert,
  44. $( "#columntoggle-option-test-explicit" ),
  45. $( "#columntoggle-option-test-explicit-button" )[ 0 ],
  46. "ui-button-", "ui-button-b", "columnButtonTheme", "a", "ui-button-a" );
  47. } );
  48. QUnit.test( "Default columnPopupTheme", function( assert ) {
  49. var popup = $( "#columntoggle-option-test-blank-popup" );
  50. assert.deepEqual( popup.popup( "option", "theme" ), "inherit",
  51. "Popup has no theme assigned initially" );
  52. testClasses( assert,
  53. $( "#columntoggle-option-test-blank" ),
  54. popup[ 0 ],
  55. "ui-body-", "ui-body-inherit", "columnPopupTheme", "b", "ui-body-b" );
  56. assert.deepEqual( popup.popup( "option", "theme" ), "b",
  57. "Popup has theme 'b' assigned in the end" );
  58. } );
  59. QUnit.test( "Explicit columnPopupTheme", function( assert ) {
  60. var popup = $( "#columntoggle-option-test-explicit-popup" );
  61. assert.deepEqual( popup.popup( "option", "theme" ), "b",
  62. "Popup has swatch 'b' assigned initially" );
  63. testClasses( assert,
  64. $( "#columntoggle-option-test-explicit" ),
  65. popup[ 0 ],
  66. "ui-body-", "ui-body-b", "columnPopupTheme", "a", "ui-body-a" );
  67. assert.deepEqual( popup.popup( "option", "theme" ), "a",
  68. "Popup has theme 'a' assigned in the end" );
  69. } );
  70. QUnit.test( "Explicitly assigned columnButtonText", function( assert ) {
  71. $( "#columntoggle-option-test-explicit" ).table( "option", "columnButtonText", "xyzzy" );
  72. assert.deepEqual( $( "#columntoggle-option-test-explicit-button" ).text(), "xyzzy",
  73. "Button text assigned via option is propagated to the button" );
  74. } );
  75. function isMenuButton( element, tableId ) {
  76. return element.is( "a#" + tableId + "-button[href='#" + tableId + "-popup']" );
  77. }
  78. QUnit.test( "Default columnButton", function( assert ) {
  79. assert.deepEqual(
  80. isMenuButton( $( "#columntoggle-option-test-blank" ).prev(),
  81. "columntoggle-option-test-blank" ),
  82. true,
  83. "By default the table is preceded by a button that opens the column selection popup" );
  84. } );
  85. function testColumnButtonOption( assert, prefix, tableId, shouldBeThere ) {
  86. var table = $( "#" + tableId ),
  87. button = table.prev();
  88. assert.deepEqual( isMenuButton( table.prev(), tableId ), shouldBeThere,
  89. prefix + "Button is initially present" );
  90. table.table( "option", "columnButton", false );
  91. assert.deepEqual( isMenuButton( table.prev(), tableId ), false,
  92. prefix + "Button is absent when 'columnButton' option is off" );
  93. table.table( "option", "columnButton", true );
  94. assert.deepEqual( isMenuButton( table.prev(), tableId ), shouldBeThere,
  95. prefix + "Button is present when 'columnButton' option is turned back on" );
  96. assert.deepEqual( table.prev()[ 0 ], button[ 0 ], prefix + "Button is reused" );
  97. }
  98. QUnit.test( "Toggling option columnButton works", function( assert ) {
  99. testColumnButtonOption( assert, "", "columntoggle-toggle-button", true );
  100. } );
  101. QUnit.test( "Toggling option columnButton when initially false works", function( assert ) {
  102. var tableId = "columntoggle-toggle-button-initially-absent",
  103. table = $( "#" + tableId );
  104. assert.deepEqual( isMenuButton( table.prev(), tableId ), false,
  105. "Initially, no button precedes the table" );
  106. table.table( "option", "columnButton", true );
  107. assert.deepEqual( isMenuButton( table.prev(), tableId ), true,
  108. "Button is present after option is turned on" );
  109. } );
  110. QUnit.test( "Toggling option columnUi works", function( assert ) {
  111. var tableId = "columntoggle-toggle-ui",
  112. table = $( "#" + tableId ),
  113. testUIPresence = function( prefix, shouldBeThere ) {
  114. var negation = shouldBeThere ? "" : "not ",
  115. inputs = $( "#" + tableId + "-popup" ).find( "input" );
  116. assert.deepEqual( isMenuButton( table.prev(), tableId ), shouldBeThere,
  117. prefix + "Button is " + negation + "present" );
  118. assert.deepEqual( $( "#" + tableId + "-popup" ).length, ( shouldBeThere ? 1 : 0 ),
  119. prefix + "Popup is " + negation + "present" );
  120. if ( shouldBeThere ) {
  121. assert.deepEqual( !!$( "#" + tableId + "-popup" ).data( "mobile-popup" ), shouldBeThere,
  122. prefix + "Popup is popup widget" );
  123. }
  124. assert.deepEqual(
  125. table
  126. .find( "thead > tr" )
  127. .children( shouldBeThere ? "[data-" + $.mobile.ns + "priority]" : "*" )
  128. .is( function() {
  129. var data = $( this ).jqmData( "input" );
  130. return shouldBeThere ?
  131. !( data && inputs.index( data[ 0 ] ) >= 0 ) :
  132. !!data;
  133. } ),
  134. false,
  135. prefix + "Data " + negation + "present at header key 'input'" +
  136. ( shouldBeThere ? " and it refers to an input in the popup" : "" ) );
  137. testColumnButtonOption( assert, "While UI is " + negation + "present: ", tableId,
  138. shouldBeThere );
  139. };
  140. testUIPresence( "By default: ", true );
  141. table.table( "option", "columnUi", false );
  142. testUIPresence( "After turning off option: ", false );
  143. table.table( "option", "columnUi", true );
  144. testUIPresence( "After turning option back on: ", true );
  145. } );
  146. } );