UNPKG

8.26 kBJavaScriptView Raw
1(function(win, TableFilter){
2 // TODO: add sort to test it with different column types
3 var tf = new TableFilter('demo', {
4 base_path: '../dist/tablefilter/',
5 col_types: [
6 null, null, null,
7 { type: 'formatted-number', decimal: ',', thousands: ','},
8 'formatted-number', null,
9 { type: 'date', locale: 'fr', },
10 { type: 'date', locale: 'en', format: '{dd}-{MM}-{yyyy|yy}' },
11 { type: 'date', locale: 'en', format: ['{dd}-{months}-{yyyy|yy}'] },
12 'IpAddress',
13 {
14 type: 'date', locale: 'en',
15 format: ['{yyyy|yy}-{MM}-{dd} {HH}:{mm}:{ss}']
16 }
17 ]
18 });
19 tf.init();
20 window.tf = tf;
21
22 module('Sanity checks');
23 test('Data types', function() {
24 deepEqual(tf instanceof TableFilter, true, 'TableFilter instanciated');
25 deepEqual(
26 tf.hasType(3, ['formatted-number']) &&
27 tf.hasType(4, ['formatted-number']),
28 true, 'Has number column types'
29 );
30 deepEqual(
31 tf.hasType(6, ['date']) &&
32 tf.hasType(7, ['date']) &&
33 tf.hasType(8, ['date']),
34 true, 'Has date column types'
35 );
36 });
37
38 module('Data types filtering');
39 test('Can filter a column with a string', function() {
40 // act
41 tf.setFilterValue(0, 'carl');
42 tf.filter();
43
44 // assert
45 deepEqual(tf.getValidRows(), [14, 18], 'Expected rows');
46
47 });
48
49 test('Can filter a EU formatted number', function() {
50 // setup
51 tf.clearFilters();
52
53 // act
54 tf.setFilterValue(3, '1.836,09');
55 tf.filter();
56
57 // assert
58 deepEqual(tf.getValidRows(), [6], 'Expected rows');
59 });
60
61 test('Can filter a EU formatted number column with a number', function() {
62 // setup
63 tf.clearFilters();
64
65 // act
66 tf.setFilterValue(3, 3876);
67 tf.filter();
68
69 // assert
70 deepEqual(tf.getValidRows(), [14], 'Expected rows');
71 });
72
73 test('Can filter a EU formatted number column with a number without ' +
74 'thousands separator', function() {
75 // setup
76 tf.clearFilters();
77
78 // act
79 tf.setFilterValue(3, '1393,52');
80 tf.filter();
81
82 // assert
83 deepEqual(tf.getValidRows(), [13], 'Expected rows');
84 });
85
86 test('Can filter a EU formatted number column with a number without ' +
87 'decimals', function() {
88 // setup
89 tf.clearFilters();
90
91 // act
92 tf.setFilterValue(3, '2.805');
93 tf.filter();
94
95 // assert
96 deepEqual(tf.getValidRows(), [7], 'Expected rows');
97 });
98
99 test('Can filter a formatted number', function() {
100 // setup
101 tf.clearFilters();
102
103 // act
104 tf.setFilterValue(4, '1,836.09');
105 tf.filter();
106
107 // assert
108 deepEqual(tf.getValidRows(), [6], 'Expected rows');
109 });
110
111 test('Can filter a number', function() {
112 // setup
113 tf.clearFilters();
114
115 // act
116 tf.setFilterValue(4, 1836.09);
117 tf.filter();
118
119 // assert
120 deepEqual(tf.getValidRows(), [6], 'Expected rows');
121 });
122
123 test('Can filter a formatted number column with a number', function() {
124 // setup
125 tf.clearFilters();
126
127 // act
128 tf.setFilterValue(4, 3876);
129 tf.filter();
130
131 // assert
132 deepEqual(tf.getValidRows(), [14], 'Expected rows');
133 });
134
135 test('Can filter a formatted number column with a number without ' +
136 'thousands separator', function() {
137 // setup
138 tf.clearFilters();
139
140 // act
141 tf.setFilterValue(4, '1393.52');
142 tf.filter();
143
144 // assert
145 deepEqual(tf.getValidRows(), [13], 'Expected rows');
146 });
147
148 test('Can filter a formatted number column with a number without ' +
149 'decimals', function() {
150 // setup
151 tf.clearFilters();
152
153 // act
154 tf.setFilterValue(4, '2,805');
155 tf.filter();
156
157 // assert
158 deepEqual(tf.getValidRows(), [7], 'Expected rows');
159 });
160
161 test('Can filter a EU formatted date column', function() {
162 // setup
163 tf.clearFilters();
164
165 // act
166 tf.setFilterValue(6, '14/7/1994');
167 tf.filter();
168
169 // assert
170 deepEqual(tf.getValidRows(), [16], 'Expected rows');
171 });
172
173 test('Can filter a EU formatted date column with different date separator',
174 function() {
175 // setup
176 tf.clearFilters();
177
178 // act
179 tf.setFilterValue(6, '20-10-97');
180 tf.filter();
181
182 // assert
183 deepEqual(tf.getValidRows(), [17], 'Expected rows');
184 });
185
186 test('Can filter a formatted date column', function() {
187 // setup
188 tf.clearFilters();
189
190 // act
191 tf.setFilterValue(7, '7/14/1994');
192 tf.filter();
193
194 // assert
195 deepEqual(tf.getValidRows(), [16], 'Expected rows');
196 });
197
198 test('Can filter a formatted date column with different date separator',
199 function() {
200 // setup
201 tf.clearFilters();
202
203 // act
204 tf.setFilterValue(7, '10-20-97');
205 tf.filter();
206
207 // assert
208 deepEqual(tf.getValidRows(), [17], 'Expected rows');
209 });
210
211 test('Can filter a dd-MMM-yyy formatted date column', function() {
212 // setup
213 tf.clearFilters();
214
215 // act
216 tf.setFilterValue(8, '3-Jul-2002');
217 tf.filter();
218
219 // assert
220 deepEqual(tf.getValidRows(), [8], 'Expected rows');
221 });
222
223 test('Can filter a dd-MMM-yyy formatted date column with different date ' +
224 'separator', function() {
225 // setup
226 tf.clearFilters();
227
228 // act
229 tf.setFilterValue(8, '25.Mar.2000');
230 tf.filter();
231
232 // assert
233 deepEqual(tf.getValidRows(), [4], 'Expected rows');
234 });
235
236 test('Can filter an IP address column', function() {
237 // setup
238 tf.clearFilters();
239
240 // act
241 tf.setFilterValue(9, '219.115.156.145');
242 tf.filter();
243
244 // assert
245 deepEqual(tf.getValidRows(), [8], 'Expected rows');
246 });
247
248 test('Can filter an IP address column with a truncated IP address',
249 function() {
250 // setup
251 tf.clearFilters();
252
253 // act
254 tf.setFilterValue(9, '219.115.15');
255 tf.filter();
256
257 // assert
258 deepEqual(tf.getValidRows(), [4, 8, 14], 'Expected rows');
259 });
260
261 test('Can filter datetime format', function() {
262 // setup
263 tf.clearFilters();
264
265 // act
266 tf.setFilterValue(10, '2006-06-03 11:59:48');
267 tf.filter();
268
269 // assert
270 deepEqual(tf.getValidRows(), [8], 'Expected rows');
271 });
272
273 test('Can filter datetime format with operator', function() {
274 // setup
275 tf.clearFilters();
276
277 // act
278 tf.setFilterValue(10, '>2006-06-03 11:59:48');
279 tf.filter();
280
281 // assert
282 deepEqual(tf.getValidRows().length, 7, 'Expected rows');
283 });
284
285 module('Locale helpers');
286 test('Can get decimal separator for given column from config', function() {
287 // act
288 var result = tf.getDecimal(3);
289
290 // assert
291 deepEqual(result, ',', 'Decimal separator for given column');
292 });
293
294 test('Can get decimal separator for given column from global setting',
295 function() {
296 // act
297 var result = tf.getDecimal(1);
298
299 // assert
300 deepEqual(result, '.', 'Decimal separator for given column');
301 });
302
303 module('Tear-down');
304 test('can destroy TableFilter DOM elements', function() {
305 tf.destroy();
306 deepEqual(tf.isInitialized(), false, 'Filters removed');
307 });
308
309})(window, TableFilter);