blob: 0b6ea87ee55611ce69eeb404b2f7b17fc5db8575 [file] [log] [blame]
Matteo Scandoloa5d03d52016-07-21 11:35:46 -07001/**
2 * © OpenCORD
3 *
4 * Created by teone on 3/24/16.
5 */
6
7(function () {
8 'use strict';
9
10 let scope, element, isolatedScope, rootScope, compile, filter;
11 const compileElement = () => {
12
13 if(!scope){
14 scope = rootScope.$new();
15 }
16
17 element = angular.element('<xos-table config="config" data="data"></xos-table>');
18 compile(element)(scope);
19 scope.$digest();
20 isolatedScope = element.isolateScope().vm;
21 };
22
23
24 describe('The xos.helper module', function(){
25 describe('The xos-table component', () => {
26
27 beforeEach(module('xos.helpers'));
28
29 beforeEach(inject(function ($compile, $rootScope, $filter) {
30 compile = $compile;
31 rootScope = $rootScope;
32 filter = $filter;
33 }));
34
35 it('should throw an error if no config is specified', () => {
36 function errorFunctionWrapper(){
37 compileElement();
38 }
39 expect(errorFunctionWrapper).toThrow(new Error('[xosTable] Please provide a configuration via the "config" attribute'));
40 });
41
42 it('should throw an error if no config columns are specified', () => {
43 function errorFunctionWrapper(){
44 // setup the parent scope
45 scope = rootScope.$new();
46 scope.config = 'green';
47 compileElement();
48 }
49 expect(errorFunctionWrapper).toThrow(new Error('[xosTable] Please provide a columns list in the configuration'));
50 });
51
52 describe('when basically configured', function() {
53
54 beforeEach(inject(function ($compile, $rootScope) {
55
56 scope = $rootScope.$new();
57
58 scope.config = {
59 columns: [
60 {
61 label: 'Label 1',
62 prop: 'label-1'
63 },
64 {
65 label: 'Label 2',
66 prop: 'label-2'
67 }
68 ]
69 };
70
71 scope.data = [
72 {
73 'label-1': 'Sample 1.1',
74 'label-2': 'Sample 1.2'
75 },
76 {
77 'label-1': 'Sample 2.1',
78 'label-2': 'Sample 2.2'
79 }
80 ]
81
82 element = angular.element('<xos-table config="config" data="data"></xos-table>');
83 $compile(element)(scope);
84 scope.$digest();
85 isolatedScope = element.isolateScope().vm;
86 }));
87
88 it('should contain 2 columns', function() {
89 const th = element[0].getElementsByTagName('th');
90 expect(th.length).toEqual(2);
91 expect(isolatedScope.columns.length).toEqual(2);
92 });
93
94 it('should contain 3 rows', function() {
95 const tr = element[0].getElementsByTagName('tr');
96 expect(tr.length).toEqual(3);
97 });
98
99 it('should render labels', () => {
100 let label1 = $(element).find('thead tr th')[0]
101 let label2 = $(element).find('thead tr th')[1]
102 expect($(label1).text().trim()).toEqual('Label 1');
103 expect($(label2).text().trim()).toEqual('Label 2');
104 });
105
106 describe('when no data are provided', () => {
107 beforeEach(() => {
108 isolatedScope.data = [];
109 scope.$digest();
110 });
111 it('should render an alert', () => {
112 let alert = element[0].getElementsByClassName('alert');
113 let table = element[0].getElementsByTagName('table');
114 expect(alert.length).toEqual(1);
115 expect(table.length).toEqual(1);
116 });
117 });
118
119 describe('when a field type is provided', () => {
120 describe('and is boolean', () => {
121 beforeEach(() => {
122 scope.config = {
123 columns: [
124 {
125 label: 'Label 1',
126 prop: 'label-1',
127 type: 'boolean'
128 },
129 {
130 label: 'Label 2',
131 prop: 'label-2',
132 type: 'boolean'
133 }
134 ]
135 };
136 scope.data = [
137 {
138 'label-1': true,
139 'label-2': 1
140 },
141 {
142 'label-1': false,
143 'label-2': 0
144 }
145 ];
146 compileElement();
147 });
148
149 it('should render an incon', () => {
150 let td1 = $(element).find('tbody tr:first-child td')[0];
151 let td2 = $(element).find('tbody tr:last-child td')[0];
152 expect($(td1).find('i')).toHaveClass('glyphicon-ok');
153 expect($(td2).find('i')).toHaveClass('glyphicon-remove');
154 });
155
156 describe('with field filters', () => {
157 beforeEach(() => {
158 scope.config.filter = 'field';
159 compileElement();
160 });
161
162 it('should render a dropdown for filtering', () => {
163 let td1 = $(element).find('table tbody tr td')[0];
164 expect(td1).toContainElement('select');
165 expect(td1).not.toContainElement('input');
166 });
167
168 it('should correctly filter results', () => {
169 isolatedScope.query = {
170 'label-1': false
171 };
172 scope.$digest();
173 expect(isolatedScope.query['label-1']).toBeFalsy();
174 const tr = $(element).find('tbody:last-child > tr');
175 const icon = $(tr[0]).find('td i');
176 expect(tr.length).toEqual(1);
177 expect(icon).toHaveClass('glyphicon-remove');
178 });
179
180 it('should correctly filter results if the field is in the form of 0|1', () => {
181 isolatedScope.query = {
182 'label-2': false
183 };
184 scope.$digest();
185 expect(isolatedScope.query['label-1']).toBeFalsy();
186 const tr = $(element).find('tbody:last-child > tr');
187 const icon = $(tr[0]).find('td i');
188 expect(tr.length).toEqual(1);
189 expect(icon).toHaveClass('glyphicon-remove');
190 });
191 });
192 });
193
194 describe('and is date', () => {
195 beforeEach(() => {
196 scope.config = {
197 columns: [
198 {
199 label: 'Label 1',
200 prop: 'label-1',
201 type: 'date'
202 }
203 ]
204 };
205 scope.data = [
206 {
207 'label-1': '2015-02-17T22:06:38.059000Z'
208 }
209 ];
210 compileElement();
211 });
212
213 it('should render an formatted date', () => {
214 let td1 = $(element).find('tbody tr:first-child td')[0];
215 const expectedDate = filter('date')(scope.data[0]['label-1'], 'H:mm MMM d, yyyy');
216 expect($(td1).text().trim()).toEqual(expectedDate);
217 });
218 });
219
220 describe('and is array', () => {
221 beforeEach(() => {
222 scope.data = [
223 {categories: ['Film', 'Music']}
224 ];
225 scope.config = {
226 filter: 'field',
227 columns: [
228 {
229 label: 'Categories',
230 prop: 'categories',
231 type: 'array'
232 }
233 ]
234 }
235 compileElement();
236 });
237 it('should render a comma separated list', () => {
238 let td1 = $(element).find('tbody:last-child tr:first-child')[0];
239 expect($(td1).text().trim()).toEqual('Film, Music');
240 });
241
242 it('should not render the filter field', () => {
243 let filter = $(element).find('tbody tr td')[0];
244 expect($(filter)).not.toContainElement('input');
245 });
246 });
247
248 describe('and is object', () => {
249 beforeEach(() => {
250 scope.data = [
251 {
252 attributes: {
253 age: 20,
254 height: 50
255 }
256 }
257 ];
258 scope.config = {
259 filter: 'field',
260 columns: [
261 {
262 label: 'Categories',
263 prop: 'attributes',
264 type: 'object'
265 }
266 ]
267 }
268 compileElement();
269 });
270 it('should render a list of key-values', () => {
271 let td = $(element).find('tbody:last-child tr:first-child')[0];
272 let ageLabel = $(td).find('dl dt')[0];
273 let ageValue = $(td).find('dl dd')[0];
274 let heightLabel = $(td).find('dl dt')[1];
275 let heightValue = $(td).find('dl dd')[1];
276 expect($(ageLabel).text().trim()).toEqual('age');
277 expect($(ageValue).text().trim()).toEqual('20');
278 expect($(heightLabel).text().trim()).toEqual('height');
279 expect($(heightValue).text().trim()).toEqual('50');
280 });
281
282 it('should not render the filter field', () => {
283 let filter = $(element).find('tbody tr td')[0];
284 expect($(filter)).not.toContainElement('input');
285 });
286 });
287
288 describe('and is custom', () => {
289
290 let formatterFn = jasmine.createSpy('formatter').and.returnValue('Formatted Content');
291
292 beforeEach(() => {
293 scope.data = [
294 {categories: ['Film', 'Music']}
295 ];
296 scope.config = {
297 filter: 'field',
298 columns: [
299 {
300 label: 'Categories',
301 prop: 'categories',
302 type: 'custom',
303 formatter: formatterFn
304 }
305 ]
306 }
307 compileElement();
308 });
309
310 it('should check for a formatter property', () => {
311 function errorFunctionWrapper(){
312 // setup the parent scope
313 scope = rootScope.$new();
314 scope.config = {
315 columns: [
316 {
317 label: 'Categories',
318 prop: 'categories',
319 type: 'custom'
320 }
321 ]
322 };
323 compileElement();
324 }
325 expect(errorFunctionWrapper).toThrow(new Error('[xosTable] You have provided a custom field type, a formatter function should provided too.'));
326 });
327
328 it('should check that the formatter property is a function', () => {
329 function errorFunctionWrapper(){
330 // setup the parent scope
331 scope = rootScope.$new();
332 scope.config = {
333 columns: [
334 {
335 label: 'Categories',
336 prop: 'categories',
337 type: 'custom',
338 formatter: 'formatter'
339 }
340 ]
341 };
342 compileElement();
343 }
344 expect(errorFunctionWrapper).toThrow(new Error('[xosTable] You have provided a custom field type, a formatter function should provided too.'));
345 });
346
347 it('should format data using the formatter property', () => {
348 let td1 = $(element).find('tbody:last-child tr:first-child')[0];
349 expect($(td1).text().trim()).toEqual('Formatted Content');
350 // the custom formatted should receive the entire object, otherwise is not so custom
351 expect(formatterFn).toHaveBeenCalledWith({categories: ['Film', 'Music']});
352 });
353
354 it('should not render the filter field', () => {
355 // displayed value is different from model val, filter would not work
356 let filter = $(element).find('tbody tr td')[0];
357 expect($(filter)).not.toContainElement('input');
358 });
359 });
360
361 describe('and is icon', () => {
362
363 beforeEach(() => {
364 scope.config = {
365 columns: [
366 {
367 label: 'Label 1',
368 prop: 'label-1',
369 type: 'icon',
370 formatter: item => {
371 switch (item['label-1']){
372 case 1:
373 return 'ok';
374 case 2:
375 return 'remove';
376 case 3:
377 return 'plus'
378 }
379 }
380 }
381 ]
382 };
383 scope.data = [
384 {
385 'label-1': 1
386 },
387 {
388 'label-1': 2
389 },
390 {
391 'label-1': 3
392 }
393 ];
394 compileElement();
395 });
396
397 it('should render a custom icon', () => {
398 let td1 = $(element).find('tbody tr:first-child td')[0];
399 let td2 = $(element).find('tbody tr:nth-child(2) td')[0];
400 let td3 = $(element).find('tbody tr:last-child td')[0];
401 expect($(td1).find('i')).toHaveClass('glyphicon-ok');
402 expect($(td2).find('i')).toHaveClass('glyphicon-remove');
403 expect($(td3).find('i')).toHaveClass('glyphicon-plus');
404 });
405 });
406 });
407
408 describe('when a link property is provided', () => {
409 beforeEach(() => {
410 scope.data = [
411 {
412 id: 1}
413 ];
414 scope.config = {
415 columns: [
416 {
417 label: 'Id',
418 prop: 'id',
419 link: (item) => {
420 return `/link/${item.id}`;
421 }
422 }
423 ]
424 }
425 compileElement();
426 });
427
428 it('should check that the link property is a function', () => {
429 function errorFunctionWrapper(){
430 // setup the parent scope
431 scope = rootScope.$new();
432 scope.config = {
433 columns: [
434 {
435 label: 'Categories',
436 prop: 'categories',
437 link: 'custom'
438 }
439 ]
440 };
441 compileElement();
442 }
443 expect(errorFunctionWrapper).toThrow(new Error('[xosTable] The link property should be a function.'));
444 });
445
446 it('should render a link with the correct url', () => {
447 let link = $(element).find('tbody tr:first-child td a')[0];
448 expect($(link).attr('href')).toEqual('/link/1');
449 });
450 });
451
452 describe('when actions are passed', () => {
453
454 let cb = jasmine.createSpy('callback')
455
456 beforeEach(() => {
457 isolatedScope.config.actions = [
458 {
459 label: 'delete',
460 icon: 'remove',
461 cb: cb,
462 color: 'red'
463 }
464 ];
465 scope.$digest();
466 });
467
468 it('should have 3 columns', () => {
469 const th = element[0].getElementsByTagName('th');
470 expect(th.length).toEqual(3);
471 expect(isolatedScope.columns.length).toEqual(2);
472 });
473
474 it('when clicking on action should invoke callback', () => {
475 const link = element[0].getElementsByTagName('a')[0];
476 link.click();
477 expect(cb).toHaveBeenCalledWith(scope.data[0]);
478 });
479 });
480
481 describe('when filter is fulltext', () => {
482 beforeEach(() => {
483 isolatedScope.config.filter = 'fulltext';
484 scope.$digest();
485 });
486
487 it('should render a text field', () => {
488 const textField = element[0].getElementsByTagName('input');
489 expect(textField.length).toEqual(1);
490 });
491
492 describe('and a value is enterd', () => {
493 beforeEach(() => {
494 isolatedScope.query = '2.2';
495 scope.$digest();
496 });
497
498 it('should contain 2 rows', function() {
499 const tr = element[0].getElementsByTagName('tr');
500 expect(tr.length).toEqual(2);
501 });
502 });
503 });
504
505 describe('when filter is field', () => {
506 beforeEach(() => {
507 isolatedScope.config.filter = 'field';
508 scope.$digest();
509 });
510
511 it('should render a text field for each column', () => {
512 const textField = element[0].getElementsByTagName('input');
513 expect(textField.length).toEqual(2);
514 });
515
516 describe('and a value is enterd', () => {
517 beforeEach(() => {
518 isolatedScope.query = {'label-1': '2.1'};
519 scope.$digest();
520 });
521
522 it('should contain 3 rows', function() {
523 const tr = element[0].getElementsByTagName('tr');
524 expect(tr.length).toEqual(3);
525 });
526 });
527 });
528
529 describe('when order is true', () => {
530 beforeEach(() => {
531 isolatedScope.config.order = true;
532 scope.$digest();
533 });
534
535 it('should render a arrows beside', () => {
536 const arrows = element[0].getElementsByTagName('i');
537 expect(arrows.length).toEqual(4);
538 });
539
540 describe('and a default ordering is passed', () => {
541
542 beforeEach(() => {
543 scope.config.order = {
544 field: 'label-1',
545 reverse: true
546 };
547 compileElement();
548 });
549
550 it('should orderBy the default order', () => {
551 const tr = $(element).find('tr');
552 expect($(tr[1]).text()).toContain('Sample 2.2');
553 expect($(tr[2]).text()).toContain('Sample 1.1');
554 });
555 });
556
557 describe('and an order is set', () => {
558 beforeEach(() => {
559 isolatedScope.orderBy = 'label-1';
560 isolatedScope.reverse = true;
561 scope.$digest();
562 });
563
564 it('should orderBy', function() {
565 // console.log($(element).find('table'));
566 const tr = $(element).find('tr');
567 expect($(tr[1]).text()).toContain('Sample 2.2');
568 expect($(tr[2]).text()).toContain('Sample 1.1');
569 });
570 });
571 });
572 });
573 });
574 });
575})();
576