UNPKG

2.26 kBJavaScriptView Raw
1(function (win, TableFilter) {
2
3 var tf = new TableFilter('demo', {
4 base_path: '../dist/tablefilter/',
5 grid_layout: true,
6 linked_filters: true,
7 col_0: 'select',
8 col_1: 'checklist'
9 });
10
11 tf.init();
12 triggerEvents();
13
14 module('Sanity checks');
15 test('Linked filters feature', function () {
16 deepEqual(tf instanceof TableFilter, true, 'TableFilter instantiated');
17 deepEqual(tf.linkedFilters, true, 'Linked filters enabled');
18 });
19
20 function triggerEvents() {
21 tf.emitter.on(['after-populating-filter'], checkFilters);
22 var flt0 = tf.getFilterElement(0);
23 var flt1 = tf.getFilterElement(1);
24
25 var evObj = document.createEvent('HTMLEvents');
26 evObj.initEvent('change', true, true);
27
28 var evObj1 = document.createEvent('HTMLEvents');
29 evObj1.initEvent('click', false, true);
30
31 tf.setFilterValue(0, 'Sydney');
32 flt0.dispatchEvent(evObj);
33 tf.setFilterValue(1, 'Adelaide');
34 flt1.querySelectorAll('input')[1].dispatchEvent(evObj1);
35 }
36
37 function checkFilters(tf, colIndex, flt) {
38 module('behaviour');
39 test('Can filter', function () {
40 if (colIndex === 0) {
41 deepEqual(flt.options.length, 2, 'Filter 0 options number');
42 }
43 if (colIndex === 1) {
44 deepEqual(flt.getElementsByTagName('li').length, 2,
45 'Filter 1 options number');
46 testClearFilters();
47 }
48 });
49 }
50
51 // Tests for https://github.com/koalyptus/TableFilter/pull/42 issue
52 function testClearFilters() {
53 test('Check clear filters functionality', function () {
54 tf.clearFilters();
55
56 deepEqual(tf.getFilterableRowsNb(), 7,
57 'Nb of filterable rows after filters are cleared');
58 });
59
60 testDestroy();
61 }
62
63 function testDestroy() {
64 test('Tear down', function () {
65 tf.destroy();
66
67 deepEqual(tf.isInitialized(), false, 'Filters removed');
68 });
69 tf.emitter.off(['after-populating-filter'], checkFilters);
70 }
71
72})(window, TableFilter);