blob: 50a5d72dea93cbbc4288ded25fa45b8b73f3a3f0 [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
Matteo Scandolofb5937d2016-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 Scandolo6a6586e2016-04-14 16:52:13 -070094 describe('when actions are passed', () => {
95
96 let cb = jasmine.createSpy('callback')
97
98 beforeEach(() => {
99 isolatedScope.config.actions = [
100 {
101 label: 'delete',
102 icon: 'remove',
103 cb: cb,
104 color: 'red'
105 }
106 ];
107 scope.$digest();
108 });
109
110 it('should have 3 columns', () => {
111 var th = element[0].getElementsByTagName('th');
112 expect(th.length).toEqual(3);
113 expect(isolatedScope.columns.length).toEqual(2);
114 });
115
116 it('when clicking on action should invoke callback', () => {
117 var link = element[0].getElementsByTagName('a')[0];
118 link.click();
119 expect(cb).toHaveBeenCalledWith(scope.data[0]);
120 });
121 });
122
123 describe('when filter is fulltext', () => {
124 beforeEach(() => {
125 isolatedScope.config.filter = 'fulltext';
126 scope.$digest();
127 });
128
129 it('should render a text field', () => {
130 var textField = element[0].getElementsByTagName('input');
131 expect(textField.length).toEqual(1);
132 });
133
134 describe('and a value is enterd', () => {
135 beforeEach(() => {
136 isolatedScope.query = '2.2';
137 scope.$digest();
138 });
139
140 it('should contain 2 rows', function() {
141 var tr = element[0].getElementsByTagName('tr');
142 expect(tr.length).toEqual(2);
143 });
144 });
145 });
146
147 describe('when filter is field', () => {
148 beforeEach(() => {
149 isolatedScope.config.filter = 'field';
150 scope.$digest();
151 });
152
153 it('should render a text field for each column', () => {
154 var textField = element[0].getElementsByTagName('input');
155 expect(textField.length).toEqual(2);
156 });
157
158 describe('and a value is enterd', () => {
159 beforeEach(() => {
160 isolatedScope.query = {'label-1': '2.1'};
161 scope.$digest();
162 });
163
164 it('should contain 3 rows', function() {
165 var tr = element[0].getElementsByTagName('tr');
166 expect(tr.length).toEqual(3);
167 });
168 });
169 });
Matteo Scandolof32a2e92016-04-14 17:19:08 -0700170
171 describe('when order is true', () => {
172 beforeEach(() => {
173 isolatedScope.config.order = true;
174 scope.$digest();
175 });
176
177 it('should render a arrows beside', () => {
178 var arrows = element[0].getElementsByTagName('i');
179 expect(arrows.length).toEqual(4);
180 });
181
182 describe('and an order is set', () => {
183 beforeEach(() => {
184 isolatedScope.orderBy = 'label-1';
185 isolatedScope.reverse = true;
186 scope.$digest();
187 });
188
189 it('should orderBy', function() {
190 var tr = element[0].getElementsByTagName('tr');
191 expect(angular.element(tr[1]).text()).toContain('Sample 2.2');
192 expect(angular.element(tr[2]).text()).toContain('Sample 1.1');
193 });
194 });
195 });
Matteo Scandoloa7e64fd2016-04-14 14:59:09 -0700196 });
197 });
198 });
199})();
200