UNPKG

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