tablesorter_test.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.ui.TableSorterTest');
  15. goog.setTestOnly('goog.ui.TableSorterTest');
  16. goog.require('goog.array');
  17. goog.require('goog.dom');
  18. goog.require('goog.dom.TagName');
  19. goog.require('goog.dom.classlist');
  20. goog.require('goog.testing.events');
  21. goog.require('goog.testing.jsunit');
  22. goog.require('goog.ui.TableSorter');
  23. var oldHtml;
  24. var alphaHeader, numberHeader, notSortableHeader, table, tableSorter;
  25. function setUpPage() {
  26. oldHtml = goog.dom.getElement('content').innerHTML;
  27. }
  28. function setUp() {
  29. goog.dom.getElement('content').innerHTML = oldHtml;
  30. table = goog.dom.getElement('sortable');
  31. alphaHeader = goog.dom.getElementsByTagName(goog.dom.TagName.TH, table)[0];
  32. numberHeader = goog.dom.getElementsByTagName(goog.dom.TagName.TH, table)[1];
  33. notSortableHeader =
  34. goog.dom.getElementsByTagName(goog.dom.TagName.TH, table)[2];
  35. tableSorter = new goog.ui.TableSorter();
  36. tableSorter.setSortFunction(0, goog.ui.TableSorter.alphaSort);
  37. tableSorter.setSortFunction(2, goog.ui.TableSorter.noSort);
  38. tableSorter.decorate(table);
  39. }
  40. function tearDown() {
  41. tableSorter.dispose();
  42. table = null;
  43. }
  44. function testConstructor() {
  45. assertNotNull('Should have successful construction', tableSorter);
  46. assertNotNull('Should be in document', tableSorter);
  47. }
  48. function testForwardAlpha() {
  49. goog.testing.events.fireClickEvent(alphaHeader);
  50. assertOrder(['A', '10', 'B', '0', 'C', '10', 'C', '17', 'C', '3']);
  51. assertTrue(
  52. goog.dom.classlist.contains(alphaHeader, 'goog-tablesorter-sorted'));
  53. assertEquals(0, tableSorter.getSortColumn());
  54. assertFalse(tableSorter.isSortReversed());
  55. }
  56. function testBackwardAlpha() {
  57. goog.testing.events.fireClickEvent(alphaHeader);
  58. goog.testing.events.fireClickEvent(alphaHeader);
  59. assertOrder(['C', '10', 'C', '17', 'C', '3', 'B', '0', 'A', '10']);
  60. assertFalse(
  61. goog.dom.classlist.contains(alphaHeader, 'goog-tablesorter-sorted'));
  62. assertTrue(
  63. goog.dom.classlist.contains(
  64. alphaHeader, 'goog-tablesorter-sorted-reverse'));
  65. assertEquals(0, tableSorter.getSortColumn());
  66. assertTrue(tableSorter.isSortReversed());
  67. }
  68. function testForwardNumeric() {
  69. goog.testing.events.fireClickEvent(numberHeader);
  70. assertOrder(['B', '0', 'C', '3', 'C', '10', 'A', '10', 'C', '17']);
  71. assertTrue(
  72. goog.dom.classlist.contains(numberHeader, 'goog-tablesorter-sorted'));
  73. assertEquals(1, tableSorter.getSortColumn());
  74. assertFalse(tableSorter.isSortReversed());
  75. }
  76. function testBackwardNumeric() {
  77. goog.testing.events.fireClickEvent(numberHeader);
  78. goog.testing.events.fireClickEvent(numberHeader);
  79. assertOrder(['C', '17', 'C', '10', 'A', '10', 'C', '3', 'B', '0']);
  80. assertTrue(
  81. goog.dom.classlist.contains(
  82. numberHeader, 'goog-tablesorter-sorted-reverse'));
  83. assertEquals(1, tableSorter.getSortColumn());
  84. assertTrue(tableSorter.isSortReversed());
  85. }
  86. function testAlphaThenNumeric() {
  87. testForwardAlpha();
  88. goog.testing.events.fireClickEvent(numberHeader);
  89. assertOrder(['B', '0', 'C', '3', 'A', '10', 'C', '10', 'C', '17']);
  90. assertFalse(
  91. goog.dom.classlist.contains(alphaHeader, 'goog-tablesorter-sorted'));
  92. assertEquals(1, tableSorter.getSortColumn());
  93. assertFalse(tableSorter.isSortReversed());
  94. }
  95. function testNotSortableUnchanged() {
  96. goog.testing.events.fireClickEvent(notSortableHeader);
  97. assertEquals(0, goog.dom.classlist.get(notSortableHeader).length);
  98. assertEquals(-1, tableSorter.getSortColumn());
  99. }
  100. function testSortWithNonDefaultSortableHeaderRowIndex() {
  101. // Check that clicking on non-sortable header doesn't trigger any sorting.
  102. assertOrder(['C', '10', 'A', '10', 'C', '17', 'B', '0', 'C', '3']);
  103. goog.testing.events.fireClickEvent(goog.dom.getElement('not-sortable'));
  104. assertOrder(['C', '10', 'A', '10', 'C', '17', 'B', '0', 'C', '3']);
  105. }
  106. function testsetSortableHeaderRowIndexAfterDecorateThrows() {
  107. var func = function() { tableSorter.setSortableHeaderRowIndex(0); };
  108. var msg = assertThrows('failFunc should throw.', func)['message'];
  109. assertEquals('Component already rendered', msg);
  110. }
  111. function testSortOnSecondHeaderRow() {
  112. // Test a table with multiple table headers.
  113. // Using setSortableHeaderRowIndex one can specify table header columns to use
  114. // in sorting.
  115. var tableSorter2 = new goog.ui.TableSorter();
  116. tableSorter2.setSortableHeaderRowIndex(1);
  117. tableSorter2.decorate(goog.dom.getElement('sortable-2'));
  118. // Initial order.
  119. assertOrder(
  120. ['4', '5', '6', '1', '2', '3', '3', '1', '9'],
  121. goog.dom.getElement('sortable-2'));
  122. // Sort on first column.
  123. goog.testing.events.fireClickEvent(goog.dom.getElement('sorttable-2-col-1'));
  124. assertOrder(
  125. ['1', '2', '3', '3', '1', '9', '4', '5', '6'],
  126. goog.dom.getElement('sortable-2'));
  127. // Sort on second column.
  128. goog.testing.events.fireClickEvent(goog.dom.getElement('sorttable-2-col-2'));
  129. assertOrder(
  130. ['3', '1', '9', '1', '2', '3', '4', '5', '6'],
  131. goog.dom.getElement('sortable-2'));
  132. // Sort on third column.
  133. goog.testing.events.fireClickEvent(goog.dom.getElement('sorttable-2-col-3'));
  134. assertOrder(
  135. ['1', '2', '3', '4', '5', '6', '3', '1', '9'],
  136. goog.dom.getElement('sortable-2'));
  137. // Reverse sort on third column.
  138. goog.testing.events.fireClickEvent(goog.dom.getElement('sorttable-2-col-3'));
  139. assertOrder(
  140. ['3', '1', '9', '4', '5', '6', '1', '2', '3'],
  141. goog.dom.getElement('sortable-2'));
  142. tableSorter2.dispose();
  143. }
  144. function testSortAfterSwapping() {
  145. // First click
  146. goog.testing.events.fireClickEvent(alphaHeader);
  147. assertOrder(['A', '10', 'B', '0', 'C', '10', 'C', '17', 'C', '3']);
  148. assertEquals(0, tableSorter.getSortColumn());
  149. // Move first column to the end
  150. for (var i = 0, r; (r = table.rows[i]); i++) {
  151. var cell = r.cells[0];
  152. cell.parentNode.appendChild(cell);
  153. }
  154. // Make sure the above worked as expected
  155. assertOrder(['10', 'A', '0', 'B', '10', 'C', '17', 'C', '3', 'C']);
  156. // Our column is now the second one
  157. assertEquals(2, tableSorter.getSortColumn());
  158. // Second click, should reverse
  159. tableSorter.setSortFunction(2, goog.ui.TableSorter.alphaSort);
  160. goog.testing.events.fireClickEvent(alphaHeader);
  161. assertOrder(['10', 'C', '17', 'C', '3', 'C', '0', 'B', '10', 'A']);
  162. }
  163. function testTwoBodies() {
  164. var table3 = goog.dom.getElement('sortable-3');
  165. var header = goog.dom.getElement('sortable-3-col');
  166. var sorter3 = new goog.ui.TableSorter();
  167. sorter3.setSortFunction(0, goog.ui.TableSorter.alphaSort);
  168. try {
  169. sorter3.decorate(table3);
  170. goog.testing.events.fireClickEvent(header);
  171. assertOrder(['A', 'B', 'C', 'A', 'B', 'C'], table3);
  172. goog.testing.events.fireClickEvent(header);
  173. assertOrder(['C', 'B', 'A', 'C', 'B', 'A'], table3);
  174. } finally {
  175. sorter3.dispose();
  176. }
  177. }
  178. function testNaNs() {
  179. var table = goog.dom.getElement('sortable-4');
  180. var header = goog.dom.getElement('sortable-4-col');
  181. var sorter = new goog.ui.TableSorter();
  182. try {
  183. // All non-numbers compare equal, i.e. Bar == Foo, so order of those
  184. // elements should not change (since we are using stable sort).
  185. sorter.decorate(table);
  186. goog.testing.events.fireClickEvent(header);
  187. assertOrder(['2', '3', '11', 'Bar', 'Foo'], table);
  188. goog.testing.events.fireClickEvent(header);
  189. assertOrder(['Bar', 'Foo', '11', '3', '2'], table);
  190. } finally {
  191. sorter.dispose();
  192. }
  193. }
  194. function assertOrder(arr, opt_table) {
  195. var tbl = opt_table || table;
  196. var actual = [];
  197. goog.array.forEach(
  198. goog.dom.getElementsByTagName(goog.dom.TagName.TD, tbl),
  199. function(td, idx) {
  200. var txt = goog.dom.getTextContent(td);
  201. if (txt) {
  202. actual.push(txt);
  203. }
  204. });
  205. assertArrayEquals(arr, actual);
  206. }