Debian Codegraph @

tests/tests.js

raw symbols SCIP
1 /*
2 * IMPORTANT NOTE: Because testing DOM manipulation is really annoying, my
3 * tests will be created using many setTimeouts. This means if your computer
4 * is substantially slower than mine, you may get inconsistent results.
5 */
6 $(function(){
7 // ===========================================================================
8 // Test helpers & QUnit callbacks
9 // ===========================================================================
10 window.WAIT_TIME_MS = 200;
11
12 var get_column_elements = function($table, col_index){
13 var vals = [];
14 $table.find("tbody tr").each(function(){
15 var val = $(this).children("td").eq(col_index).html();
16 vals.push(val);
17 });
18 return vals;
19 };
20
21 var test_table_state = function(fn){
22 setTimeout(function(){
23 fn();
24 QUnit.start();
25 }, WAIT_TIME_MS);
26 };
27
28 var date_from_string = function(str){
29 var months = ["jan","feb","mar","apr","may","jun","jul",
30 "aug","sep","oct","nov","dec"];
31 var pattern = "^([a-zA-Z]{3})\\s*(\\d{2}),\\s*(\\d{4})$";
32 var re = new RegExp(pattern);
33 var DateParts = re.exec(str).slice(1);
34
35 var Year = DateParts[2];
36 var Month = $.inArray(DateParts[0].toLowerCase(), months);
37 var Day = DateParts[1];
38 return new Date(Year, Month, Day);
39 };
40
41 /*
42 * In order to accurately simulate a double click, we have to use a slight pause
43 * between clicks. During unittests, calling .click() on a column header twice
44 * without delay causes the sort to get out of order. I have not been able to
45 * reproduce this manually. Maybe some sort of lock is needed...UNTIL THEN!...
46 */
47 $.fn.doubleclick = function(){
48 var $this = $(this);
49 $this.click();
50 setTimeout(function(){
51 $this.click();
52 }, 10);
53 return this;
54 };
55
56 /*
57 * Enable stupid tables at the end for manual testing and experiments
58 */
59 QUnit.done(function(){
60 $("#complex").stupidtable({
61 "date":function(a,b){
62 // Get these into date objects for comparison.
63 var aDate = date_from_string(a);
64 var bDate = date_from_string(b);
65
66 return aDate - bDate;
67 }
68 });
69 $("#basic").stupidtable();
70 $("#basic-colspan").stupidtable();
71 $("#complex-colspan").stupidtable();
72 $("#stability-test").stupidtable();
73 $("#multicolumn-sort-test").stupidtable();
74 $("#qunit-fixture").removeClass("test-hidden");
75 });
76
77
78 /*
79 * Begin tests
80 * NOTE: when to use test vs asyncTest: When you plan on sorting the table and
81 * examining the results, you must use the test_table_state function and have
82 * your test wrapped in asyncTest(). If you wish to only make assertions
83 * regarding the initial state of the html, just use test().
84 */
85
86 // =============================================================================
87 // basic table
88 // =============================================================================
89 test("Basic table initial order", function(){
90 var INT_COLUMN = 0;
91 var FLOAT_COLUMN = 1;
92 var STRING_COLUMN = 2;
93 var expected;
94 var vals;
95
96 var $table = $("#basic");
97
98 expected = ["15", "95", "2", "-53", "195"];
99 vals = get_column_elements($table, INT_COLUMN);
100 ok(_.isEqual(vals, expected));
101
102 expected = ["-.18", "36", "-152.5", "88.5", "-858"];
103 vals = get_column_elements($table, FLOAT_COLUMN);
104 ok(_.isEqual(vals, expected));
105
106 expected = ["banana", "coke", "apple", "zebra", "orange"];
107 vals = get_column_elements($table, STRING_COLUMN);
108 ok(_.isEqual(vals, expected));
109
110 });
111
112 asyncTest("Basic int sort", function(){
113 var INT_COLUMN = 0;
114 var $table = $("#basic");
115 var $table_cols = $table.find("th");
116
117 $table.stupidtable();
118 $table_cols.eq(INT_COLUMN).click();
119
120 test_table_state(function(){
121 var expected = ["-53", "2", "15", "95", "195"];
122 var vals = get_column_elements($table, INT_COLUMN);
123 ok(_.isEqual(vals, expected));
124 });
125 });
126
127 asyncTest("Basic float sort", function(){
128 var FLOAT_COLUMN = 1;
129 var $table = $("#basic");
130 var $table_cols = $table.find("th");
131
132 $table.stupidtable();
133 $table_cols.eq(FLOAT_COLUMN).click();
134
135 test_table_state(function(){
136 var expected = ["-858", "-152.5", "-.18", "36", "88.5"];
137 var vals = get_column_elements($table, FLOAT_COLUMN);
138 ok(_.isEqual(vals, expected));
139 });
140 });
141
142 asyncTest("Basic string sort", function(){
143 var STRING_COLUMN = 2;
144 var $table = $("#basic");
145 var $table_cols = $table.find("th");
146
147 $table.stupidtable();
148 $table_cols.eq(STRING_COLUMN).click();
149
150 test_table_state(function(){
151 var expected = ["apple", "banana", "coke", "orange", "zebra"];
152 var vals = get_column_elements($table, STRING_COLUMN);
153 ok(_.isEqual(vals, expected));
154 });
155 });
156
157 asyncTest("Basic alternating sort", function(){
158 // A double click should cause the sort to reverse
159 var INT_COLUMN = 0;
160 var $table = $("#basic");
161 var $table_cols = $table.find("th");
162 $table.stupidtable();
163
164 $table_cols.eq(INT_COLUMN).doubleclick();
165
166 test_table_state(function(){
167 var expected = ["195", "95", "15", "2", "-53"];
168 var vals = get_column_elements($table, INT_COLUMN);
169 ok(_.isEqual(vals, expected));
170 });
171 });
172
173
174 // =============================================================================
175 // complex table
176 // =============================================================================
177 test("Complex table initial order", function(){
178 var INT_COLUMN = 0;
179 var FLOAT_COLUMN = 1;
180 var STRING_COLUMN = 2;
181 var NOSORT_COLUMN = 3;
182 var DATE_COLUMN = 4;
183 var LETTER_FREQ_COLUMN = 5;
184 var expected;
185 var vals;
186
187 var $table = $("#complex");
188
189 expected = ["15", "95", "2", "-53", "195"];
190 vals = get_column_elements($table, INT_COLUMN);
191 ok(_.isEqual(vals, expected));
192
193 expected = ["-.18", "36", "-152.5", "88.5", "-858"];
194 vals = get_column_elements($table, FLOAT_COLUMN);
195 ok(_.isEqual(vals, expected));
196
197 expected = ["Homer", "purple", "is", "a", "fruit"];
198 vals = get_column_elements($table, STRING_COLUMN);
199 ok(_.isEqual(vals, expected));
200
201 expected = ["arbitrary", "pointless", "silly", "eccentric", "garbage"];
202 vals = get_column_elements($table, NOSORT_COLUMN);
203 ok(_.isEqual(vals, expected));
204
205 expected = ["Sep 15, 2002", "Aug 07, 2004", "Mar 15, 1986", "Feb 27, 2086",
206 "Mar 15, 1986"];
207 vals = get_column_elements($table, DATE_COLUMN);
208 ok(_.isEqual(vals, expected));
209
210 expected = ["E", "T", "A", "O", "I"];
211 vals = get_column_elements($table, LETTER_FREQ_COLUMN);
212 ok(_.isEqual(vals, expected));
213 });
214
215 asyncTest("No data-sort means no sort", function(){
216 var NOSORT_COLUMN = 3;
217 var $table = $("#complex");
218 var $table_cols = $table.find("th");
219
220 $table.stupidtable();
221 $table_cols.eq(NOSORT_COLUMN).click();
222
223 test_table_state(function(){
224 var expected = ["arbitrary", "pointless", "silly", "eccentric", "garbage"];
225 var vals = get_column_elements($table, NOSORT_COLUMN);
226 ok(_.isEqual(vals, expected));
227 });
228 });
229
230 asyncTest("data-sort-by values should be used over text values", function(){
231 var LETTER_FREQ_COLUMN = 5;
232 var $table = $("#complex");
233 var $table_cols = $table.find("th");
234
235 $table.stupidtable();
236 $table_cols.eq(LETTER_FREQ_COLUMN).click();
237
238 test_table_state(function(){
239 var expected = ["E", "T", "A", "O", "I"];
240 var vals = get_column_elements($table, LETTER_FREQ_COLUMN);
241 ok(_.isEqual(vals, expected));
242 });
243 });
244
245 asyncTest("case insensitive string sort", function(){
246 var STRING_COLUMN = 2;
247 var $table = $("#complex");
248 var $table_cols = $table.find("th");
249
250 $table.stupidtable();
251 $table_cols.eq(STRING_COLUMN).click();
252
253 test_table_state(function(){
254 var expected = ["a", "fruit", "Homer", "is", "purple"];
255 var vals = get_column_elements($table, STRING_COLUMN);
256 ok(_.isEqual(vals, expected));
257 });
258 });
259
260 asyncTest("custom sort functions", function(){
261 var DATE_COLUMN = 4;
262 var $table = $("#complex");
263 var $table_cols = $table.find("th");
264
265
266 $table.stupidtable({
267 "date":function(a,b){
268 // Get these into date objects for comparison.
269 var aDate = date_from_string(a);
270 var bDate = date_from_string(b);
271
272 return aDate - bDate;
273 }
274 });
275
276 $table_cols.eq(DATE_COLUMN).click();
277 test_table_state(function(){
278 var expected = ["Mar 15, 1986", "Mar 15, 1986", "Sep 15, 2002", "Aug 07, 2004",
279 "Feb 27, 2086"];
280 var vals = get_column_elements($table, DATE_COLUMN);
281 ok(_.isEqual(vals, expected));
282 });
283
284 });
285
286 asyncTest("default sort direction - DESC", function(){
287 var FLOAT_COLUMN = 1;
288 var $table = $("#complex");
289 var $table_cols = $table.find("th");
290
291 $table_cols.eq(FLOAT_COLUMN).data('sort-default', 'desc');
292 $table.stupidtable();
293 $table_cols.eq(FLOAT_COLUMN).click();
294
295 test_table_state(function(){
296 var expected = ["88.5", "36", "-.18", "-152.5", "-858"];
297 var vals = get_column_elements($table, FLOAT_COLUMN);
298 ok(_.isEqual(vals, expected));
299 });
300 });
301
302 asyncTest("default sort direction - ASC", function(){
303 var FLOAT_COLUMN = 1;
304 var $table = $("#complex");
305 var $table_cols = $table.find("th");
306
307 $table_cols.eq(FLOAT_COLUMN).data('sort-default', 'asc');
308 $table.stupidtable();
309 $table_cols.eq(FLOAT_COLUMN).click();
310
311 test_table_state(function(){
312 var expected = ["-858", "-152.5", "-.18", "36", "88.5"];
313 var vals = get_column_elements($table, FLOAT_COLUMN);
314 ok(_.isEqual(vals, expected));
315 });
316 });
317
318 asyncTest("sorting should preserve tbody classes", function(){
319 var FLOAT_COLUMN = 1;
320 var $table = $("#complex");
321 var $table_cols = $table.find("th");
322 var $tbody = $table.find("tbody");
323 var tbodyStyleBefore = $tbody.attr("style");
324
325 $table.stupidtable();
326
327 // These are initial values hardcoded in the html. We need to make sure they
328 // aren't changed once we click a column.
329 ok($tbody.hasClass('some-tbody-class'));
330 ok(_.isEqual(tbodyStyleBefore, $tbody.attr("style")));
331
332 $table_cols.eq(FLOAT_COLUMN).click();
333
334 test_table_state(function(){
335 var $table = $("#complex");
336 var $tbody = $table.find("tbody");
337 ok($tbody.hasClass('some-tbody-class'));
338 ok(_.isEqual(tbodyStyleBefore, $tbody.attr("style")));
339 });
340 });
341
342 asyncTest("Basic colspan table sort column before colspan column", function(){
343 var LETTER_COLUMN = 0;
344 var $table = $("#basic-colspan");
345 var $table_cols = $table.find("th");
346 var $tbody = $table.find("tbody");
347
348 $table.stupidtable();
349 $table_cols.eq(LETTER_COLUMN).click();
350
351 test_table_state(function(){
352 var expected = ["abc", "bcd", "def"];
353 var vals = get_column_elements($table, LETTER_COLUMN);
354 ok(_.isEqual(vals, expected));
355 });
356 });
357
358 asyncTest("Basic colspan table sort column after colspan column", function(){
359 var NUMBER_COLUMN_TH = 2;
360 var NUMBER_COLUMN = 3;
361 var $table = $("#basic-colspan");
362 var $table_cols = $table.find("th");
363 var $tbody = $table.find("tbody");
364
365 $table.stupidtable();
366 $table_cols.eq(NUMBER_COLUMN_TH).click();
367
368 test_table_state(function(){
369 var expected = ["0", "1", "2"];
370 var vals = get_column_elements($table, NUMBER_COLUMN);
371 ok(_.isEqual(vals, expected));
372 });
373 });
374
375 asyncTest("Basic colspan table sort column on colspan column", function(){
376 var COLSPAN_COLUMN = 1;
377 var $table = $("#basic-colspan");
378 var $table_cols = $table.find("th");
379 var $tbody = $table.find("tbody");
380
381 $table.stupidtable();
382 $table_cols.eq(COLSPAN_COLUMN).click();
383
384 test_table_state(function(){
385 var expected = ["X", "Y", "Z"];
386 var vals = get_column_elements($table, COLSPAN_COLUMN);
387 ok(_.isEqual(vals, expected));
388 });
389 });
390
391 asyncTest("Complex colspan table sort - single click", function(){
392 var NUMBER_COLUMN_TH = 3;
393 var NUMBER_COLUMN = 3;
394 var $table = $("#complex-colspan");
395 var $table_cols = $table.find("th");
396 var $tbody = $table.find("tbody");
397
398 $table.stupidtable();
399 $table_cols.eq(NUMBER_COLUMN_TH).click();
400
401 test_table_state(function(){
402 var expected = ["0", "1", "2"];
403 var vals = get_column_elements($table, NUMBER_COLUMN);
404 ok(_.isEqual(vals, expected));
405 });
406 });
407
408 asyncTest("Complex colspan table sort - double click", function(){
409 var NUMBER_COLUMN_TH = 3;
410 var NUMBER_COLUMN = 3;
411 var $table = $("#complex-colspan");
412 var $table_cols = $table.find("th");
413 var $tbody = $table.find("tbody");
414
415 $table.stupidtable();
416 $table_cols.eq(NUMBER_COLUMN_TH).doubleclick();
417
418 test_table_state(function(){
419 var expected = ["2", "1", "0"];
420 var vals = get_column_elements($table, NUMBER_COLUMN);
421 ok(_.isEqual(vals, expected));
422 });
423 });
424
425 asyncTest("Update sort value - same display and sort values - single click", function(){
426 var INT_COLUMN = 0;
427 var $table = $("#basic");
428 var $table_cols = $table.find("th");
429 var $int_column = $table_cols.eq(INT_COLUMN);
430 var $first_int_td = $table.find("tbody tr td").first();
431
432 $table.stupidtable();
433 ok(_.isEqual($first_int_td.text(), "15"));
434
435 $first_int_td.updateSortVal(200);
436 $first_int_td.text("200");
437 $int_column.click();
438
439 test_table_state(function(){
440 var expected = ["-53", "2", "95", "195", "200"];
441 var vals = get_column_elements($table, INT_COLUMN);
442 ok(_.isEqual(vals, expected));
443 });
444 });
445
446 asyncTest("Update sort value - same display and sort values - double click", function(){
447 var INT_COLUMN = 0;
448 var $table = $("#basic");
449 var $table_cols = $table.find("th");
450 var $int_column = $table_cols.eq(INT_COLUMN);
451 var $first_int_td = $table.find("tbody tr td").first();
452
453 $table.stupidtable();
454 ok(_.isEqual($first_int_td.text(), "15"));
455
456 $first_int_td.updateSortVal(200);
457 $first_int_td.text("200");
458 $int_column.doubleclick();
459
460 test_table_state(function(){
461 var expected = ["200", "195", "95", "2", "-53"];
462 var vals = get_column_elements($table, INT_COLUMN);
463 ok(_.isEqual(vals, expected));
464 });
465 });
466
467 asyncTest("Update sort value - different display and sort value - single click", function(){
468 var LETTER_FREQ_COLUMN = 5;
469 var $table = $("#complex");
470 var $table_cols = $table.find("th");
471 var $letter_freq_col = $table_cols.eq(LETTER_FREQ_COLUMN);
472 var $e_td = $table.find("[data-sort-value=0]");
473
474 $table.stupidtable();
475 $letter_freq_col.click();
476
477 ok(_.isEqual($e_td.text(), "E"));
478 ok(_.isEqual($e_td.data('sort-value'), 0));
479
480 $e_td.updateSortVal(10);
481 $e_td.html("<b>YO</b>");
482
483
484 test_table_state(function(){
485 var expected = ["T", "A", "O", "I", "<b>YO</b>"];
486 var vals = get_column_elements($table, LETTER_FREQ_COLUMN);
487 ok(_.isEqual(vals, expected));
488 });
489 });
490
491 asyncTest("Update sort value - different display and sort value - double click", function(){
492 var LETTER_FREQ_COLUMN = 5;
493 var $table = $("#complex");
494 var $table_cols = $table.find("th");
495 var $letter_freq_col = $table_cols.eq(LETTER_FREQ_COLUMN);
496 var $e_td = $table.find("[data-sort-value=0]");
497
498 $table.stupidtable();
499 $letter_freq_col.doubleclick();
500
501 ok(_.isEqual($e_td.text(), "E"));
502 ok(_.isEqual($e_td.data('sort-value'), 0));
503
504 $e_td.updateSortVal(10);
505 $e_td.html("<b>YO</b>");
506
507
508 test_table_state(function(){
509 var expected = ["<b>YO</b>", "I", "O", "A", "T"];
510 var vals = get_column_elements($table, LETTER_FREQ_COLUMN);
511 ok(_.isEqual(vals, expected));
512 });
513 });
514
515 asyncTest("Update sort value - also updates data-sort-value attribute", function(){
516 var LETTER_FREQ_COLUMN = 5;
517 var $table = $("#complex");
518 var $table_cols = $table.find("th");
519 var $letter_freq_col = $table_cols.eq(LETTER_FREQ_COLUMN);
520 var $e_td = $table.find("[data-sort-value=0]");
521
522 $table.stupidtable();
523 $letter_freq_col.doubleclick();
524
525 ok(_.isEqual($e_td.text(), "E"));
526 ok(_.isEqual($e_td.data('sort-value'), 0));
527
528 $e_td.updateSortVal(10);
529 $e_td.html("<b>YO</b>");
530
531
532 test_table_state(function(){
533 var expected = ["<b>YO</b>", "I", "O", "A", "T"];
534 var vals = get_column_elements($table, LETTER_FREQ_COLUMN);
535 ok(_.isEqual(vals, expected));
536 ok(_.isEqual($e_td.attr('data-sort-value'), "10"));
537 });
538 });
539
540 asyncTest("Update sort value - display value only doesn't add data-sort-value attribute", function(){
541 var INT_COLUMN = 0;
542 var $table = $("#basic");
543 var $table_cols = $table.find("th");
544 var $int_column = $table_cols.eq(INT_COLUMN);
545 var $first_int_td = $table.find("tbody tr td").first();
546
547 $table.stupidtable();
548 ok(_.isEqual($first_int_td.text(), "15"));
549
550 $first_int_td.updateSortVal(200);
551 $first_int_td.text("200");
552 $int_column.click();
553
554 test_table_state(function(){
555 var $first_int_td = $table.find("tbody tr td").first();
556 var expected = ["-53", "2", "95", "195", "200"];
557 var vals = get_column_elements($table, INT_COLUMN);
558
559 ok(_.isEqual(vals, expected));
560 ok(!$first_int_td.attr('data-sort-value'));
561 });
562 });
563
564 asyncTest("Basic individual column sort - no force direction", function(){
565 var FLOAT_COLUMN = 1;
566 var $table = $("#complex");
567 var $table_cols = $table.find("th");
568
569 // Specify a sorting direction
570 $table_cols.eq(FLOAT_COLUMN).data('sort-default', 'desc');
571 $table.stupidtable();
572
573 $table_cols.eq(FLOAT_COLUMN).stupidsort();
574
575 test_table_state(function(){
576 var expected = ["88.5", "36", "-.18", "-152.5", "-858"];
577 var vals = get_column_elements($table, FLOAT_COLUMN);
578 ok(_.isEqual(vals, expected));
579 });
580 });
581
582 asyncTest("Basic individual column sort - force direction", function(){
583 var FLOAT_COLUMN = 1;
584 var $table = $("#complex");
585 var $table_cols = $table.find("th");
586
587 // Specify a sorting direction
588 $table_cols.eq(FLOAT_COLUMN).data('sort-default', 'desc');
589 $table.stupidtable();
590
591 $table_cols.eq(FLOAT_COLUMN).stupidsort('asc');
592
593 test_table_state(function(){
594 var expected = ["-858", "-152.5", "-.18", "36", "88.5"];
595 var vals = get_column_elements($table, FLOAT_COLUMN);
596 ok(_.isEqual(vals, expected));
597 });
598 });
599
600 asyncTest("Sort Stability Testing - 1", function(){
601 // Not all browsers implement stable sorting. View the following for more
602 // information:
603 // http://ofb.net/~sethml/is-sort-stable.html
604 // http://codecoding.com/beware-chrome-array-sort-implementation-is-unstable/
605 var INDEX_COLUMN = 0;
606 var LETTER_COLUMN = 1;
607 var $table = $("#stability-test");
608 var $table_cols = $table.find("th");
609
610 $table.stupidtable();
611 $table_cols.eq(LETTER_COLUMN).click();
612
613 test_table_state(function(){
614 var expected = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
615 var vals = get_column_elements($table, INDEX_COLUMN);
616 ok(_.isEqual(vals, expected));
617 });
618 });
619
620 asyncTest("Sort Stability Testing - 2", function(){
621 // Not all browsers implement stable sorting. View the following for more
622 // information:
623 // http://ofb.net/~sethml/is-sort-stable.html
624 // http://codecoding.com/beware-chrome-array-sort-implementation-is-unstable/
625 var INDEX_COLUMN = 0;
626 var LETTER_COLUMN = 1;
627 var $table = $("#stability-test");
628 var $table_cols = $table.find("th");
629
630 $table.stupidtable();
631 $table_cols.eq(LETTER_COLUMN).doubleclick();
632
633 test_table_state(function(){
634 var expected = ["10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "0"];
635 var vals = get_column_elements($table, INDEX_COLUMN);
636 ok(_.isEqual(vals, expected));
637 });
638 });
639
640 asyncTest("$th passed to after/before tablesort event handlers", function(){
641 var INT_COLUMN = 0;
642 var $table = $("#basic");
643 var $table_cols = $table.find("th");
644 var $th_objs = [];
645
646 $table.stupidtable();
647 var $int_header = $table_cols.eq(INT_COLUMN)
648
649 $table.bind('beforetablesort', function(e, data){
650 $th_objs.push(data.$th);
651 });
652 var afterCalls = 0;
653 $table.bind('aftertablesort', function(e, data){
654 $th_objs.push(data.$th);
655 });
656
657 $int_header.click();
658
659 var get_raw_elements = function(arr){
660 return _.map(arr, function(el){
661 return el[0];
662 });
663 };
664
665 test_table_state(function(){
666 var expected = get_raw_elements([$int_header, $int_header]);
667 var raw_th_objs = get_raw_elements($th_objs);
668 ok(_.isEqual(raw_th_objs, expected));
669 });
670 });
671
672 asyncTest("consective calls to stupidsort in same direction should work (issue #183) ", function(){
673 var INT_COLUMN = 0;
674 var $table = $("#basic");
675 var $table_cols = $table.find("th");
676 var $int_col = $table_cols.eq(INT_COLUMN);
677 var $e_td = $table.find("[data-sort-value=2]");
678
679 $table.stupidtable();
680 $table_cols.eq(INT_COLUMN).stupidsort($.fn.stupidtable.dir.ASC);
681
682 // Verify we have td with the value 2 in the INT column.
683 ok(_.isEqual($e_td.text(), "2"));
684
685 setTimeout(function(){
686 var newval = -100;
687 $e_td.updateSortVal(newval);
688 $e_td.text(newval);
689
690 $table_cols.eq(INT_COLUMN).stupidsort($.fn.stupidtable.dir.ASC);
691
692
693 test_table_state(function(){
694 var expected = ["-100", "-53", "15", "95", "195"];
695 var vals = get_column_elements($table, INT_COLUMN);
696 ok(_.isEqual(vals, expected));
697 });
698
699 }, window.WAIT_TIME_MS);
700 });
701
702 asyncTest("table sorts column onload when specified (issue #180) ", function(){
703 var STRING_COLUMN = 2;
704 var $table = $("#basic-onload");
705 var $table_cols = $table.find("th");
706 var $str_col = $table_cols.eq(STRING_COLUMN);
707
708 $table.stupidtable();
709
710 test_table_state(function(){
711 var expected = ["apple", "banana", "coke", "orange", "zebra"];
712 var vals = get_column_elements($table, STRING_COLUMN);
713 ok(_.isEqual(vals, expected));
714 });
715 });
716
717 asyncTest("test should_redraw setting", function(){
718 var INT_COLUMN = 0;
719 var $table = $("#basic");
720 var $table_cols = $table.find("th");
721
722 $table.stupidtable_settings({
723 should_redraw: function(sort_info){
724 return false;
725 }
726 });
727 $table.stupidtable();
728 $table_cols.eq(INT_COLUMN).click();
729
730 test_table_state(function(){
731 // Redrawing will never occur so we should expect the initial order to
732 // be the current column order.
733 var expected = ["15", "95", "2", "-53", "195"];
734 var vals = get_column_elements($table, INT_COLUMN);
735 ok(_.isEqual(vals, expected));
736 });
737 });
738
739 asyncTest("test will_manually_build_table setting - 1", function(){
740 var INT_COLUMN = 0;
741 var $table = $("#basic");
742 var $table_cols = $table.find("th");
743 var $int_column = $table_cols.eq(INT_COLUMN);
744 var $first_int_td = $table.find("tbody tr td").first();
745
746 $table.stupidtable_settings({
747 will_manually_build_table: true
748 });
749 $table.stupidtable();
750 ok(_.isEqual($first_int_td.text(), "15"));
751
752 $first_int_td.updateSortVal(200);
753 $first_int_td.text("200");
754 $int_column.click();
755
756 test_table_state(function(){
757 // Since we didn't manually build the table after updating the value, we
758 // shouldn't expect the table to sort correctly. 200 will still function
759 // as 15
760 var expected = ["-53", "2", "200", "95", "195"];
761 var vals = get_column_elements($table, INT_COLUMN);
762 ok(_.isEqual(vals, expected));
763 });
764 });
765
766 asyncTest("test will_manually_build_table setting - 2", function(){
767 var INT_COLUMN = 0;
768 var $table = $("#basic");
769 var $table_cols = $table.find("th");
770 var $int_column = $table_cols.eq(INT_COLUMN);
771 var $first_int_td = $table.find("tbody tr td").first();
772
773 $table.stupidtable_settings({
774 will_manually_build_table: true
775 });
776 $table.stupidtable();
777 ok(_.isEqual($first_int_td.text(), "15"));
778
779 $first_int_td.updateSortVal(200);
780 $first_int_td.text("200");
781
782 // Rebuild the table
783 $table.stupidtable_build();
784 $int_column.click();
785
786 test_table_state(function(){
787 // Since we didn't manually build the table after updating the value, we
788 // shouldn't expect the table to sort correctly. 200 will still function
789 // as 15
790 var expected = ["-53", "2", "95", "195", "200"];
791 var vals = get_column_elements($table, INT_COLUMN);
792 ok(_.isEqual(vals, expected));
793 });
794 });
795
796 asyncTest("test will_manually_build_table setting - 3", function(){
797 var INT_COLUMN = 0;
798 var $table = $("#basic");
799 var $table_cols = $table.find("th");
800 var $int_column = $table_cols.eq(INT_COLUMN);
801 var $first_int_td = $table.find("tbody tr td").first();
802
803 $table.stupidtable_settings({
804 will_manually_build_table: false
805 });
806 $table.stupidtable();
807 ok(_.isEqual($first_int_td.text(), "15"));
808
809 $first_int_td.updateSortVal(200);
810 $first_int_td.text("200");
811 $int_column.click();
812
813 test_table_state(function(){
814 // Since we didn't manually build the table after updating the value, we
815 // shouldn't expect the table to sort correctly. 200 will still function
816 // as 15
817 var expected = ["-53", "2", "95", "195", "200"];
818 var vals = get_column_elements($table, INT_COLUMN);
819 ok(_.isEqual(vals, expected));
820 });
821 });
822
823 test("Multicolumn table initial order", function(){
824 var INT_COLUMN = 0;
825 var FLOAT_COLUMN = 1;
826 var STRING_COLUMN = 2;
827 var expected;
828 var vals;
829
830 var $table = $("#multicolumn-sort-test");
831
832 expected = ["1", "1", "2", "0", "1", "2", "0", "0", "0", "1", "2", "3", "2"];
833 vals = get_column_elements($table, INT_COLUMN);
834 ok(_.isEqual(vals, expected));
835
836 expected = ["10.0", "10.0", "10.0", "20.0", "30.0", "30.0", "10.0", "20.0", "10.0", "20.0", "10.0", "30.0", "30.0"];
837 vals = get_column_elements($table, FLOAT_COLUMN);
838 ok(_.isEqual(vals, expected));
839
840 expected = ["a", "a", "b", "c", "b", "a", "b", "a", "c", "a", "b", "a", "b"];
841 vals = get_column_elements($table, STRING_COLUMN);
842 ok(_.isEqual(vals, expected));
843
844 });
845
846 asyncTest("Basic multicolumn sort", function(){
847 var INT_COLUMN = 0;
848 var FLOAT_COLUMN = 1;
849 var STRING_COLUMN = 2;
850 var $table = $("#multicolumn-sort-test");
851 var $table_cols = $table.find("th");
852
853 $table.stupidtable();
854 $table_cols.eq(INT_COLUMN).click();
855
856 test_table_state(function(){
857 var expected;
858 var vals;
859
860 expected = ["0", "0", "0", "0", "1", "1", "1", "1", "2", "2", "2", "2", "3"];
861 vals = get_column_elements($table, INT_COLUMN);
862 ok(_.isEqual(vals, expected));
863
864 expected = ["10.0", "10.0", "20.0", "20.0", "10.0", "10.0", "20.0", "30.0", "10.0", "10.0", "30.0", "30.0", "30.0"];
865 vals = get_column_elements($table, FLOAT_COLUMN);
866 ok(_.isEqual(vals, expected));
867
868 expected = ["b", "c", "a", "c", "a", "a", "a", "b", "b", "b", "a", "b", "a"];
869 vals = get_column_elements($table, STRING_COLUMN);
870 ok(_.isEqual(vals, expected));
871 });
872 });
873
874 }); //jQuery