UNPKG

2.19 kBJavaScriptView Raw
1
2var tag = function (elm, tag){ return elm.getElementsByTagName(tag); };
3
4var tf = new TableFilter('demo', {
5 base_path: '../dist/tablefilter/',
6 extensions:[{
7 name: 'sort',
8 types:[
9 'String',
10 'enforceinteger',
11 'String',
12 'String',
13 'EU',
14 'US',
15 'String',
16 'dmydate',
17 'mdydate'
18 ],
19 on_sort_loaded: function(tf, sort){
20 sort.addSortType('enforceinteger', customIntegerSorter);
21 startTest(tf, sort);
22 }
23 }]
24});
25
26//Custom sorter function
27function customIntegerSorter(val){
28 var m = val.match(/\d+/);
29 return parseInt(m[0], 10);
30}
31
32tf.init();
33
34function startTest(tf, sort){
35 module('Sanity checks');
36 test('Sort extension', function() {
37 notEqual(sort, null, 'Sort instanciated');
38 deepEqual(sort.sorted, false, 'Table not sorted');
39 deepEqual(sort.initialized, true, 'Sort initialized');
40 });
41
42 module('UI elements');
43 test('Sort UI elements', function() {
44 var th = tf.getHeaderElement(0),
45 indicator = tag(th, 'img'),
46 validRows = tf.getValidRows(true);
47
48 deepEqual(indicator.length, 1, 'Sort indicator in header element');
49 deepEqual(
50 (tf.dom().rows[validRows[0]].cells[1]).innerHTML,
51 'AUY78',
52 'First custom key cell text before sorting');
53 });
54
55 test('Sort behaviour', function() {
56 var validRows = tf.getValidRows(true);
57 sort.sortByColumnIndex(1);
58
59 deepEqual(sort.sorted, true, 'Table column sorted');
60 deepEqual(
61 (tf.dom().rows[validRows[0]].cells[1]).innerHTML,
62 'QT1',
63 'First custom key cell text after sorting');
64 });
65
66 module('Destroy and re-init');
67 test('Remove sort', function() {
68 sort.destroy();
69 var th = tf.getHeaderElement(0),
70 indicator = tag(th, 'img');
71 deepEqual(sort.initialized, false, 'Sort is removed');
72 deepEqual(indicator.length, 0, 'Sort indicator is removed');
73 });
74}