UNPKG

3.16 kBJavaScriptView Raw
1(function(win, TableFilter) {
2
3 var tf = new TableFilter('demo', {
4 base_path: '../dist/tablefilter/',
5 linked_filters: true,
6 col_0: 'checklist',
7 col_1: 'multiple',
8 load_filters_on_demand: true
9 });
10
11 tf.init();
12
13 var checkList = tf.feature('checkList');
14 var cont0 = checkList.containers[0];
15 var cont1 = tf.getFilterElement(1);
16
17 module('Sanity checks');
18 test('Linked filters feature', function() {
19 deepEqual(tf instanceof TableFilter, true, 'TableFilter instantiated');
20 deepEqual(tf.linkedFilters, true, 'Linked filters enabled');
21 deepEqual(tf.loadFltOnDemand, true, 'Load filters on demand setting');
22 });
23
24 test('Can generate filter options', function() {
25 // setup
26 var ct = 0;
27 tf.emitter.on(['after-populating-filter'], checkGeneratedFilters);
28
29 function checkGeneratedFilters(tf, colIndex, flt) {
30 ct++;
31
32 if (colIndex === 0) {
33 // assert
34 deepEqual(flt.getElementsByTagName('li').length, 3,
35 'Filter 0 options length');
36 }
37 if (colIndex === 1) {
38 // assert
39 deepEqual(flt.options.length, 7, 'Filter 1 options length');
40 }
41 if (ct === 2) {
42 tf.emitter.off(['after-populating-filter'],
43 checkGeneratedFilters);
44 canFilterLinkedFilters();
45 }
46 }
47
48 // act
49 var evObj = document.createEvent('HTMLEvents');
50 evObj.initEvent('click', true, true);
51 cont0.dispatchEvent(evObj);
52 cont1.focus();
53 });
54
55 function canFilterLinkedFilters() {
56 test('Can filter linked filters', function() {
57 // act
58 tf.activateFilter(0);
59 tf.setFilterValue(0, 'Adelaide');
60 tf.filter();
61
62 // assert
63 deepEqual(cont0.getElementsByTagName('li').length, 2,
64 'Filter 0 options length');
65 deepEqual(cont1.options.length, 4, 'Filter 1 options length');
66 });
67
68 test('Can filter linked filters 2', function() {
69 // act
70 tf.activateFilter(1);
71 tf.setFilterValue(1, 'Brisbane');
72 tf.filter();
73
74 // assert
75 deepEqual(cont0.getElementsByTagName('li').length, 2,
76 'Filter 0 options length');
77 deepEqual(cont1.options.length, 2, 'Filter 1 options length');
78 });
79
80 test('Can clear filters', function() {
81 // act
82 tf.clearFilters();
83
84 // assert
85 deepEqual(cont0.getElementsByTagName('li').length, 3,
86 'Filter 0 options length');
87 deepEqual(cont1.options.length, 7, 'Filter 1 options length');
88 });
89
90 test('Destroy TableFilter', function() {
91 // act
92 tf.destroy();
93
94 // assert
95 deepEqual(tf.isInitialized(), false, 'Filters removed');
96 });
97 }
98
99})(window, TableFilter);