blob: d83b6f6af446b8ad563be444383be855713ded52 [file] [log] [blame]
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -07001/**
2 * © OpenCORD
3 *
4 * Created by teone on 3/24/16.
5 */
6
7(function () {
8 'use strict';
9
10 describe('The xos.helper module', function(){
11 describe('The xos-table component', () => {
12
13 beforeEach(module('xos.helpers'));
14
15 it('should throw an error if no config is specified', inject(($compile, $rootScope) => {
16 function errorFunctionWrapper(){
17 $compile(angular.element('<xos-table></xos-table>'))($rootScope);
18 $rootScope.$digest();
19 }
20 expect(errorFunctionWrapper).toThrow(new Error('[xosTable] Please provide a configuration via the "config" attribute'));
21 }));
22
23 it('should throw an error if no config columns are specified', inject(($compile, $rootScope) => {
24 function errorFunctionWrapper(){
25 // setup the parent scope
26 let scope = $rootScope.$new();
27 scope.config = 'green';
28 $compile(angular.element('<xos-table config="config"></xos-table>'))(scope);
29 $rootScope.$digest();
30 }
31 expect(errorFunctionWrapper).toThrow(new Error('[xosTable] Please provide a columns list in the configuration'));
32 }));
33
Matteo Scandolo6a6586e2016-04-14 16:52:13 -070034 describe('when basicly configured', function() {
35 var scope, element, isolatedScope;
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -070036
Matteo Scandolo6a6586e2016-04-14 16:52:13 -070037 beforeEach(inject(function ($compile, $rootScope) {
38 scope = $rootScope.$new();
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -070039
Matteo Scandolo6a6586e2016-04-14 16:52:13 -070040 scope.config = {
41 columns: [
42 {
43 label: 'Label 1',
44 prop: 'label-1'
45 },
46 {
47 label: 'Label 2',
48 prop: 'label-2'
49 }
50 ]
51 };
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -070052
Matteo Scandolo6a6586e2016-04-14 16:52:13 -070053 scope.data = [
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -070054 {
Matteo Scandolo6a6586e2016-04-14 16:52:13 -070055 'label-1': 'Sample 1.1',
56 'label-2': 'Sample 1.2'
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -070057 },
58 {
Matteo Scandolo6a6586e2016-04-14 16:52:13 -070059 'label-1': 'Sample 2.1',
60 'label-2': 'Sample 2.2'
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -070061 }
62 ]
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -070063
Matteo Scandolo6a6586e2016-04-14 16:52:13 -070064 element = angular.element('<xos-table config="config" data="data"></xos-table>');
65 $compile(element)(scope);
66 scope.$digest();
67 isolatedScope = element.isolateScope().vm;
68 }));
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -070069
Matteo Scandolo6a6586e2016-04-14 16:52:13 -070070 it('should contain 2 columns', function() {
71 var th = element[0].getElementsByTagName('th');
72 expect(th.length).toEqual(2);
73 expect(isolatedScope.columns.length).toEqual(2);
74 });
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -070075
Matteo Scandolo6a6586e2016-04-14 16:52:13 -070076 it('should contain 3 rows', function() {
77 var tr = element[0].getElementsByTagName('tr');
78 expect(tr.length).toEqual(3);
79 });
80
81 describe('when actions are passed', () => {
82
83 let cb = jasmine.createSpy('callback')
84
85 beforeEach(() => {
86 isolatedScope.config.actions = [
87 {
88 label: 'delete',
89 icon: 'remove',
90 cb: cb,
91 color: 'red'
92 }
93 ];
94 scope.$digest();
95 });
96
97 it('should have 3 columns', () => {
98 var th = element[0].getElementsByTagName('th');
99 expect(th.length).toEqual(3);
100 expect(isolatedScope.columns.length).toEqual(2);
101 });
102
103 it('when clicking on action should invoke callback', () => {
104 var link = element[0].getElementsByTagName('a')[0];
105 link.click();
106 expect(cb).toHaveBeenCalledWith(scope.data[0]);
107 });
108 });
109
110 describe('when filter is fulltext', () => {
111 beforeEach(() => {
112 isolatedScope.config.filter = 'fulltext';
113 scope.$digest();
114 });
115
116 it('should render a text field', () => {
117 var textField = element[0].getElementsByTagName('input');
118 expect(textField.length).toEqual(1);
119 });
120
121 describe('and a value is enterd', () => {
122 beforeEach(() => {
123 isolatedScope.query = '2.2';
124 scope.$digest();
125 });
126
127 it('should contain 2 rows', function() {
128 var tr = element[0].getElementsByTagName('tr');
129 expect(tr.length).toEqual(2);
130 });
131 });
132 });
133
134 describe('when filter is field', () => {
135 beforeEach(() => {
136 isolatedScope.config.filter = 'field';
137 scope.$digest();
138 });
139
140 it('should render a text field for each column', () => {
141 var textField = element[0].getElementsByTagName('input');
142 expect(textField.length).toEqual(2);
143 });
144
145 describe('and a value is enterd', () => {
146 beforeEach(() => {
147 isolatedScope.query = {'label-1': '2.1'};
148 scope.$digest();
149 });
150
151 it('should contain 3 rows', function() {
152 var tr = element[0].getElementsByTagName('tr');
153 expect(tr.length).toEqual(3);
154 });
155 });
156 });
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -0700157 });
158 });
159 });
160})();
161