UNPKG

5.94 kBJavaScriptView Raw
1
2var id = function (id){ return document.getElementById(id); };
3var tf = new TableFilter('demo', {
4 base_path: '../dist/tablefilter/',
5 col_3: 'checklist',
6 load_filters_on_demand: false
7});
8tf.init();
9
10var checkList = tf.feature('checkList');
11module('Sanity checks');
12test('CheckList component', function() {
13 deepEqual(typeof checkList, 'object', 'CheckList instanciated');
14 deepEqual(checkList.containers instanceof Array, true,
15 'Type of containers property');
16});
17
18module('UI elements');
19test('CheckList UI elements', function() {
20 var flt = id(tf.fltIds[3]);
21 notEqual(flt, null, 'CheckList UL element');
22 deepEqual(flt.firstChild.nodeName, 'LI',
23 'First CheckList option element name');
24 deepEqual(flt.childNodes.length, 8, 'number of checklist options');
25});
26
27module('Behaviour');
28test('Can filter on checkList item click', function() {
29 var flt3 = tf.getFilterElement(3);
30
31 var evObj = document.createEvent('HTMLEvents');
32 evObj.initEvent('click', true, true);
33 tf.setFilterValue(3, '1.1');
34 flt3.querySelectorAll('input')[1].dispatchEvent(evObj);
35
36 deepEqual(tf.getValidRows().length, 1, 'Table filtered');
37 deepEqual(tf.getFilteredData()[0][1][3], '1.1', 'Matched value');
38});
39test('Can refresh all drop-down filters', function() {
40 //setup
41 tf.clearFilters();
42 var build = checkList.build;
43 var hit = 0;
44 checkList.build = function() { hit++; };
45
46 //act
47 checkList.refreshAll();
48
49 //assert
50 deepEqual(hit, 1, 'build method called');
51
52 checkList.build = build;
53});
54test('Can select options', function() {
55 tf.clearFilters();
56 var flt1 = id(tf.fltIds[3]);
57
58 checkList.selectOptions(3, ['1.4', '.6']);
59
60 notEqual(flt1.getAttribute('value').indexOf('1.4'), -1, 'Option selected');
61 notEqual(flt1.getAttribute('value').indexOf('.6'), -1, 'Option selected');
62});
63test('Can get selected values', function() {
64 //setup
65 var values = ['.6', '1.4'];
66 tf.clearFilters();
67 tf.setFilterValue(3, values);
68
69 //act
70 var result = checkList.getValues(3);
71
72 //assert
73 deepEqual(values, result);
74});
75test('Can return values when no selected options', function() {
76 //setup
77 tf.clearFilters();
78
79 //act
80 var result = checkList.getValues(3);
81
82 //assert
83 deepEqual([''], result);
84});
85test('Can return values checklist element has no value attribute', function() {
86 //setup
87 tf.getFilterElement(3).removeAttribute('value');
88
89 //act
90 var result = checkList.getValues(3);
91
92 //assert
93 deepEqual([''], result);
94});
95
96// Issue 113, addressing option sorting for numeric values
97module('Options sorting');
98test('Can sort options', function() {
99 tf.clearFilters();
100 tf.destroy();
101 tf = new TableFilter('demo', {
102 base_path: '../dist/tablefilter/',
103 col_2: 'checklist',
104 col_3: 'checklist',
105 col_4: 'checklist',
106 col_types: ['string', 'string', 'number', 'number', 'number'],
107 sort_num_asc: [2, 3],
108 sort_num_desc: [4]
109 });
110 tf.init();
111
112 var flt2 = id(tf.fltIds[2]);
113 var flt3 = id(tf.fltIds[3]);
114 var flt4 = id(tf.fltIds[4]);
115
116 deepEqual(
117 flt2.getElementsByTagName('li')[1].firstChild.firstChild.value,
118 '286',
119 'First option value for column 2'
120 );
121 deepEqual(
122 flt2.getElementsByTagName('li')[7].firstChild.firstChild.value,
123 '2781',
124 'Last option value for column 2'
125 );
126 deepEqual(
127 flt3.getElementsByTagName('li')[1].firstChild.firstChild.value,
128 '.6',
129 'First option value for column 3'
130 );
131 deepEqual(
132 flt3.getElementsByTagName('li')[7].firstChild.firstChild.value,
133 '3.1',
134 'Last option value for column 3'
135 );
136 deepEqual(
137 flt4.getElementsByTagName('li')[1].firstChild.firstChild.value,
138 '40',
139 'First option value for column 4'
140 );
141 deepEqual(
142 flt4.getElementsByTagName('li')[7].firstChild.firstChild.value,
143 '4.3',
144 'Last option value for column 4'
145 );
146});
147
148// Issue 238, empty and non-empty options cannot be selected
149module('Empty and non-empty options');
150test('Can select empty and non-empty options', function() {
151 // setup
152 tf.clearFilters();
153 tf.destroy();
154 tf = new TableFilter('demo', {
155 base_path: '../dist/tablefilter/',
156 col_2: 'checklist',
157 col_3: 'checklist',
158 enable_empty_option: true,
159 enable_non_empty_option: true
160 });
161 tf.init();
162 var checkList = tf.feature('checkList');
163 var flt2 = tf.getFilterElement(2);
164 var flt3 = tf.getFilterElement(3);
165
166 // act
167 checkList.selectOptions(2, [tf.emOperator, tf.nmOperator]);
168 checkList.selectOptions(3, [tf.emOperator, tf.nmOperator, '1.4']);
169
170 // assert
171 deepEqual(checkList.getValues(2), [tf.emOperator, tf.nmOperator],
172 'Filter 2 empty and non-empty options selected');
173 notEqual(flt2.getAttribute('value').indexOf(tf.emOperator), -1,
174 'Filter 2 options values attribute');
175 notEqual(flt2.getAttribute('value').indexOf(tf.nmOperator), -1,
176 'Filter 2 options values attribute');
177 deepEqual(checkList.getValues(3), [tf.emOperator, tf.nmOperator, '1.4'],
178 'Filter 3 options selected');
179 notEqual(flt3.getAttribute('value').indexOf(tf.emOperator), -1,
180 'Filter 3 options values attribute');
181 notEqual(flt3.getAttribute('value').indexOf(tf.nmOperator), -1,
182 'Filter 3 options values attribute');
183});
184
185module('Tear down');
186test('TableFilter removed', function() {
187 tf.destroy();
188 deepEqual(id(tf.fltIds[3]), null, 'CheckList UL element');
189 deepEqual(
190 tf.feature('checkList').initialized,
191 false,
192 'CheckList not initialised'
193 );
194});