1 |
|
2 | var tag = function (elm, tag){ return elm.getElementsByTagName(tag); };
|
3 | var outputBefore = null;
|
4 | var outputAfter = null;
|
5 |
|
6 | var tf = new TableFilter('demo', {
|
7 | base_path: '../dist/tablefilter/',
|
8 | extensions:[{
|
9 | name: 'sort',
|
10 | types: ['string','string','number','number','number'],
|
11 | on_sort_loaded: startSimple,
|
12 | on_before_sort: function(o, col){
|
13 | outputBefore = [o, col];
|
14 | },
|
15 | on_after_sort: function(o, col){
|
16 | outputAfter = [o, col];
|
17 | }
|
18 | }]
|
19 | });
|
20 | tf.init();
|
21 |
|
22 | var tf1 = new TableFilter('demo2', {
|
23 | base_path: '../dist/tablefilter/',
|
24 | paging: true,
|
25 | extensions:[{
|
26 | name: 'sort',
|
27 | types: ['string','string','number','number','number'],
|
28 | on_sort_loaded: startPaging
|
29 | }]
|
30 | });
|
31 | tf1.init();
|
32 |
|
33 | function startSimple(tf, sort){
|
34 |
|
35 | module('Sanity checks');
|
36 | test('Sort extension', function() {
|
37 | notEqual(sort, null, 'Sort instanciated');
|
38 | deepEqual(sort.stt instanceof SortableTable, true, 'Sort type');
|
39 | deepEqual(sort.sorted, false, 'Table not sorted');
|
40 | deepEqual(sort.initialized, true, 'Sort initialized');
|
41 | });
|
42 |
|
43 | module('UI elements');
|
44 | test('Sort UI elements', function() {
|
45 | var th = tf.getHeaderElement(0),
|
46 | indicator = tag(th, 'img');
|
47 |
|
48 | deepEqual(indicator.length, 1, 'Sort indicator in header element');
|
49 | });
|
50 |
|
51 | module('Behaviour');
|
52 | test('Sort sanity checks', function() {
|
53 | sort.sortByColumnIndex(0);
|
54 |
|
55 | deepEqual(sort.sorted, true, 'Table column sorted');
|
56 | deepEqual(
|
57 | outputBefore[0] instanceof TableFilter,
|
58 | true,
|
59 | 'on_before_sort callback param 0 verified'
|
60 | );
|
61 | deepEqual(outputBefore[1], 0,
|
62 | 'on_before_sort callback param 1 verified'
|
63 | );
|
64 | deepEqual(
|
65 | outputAfter[0] instanceof TableFilter,
|
66 | true,
|
67 | 'on_after_sort callback param 0 verified'
|
68 | );
|
69 | deepEqual(outputAfter[1], 0,
|
70 | 'on_after_sort callback param 1 verified'
|
71 | );
|
72 | });
|
73 |
|
74 | test('Sort API', function() {
|
75 | sort.sortByColumnIndex(1, true);
|
76 | deepEqual(tf.getColValues(1)[0], 'Perth', 'Descending sort');
|
77 |
|
78 | sort.sortByColumnIndex(1, false);
|
79 | deepEqual(tf.getColValues(1)[0], 'Adelaide', 'Descending sort');
|
80 | });
|
81 |
|
82 | module('Destroy and re-init');
|
83 | test('Remove sort', function() {
|
84 | sort.destroy();
|
85 | var th = tf.getHeaderElement(0),
|
86 | indicator = tag(th, 'img');
|
87 | deepEqual(sort.initialized, false, 'Sort is removed');
|
88 | deepEqual(indicator.length, 0, 'Sort indicator is removed');
|
89 | });
|
90 | }
|
91 |
|
92 | function startPaging(tf, sort){
|
93 |
|
94 | module('Sanity checks');
|
95 | test('Sort extension', function() {
|
96 | notEqual(sort, null, 'Sort instanciated');
|
97 | deepEqual(sort.stt instanceof SortableTable, true, 'Sort type');
|
98 | deepEqual(sort.sorted, false, 'Table not sorted');
|
99 | deepEqual(sort.initialized, true, 'Sort initialized');
|
100 | deepEqual(tf.paging, true, 'Table is paged');
|
101 | });
|
102 |
|
103 | module('UI elements');
|
104 | test('Sort UI elements', function() {
|
105 | var th = tf.getHeaderElement(0),
|
106 | indicator = tag(th, 'img');
|
107 |
|
108 | deepEqual(indicator.length, 1, 'Sort indicator in header element');
|
109 | });
|
110 |
|
111 | test('Sort behaviour', function() {
|
112 | sort.sortByColumnIndex(0);
|
113 |
|
114 | deepEqual(sort.sorted, true, 'Table column sorted');
|
115 | });
|
116 |
|
117 | module('Destroy and re-init');
|
118 | test('Remove sort', function() {
|
119 | sort.destroy();
|
120 | var th = tf.getHeaderElement(0),
|
121 | indicator = tag(th, 'img');
|
122 | deepEqual(sort.initialized, false, 'Sort is removed');
|
123 | deepEqual(indicator.length, 0, 'Sort indicator is removed');
|
124 | });
|
125 |
|
126 | }
|