blob: 5ad1360f15072202cc342342e58bf9e5d3d481e3 [file] [log] [blame]
Matteo Scandolo9e6c6fc2016-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 Scandoloa6a9e612016-04-14 16:52:13 -070034 describe('when basicly configured', function() {
35 var scope, element, isolatedScope;
Matteo Scandolo9e6c6fc2016-04-14 14:59:09 -070036
Matteo Scandoloa6a9e612016-04-14 16:52:13 -070037 beforeEach(inject(function ($compile, $rootScope) {
38 scope = $rootScope.$new();
Matteo Scandolo9e6c6fc2016-04-14 14:59:09 -070039
Matteo Scandoloa6a9e612016-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 Scandolo9e6c6fc2016-04-14 14:59:09 -070052
Matteo Scandoloa6a9e612016-04-14 16:52:13 -070053 scope.data = [
Matteo Scandolo9e6c6fc2016-04-14 14:59:09 -070054 {
Matteo Scandoloa6a9e612016-04-14 16:52:13 -070055 'label-1': 'Sample 1.1',
56 'label-2': 'Sample 1.2'
Matteo Scandolo9e6c6fc2016-04-14 14:59:09 -070057 },
58 {
Matteo Scandoloa6a9e612016-04-14 16:52:13 -070059 'label-1': 'Sample 2.1',
60 'label-2': 'Sample 2.2'
Matteo Scandolo9e6c6fc2016-04-14 14:59:09 -070061 }
62 ]
Matteo Scandolo9e6c6fc2016-04-14 14:59:09 -070063
Matteo Scandoloa6a9e612016-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 Scandolo9e6c6fc2016-04-14 14:59:09 -070069
Matteo Scandoloa6a9e612016-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 Scandolo9e6c6fc2016-04-14 14:59:09 -070075
Matteo Scandoloa6a9e612016-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
Matteo Scandoloe15a8202016-04-15 14:27:54 -070081 describe('when no data are provided', () => {
82 beforeEach(() => {
83 isolatedScope.data = [];
84 scope.$digest();
85 });
86 it('should render an alert', () => {
87 let alert = element[0].getElementsByClassName('alert');
88 let table = element[0].getElementsByTagName('table');
89 expect(alert.length).toEqual(1);
90 expect(table.length).toEqual(1);
91 });
92 });
93
Matteo Scandoloeabfb712016-04-27 17:26:21 -070094 describe('when a field type is provided', () => {
95 describe('and is boolean', () => {
96 beforeEach(() => {
97 console.log('iS: ' + isolatedScope);
98 isolatedScope.config = {
99 columns: [
100 {
101 label: 'Label 1',
102 prop: 'label-1',
103 type: 'boolean'
104 }
105 ]
106 };
107 isolatedScope.data = [
108 {
109 'label-1': true
110 },
111 {
112 'label-1': false
113 }
114 ];
115 scope.$digest();
116 });
117
118 it('should render an incon', () => {
119 let td1 = $(element).find('tbody tr:first-child td')[0];
120 let td2 = $(element).find('tbody tr:last-child td')[0];
121 console.log(td2);
122 expect($(td1).find('i')).toHaveClass('glyphicon-ok');
123 expect($(td2).find('i')).toHaveClass('glyphicon-remove');
124 });
125 });
126
127 describe('and is date', () => {
128 beforeEach(() => {
129 console.log('iS: ' + isolatedScope);
130 isolatedScope.config = {
131 columns: [
132 {
133 label: 'Label 1',
134 prop: 'label-1',
135 type: 'date'
136 }
137 ]
138 };
139 isolatedScope.data = [
140 {
141 'label-1': '2015-02-17T22:06:38.059000Z'
142 }
143 ];
144 scope.$digest();
145 });
146
147 it('should render an formatted date', () => {
148 let td1 = $(element).find('tbody tr:first-child td')[0];
149 expect($(td1).text()).toEqual('02-17-2015 14:06:38');
150 });
151 });
152 });
153
Matteo Scandoloa6a9e612016-04-14 16:52:13 -0700154 describe('when actions are passed', () => {
155
156 let cb = jasmine.createSpy('callback')
157
158 beforeEach(() => {
159 isolatedScope.config.actions = [
160 {
161 label: 'delete',
162 icon: 'remove',
163 cb: cb,
164 color: 'red'
165 }
166 ];
167 scope.$digest();
168 });
169
170 it('should have 3 columns', () => {
171 var th = element[0].getElementsByTagName('th');
172 expect(th.length).toEqual(3);
173 expect(isolatedScope.columns.length).toEqual(2);
174 });
175
176 it('when clicking on action should invoke callback', () => {
177 var link = element[0].getElementsByTagName('a')[0];
178 link.click();
179 expect(cb).toHaveBeenCalledWith(scope.data[0]);
180 });
181 });
182
183 describe('when filter is fulltext', () => {
184 beforeEach(() => {
185 isolatedScope.config.filter = 'fulltext';
186 scope.$digest();
187 });
188
189 it('should render a text field', () => {
190 var textField = element[0].getElementsByTagName('input');
191 expect(textField.length).toEqual(1);
192 });
193
194 describe('and a value is enterd', () => {
195 beforeEach(() => {
196 isolatedScope.query = '2.2';
197 scope.$digest();
198 });
199
200 it('should contain 2 rows', function() {
201 var tr = element[0].getElementsByTagName('tr');
202 expect(tr.length).toEqual(2);
203 });
204 });
205 });
206
207 describe('when filter is field', () => {
208 beforeEach(() => {
209 isolatedScope.config.filter = 'field';
210 scope.$digest();
211 });
212
213 it('should render a text field for each column', () => {
214 var textField = element[0].getElementsByTagName('input');
215 expect(textField.length).toEqual(2);
216 });
217
218 describe('and a value is enterd', () => {
219 beforeEach(() => {
220 isolatedScope.query = {'label-1': '2.1'};
221 scope.$digest();
222 });
223
224 it('should contain 3 rows', function() {
225 var tr = element[0].getElementsByTagName('tr');
226 expect(tr.length).toEqual(3);
227 });
228 });
229 });
Matteo Scandolo03e59602016-04-14 17:19:08 -0700230
231 describe('when order is true', () => {
232 beforeEach(() => {
233 isolatedScope.config.order = true;
234 scope.$digest();
235 });
236
237 it('should render a arrows beside', () => {
238 var arrows = element[0].getElementsByTagName('i');
239 expect(arrows.length).toEqual(4);
240 });
241
242 describe('and an order is set', () => {
243 beforeEach(() => {
244 isolatedScope.orderBy = 'label-1';
245 isolatedScope.reverse = true;
246 scope.$digest();
247 });
248
249 it('should orderBy', function() {
250 var tr = element[0].getElementsByTagName('tr');
251 expect(angular.element(tr[1]).text()).toContain('Sample 2.2');
252 expect(angular.element(tr[2]).text()).toContain('Sample 1.1');
253 });
254 });
255 });
Matteo Scandolo9e6c6fc2016-04-14 14:59:09 -0700256 });
257 });
258 });
259})();
260