1 |
|
2 | var tf = new TableFilter('demo', {
|
3 | base_path: '../dist/tablefilter/',
|
4 | help_instructions: true
|
5 | });
|
6 | tf.init();
|
7 |
|
8 | var help = tf.feature('help');
|
9 | module('Sanity checks');
|
10 | test('Button element', function() {
|
11 | deepEqual(typeof help, 'object', 'Help instanciated');
|
12 | notEqual(help.btn, null, 'btn property');
|
13 | });
|
14 |
|
15 | module('Feature interface');
|
16 | test('Properties', function() {
|
17 | deepEqual(help.tf instanceof TableFilter, true, 'TableFilter instance');
|
18 | deepEqual(help.feature, 'help', 'Feature name');
|
19 | deepEqual(help.enabled, true, 'Feature enabled');
|
20 | deepEqual(help.initialized, true, 'Feature enabled');
|
21 | deepEqual(typeof help.emitter, 'object', 'Feature has emitter instance');
|
22 | deepEqual(typeof help.config, 'object', 'TF configuration object');
|
23 | deepEqual(typeof help.init, 'function', 'Feature init method');
|
24 | deepEqual(typeof help.destroy, 'function', 'Feature destroy method');
|
25 | deepEqual(typeof help.reset, 'function', 'Feature reset method');
|
26 | deepEqual(typeof help.enable, 'function', 'Feature enable method');
|
27 | deepEqual(typeof help.disable, 'function', 'Feature enable method');
|
28 | deepEqual(typeof help.isEnabled, 'function', 'Feature enable method');
|
29 | });
|
30 | test('Can destroy', function() {
|
31 | help.destroy();
|
32 | deepEqual(help.initialized, false, 'not initialised');
|
33 | });
|
34 | test('Can reset', function() {
|
35 | help.reset();
|
36 | deepEqual(help.enabled, true, 'enabled');
|
37 | });
|
38 | test('Can disable', function() {
|
39 | help.disable();
|
40 | deepEqual(help.enabled, false, 'disabled');
|
41 | });
|
42 | test('Can enable', function() {
|
43 | help.enable();
|
44 | deepEqual(help.enabled, true, 'enabled');
|
45 | });
|
46 | test('Can init', function() {
|
47 | help.destroy();
|
48 | help.enable();
|
49 | help.init();
|
50 | deepEqual(help.enabled, true, 'enabled');
|
51 | });
|
52 | test('Can check is enabled', function() {
|
53 | help.isEnabled();
|
54 | deepEqual(help.enabled, true, 'enabled');
|
55 | });
|
56 |
|
57 | module('UI elements');
|
58 | test('Help UI elements', function() {
|
59 | var container = help.cont,
|
60 | helpBtn = help.btn;
|
61 | deepEqual(container.nodeName, 'DIV', 'Help container');
|
62 | deepEqual(helpBtn.nodeName, 'SPAN', 'Help button');
|
63 | });
|
64 |
|
65 | test('Help container auto-closes when user clicks away', function() {
|
66 |
|
67 | help.toggle();
|
68 |
|
69 |
|
70 | var evObj = document.createEvent('HTMLEvents');
|
71 | evObj.initEvent('mouseup', true, true);
|
72 |
|
73 | tf.dom().rows[3].cells[2].dispatchEvent(evObj);
|
74 |
|
75 |
|
76 | deepEqual(help.cont.style.display, 'none',
|
77 | 'Help container closed after user clicks away'
|
78 | );
|
79 | });
|
80 |
|
81 |
|
82 | test('Close button closes popup', function() {
|
83 |
|
84 | help.toggle();
|
85 |
|
86 |
|
87 | var evObj = document.createEvent('HTMLEvents');
|
88 | evObj.initEvent('click', true, true);
|
89 | help.cont.querySelector('.close').dispatchEvent(evObj);
|
90 |
|
91 |
|
92 | deepEqual(help.cont.style.display, 'none',
|
93 | 'Close button closes popup'
|
94 | );
|
95 | });
|
96 | test('Help button closes popup when already open', function() {
|
97 |
|
98 | help.toggle();
|
99 |
|
100 |
|
101 | var evObj = document.createEvent('HTMLEvents');
|
102 | evObj.initEvent('click', true, true);
|
103 | help.btn.querySelector('.helpBtn').dispatchEvent(evObj);
|
104 |
|
105 |
|
106 | deepEqual(help.cont.style.display, 'none',
|
107 | 'Close button closes popup'
|
108 | );
|
109 | });
|
110 |
|
111 | module('Destroy and re-init');
|
112 | test('Remove UI', function() {
|
113 | help.destroy();
|
114 | var container = help.cont,
|
115 | helpBtn = help.btn;
|
116 | deepEqual(container, null, 'Help container removed');
|
117 | deepEqual(helpBtn, null, 'Help button removed');
|
118 | });
|
119 |
|
120 | test('Re-set UI', function() {
|
121 | var help = tf.feature('help');
|
122 | help.destroy();
|
123 | help.btnText = '→Help←';
|
124 | help.instrText = 'Hello world!';
|
125 | help.init();
|
126 |
|
127 | var container = help.cont,
|
128 | helpBtn = help.btn;
|
129 | notEqual(
|
130 | container.innerHTML.indexOf('Hello world!'),
|
131 | -1,
|
132 | 'Help pop-up text'
|
133 | );
|
134 | notEqual(helpBtn.innerHTML.indexOf('→Help←'), -1, 'Help button text');
|
135 | });
|
136 |
|
137 |
|
138 | module('Destroy and re-init');
|
139 | test('Can init help when property is undefined and toolbar is set',
|
140 | function() {
|
141 | tf.destroy();
|
142 | tf = new TableFilter('demo', {
|
143 | base_path: '../dist/tablefilter/',
|
144 | help_instructions: undefined,
|
145 |
|
146 | rows_counter: true
|
147 | });
|
148 | tf.init();
|
149 | var help = tf.feature('help');
|
150 |
|
151 | notEqual(help.btn, null, 'btn property');
|
152 | deepEqual(help.btn.childNodes[1].innerHTML, '?', 'Button text');
|
153 | }
|
154 | );
|
155 |
|
156 | test('Does not init help when property is undefined and toolbar is not set ' +
|
157 | 'by other feature(s)',
|
158 | function() {
|
159 | tf.destroy();
|
160 | tf = new TableFilter('demo', {
|
161 | base_path: '../dist/tablefilter/'
|
162 | });
|
163 | tf.init();
|
164 | var help = tf.feature('help');
|
165 |
|
166 | deepEqual(help.initialized, false, 'feature not initialized');
|
167 | deepEqual(help.btn, null, 'btn property not set');
|
168 | deepEqual(help.cont, null, 'cont property not set');
|
169 | });
|
170 |
|
171 | test('Can init help when property is defined with a literal object ' +
|
172 | 'and toolbar is set',
|
173 | function() {
|
174 | tf.destroy();
|
175 | tf = new TableFilter('demo', {
|
176 | base_path: '../dist/tablefilter/',
|
177 | help_instructions: {
|
178 | btn_text: '??',
|
179 | text: 'hello world'
|
180 | },
|
181 |
|
182 | rows_counter: true
|
183 | });
|
184 | tf.init();
|
185 | var help = tf.feature('help');
|
186 |
|
187 | notEqual(help.btn, null, 'btn property');
|
188 | deepEqual(help.btn.childNodes[1].innerHTML, '??', 'Button text');
|
189 | deepEqual(help.instrText, 'hello world', 'Text property');
|
190 | deepEqual(help.cont.innerHTML.indexOf('hello world') !== -1, true,
|
191 | 'Text in DOM element');
|
192 | });
|
193 |
|
194 | test('Can toggle help when property is undefined and toolbar is set',
|
195 | function() {
|
196 | tf.destroy();
|
197 | tf = new TableFilter('demo', {
|
198 | base_path: '../dist/tablefilter/',
|
199 |
|
200 | rows_counter: true
|
201 | });
|
202 | tf.init();
|
203 | var help = tf.feature('help');
|
204 |
|
205 |
|
206 | help.toggle();
|
207 |
|
208 | notEqual(help, null, 'help instantiated');
|
209 | deepEqual(help.enabled, true, 'help enabled');
|
210 | deepEqual(help.cont.style.display, 'inline', 'Container is open');
|
211 | });
|
212 |
|
213 | module('Tear-down');
|
214 | test('can destroy TableFilter DOM elements', function() {
|
215 | tf.destroy();
|
216 | deepEqual(tf.isInitialized(), false, 'Filters removed');
|
217 | });
|