UNPKG

3.64 kBJavaScriptView Raw
1(function(win, TableFilter) {
2
3 var tf = new TableFilter('demo', {
4 base_path: '../dist/tablefilter/'
5 });
6 tf.init();
7
8 module('Table 1: sanity checks');
9 test('TableFilter object', function() {
10 deepEqual(tf instanceof TableFilter, true, 'TableFilter instanciated');
11 deepEqual(tf.id, 'demo', 'id check');
12 deepEqual(tf.filtersRowIndex, 0, 'Filters row index');
13 deepEqual(tf.getCellsNb(), 5, 'filters type collection length');
14 });
15
16 module('Table 1: DOM tests');
17 test('Filters row', function() {
18 equal(
19 tf.dom().rows[0].className,
20 'fltrow',
21 'Filters row CSS class name'
22 );
23 equal(tf.getFilterElement(0).nodeName, 'INPUT', 'Filter DOM element');
24 deepEqual(
25 tf.dom().scrollWidth === tf.dom().clientWidth,
26 true,
27 'Horizontal scrollbar is not displayed'
28 );
29 });
30
31 var tf1 = new TableFilter(
32 document.querySelector('.test'),
33 {
34 base_path: '../dist/tablefilter/',
35 responsive: true,
36 filters_row_index: 1,
37 btn: true
38 }
39 );
40 tf1.init();
41
42 var btn = document.querySelector('.' + tf1.btnCssClass);
43
44 module('Table 2: sanity checks');
45 test('TableFilter instance', function() {
46 notEqual(tf1.id, null, 'id check');
47 deepEqual(tf1.dom().classList.contains(tf1.prfxResponsive), true,
48 'Responsive CSS class');
49 equal(tf1.filtersRowIndex, 1, 'Filters row index');
50 deepEqual(tf1.getCellsNb(), 5, 'filters type collection length');
51 });
52
53 module('Table 2: DOM tests');
54 test('Filters row', function() {
55 equal(
56 tf1.dom().rows[1].className,
57 'fltrow',
58 'Filters row CSS class name'
59 );
60 equal(tf1.getFilterElement(0).nodeName, 'INPUT', 'Filter DOM element');
61 deepEqual(
62 tf1.dom().scrollWidth > tf1.dom().clientWidth,
63 true,
64 'Horizontal scrollbar is displayed'
65 );
66 });
67 test('Filter button', function(){
68 notEqual(btn, null, 'Button exists');
69 deepEqual(btn.nodeName, 'INPUT', 'Expected element');
70 });
71 test('Filter button click event', function(){
72 tf1.setFilterValue(4, '>30');
73 btn.click();
74 deepEqual(tf1.getValidRows().length, 2, 'Filter button event result');
75 });
76
77 test('Cannot init if initialised', function() {
78 // setup
79 var importFile = tf1.import;
80 var hit = 0;
81 tf1.import = function() { hit++; };
82 tf1.initialized = true;
83
84 // act
85 tf1.init();
86
87 // assert
88 deepEqual(hit, 0, 'import not called');
89
90 tf1.import = importFile;
91 });
92
93 module('Tear-down');
94 test('can destroy TableFilter DOM elements', function() {
95 tf.destroy();
96 tf1.destroy();
97
98 deepEqual(tf.isInitialized(), false, 'Instance no longer initialised');
99 deepEqual(tf1.getFilterElement(0), null, 'Filter 0 removed');
100 });
101
102 module('Edge cases');
103 test('throws when no working DOM element', function() {
104 throws(
105 function() { new TableFilter('xyz'); },
106 Error,
107 'Throws Error when no DOM table'
108 );
109 });
110 test('Can instantiate with wrong refRow', function() {
111 var tf2 = new TableFilter('demo', -9);
112 tf2.init();
113 deepEqual(tf2.nbCells, 5, 'Expected number of columns');
114 });
115
116})(window, TableFilter);