blob: 4c90421c311fa4eced6fbdffbd3c2abf86c7c11c [file] [log] [blame]
Matteo Scandolo6872a0f2016-05-03 15:13:59 -07001/**
2 * © OpenCORD
3 *
4 * Created by teone on 3/24/16.
5 */
6
7(function () {
8 'use strict';
9
Matteo Scandolo2b38f622016-05-04 17:22:45 -070010 let mockData, compile, rootScope, spy, scope, isolatedScope, element, interval;
11
12 const compileElement = () => {
13
14 if(!scope){
15 scope = rootScope.$new();
16 }
17
18 element = angular.element('<xos-smart-pie config="config"></xos-smart-pie>');
19 compile(element)(scope);
20 scope.$digest();
21 isolatedScope = element.isolateScope().vm;
22 }
Matteo Scandolo6872a0f2016-05-03 15:13:59 -070023
24 describe('The xos.helper module', function(){
25 describe('The xos-smart-pie component', () => {
26
Matteo Scandolo6872a0f2016-05-03 15:13:59 -070027
28 beforeEach(module('xos.helpers'));
29
Matteo Scandolo2b38f622016-05-04 17:22:45 -070030 beforeEach(function(){
31 module(function($provide){
32 $provide.service('MockResource', function(){
33 return {
34 query: ''
35 }
36 });
37 });
38 });
39
40 beforeEach(inject(function ($compile, $rootScope) {
Matteo Scandolo6872a0f2016-05-03 15:13:59 -070041
42 // set mockData
43 mockData = [
44 {
45 id: 1,
46 first_name: 'Jon',
47 last_name: 'Snow',
48 category: 1
49 },
50 {
51 id: 2,
52 first_name: 'Danaerys',
53 last_name: 'Targaryen',
54 category: 2
55 },
56 {
57 id: 3,
58 first_name: 'Aria',
59 last_name: 'Stark',
60 category: 1
61 }
62 ]
63
Matteo Scandolo2b38f622016-05-04 17:22:45 -070064 compile = $compile;
65 rootScope = $rootScope;
Matteo Scandolo6872a0f2016-05-03 15:13:59 -070066 }));
67
Matteo Scandolo2b38f622016-05-04 17:22:45 -070068 it('should throw an error if no resource and no data are passed in the config', inject(($compile, $rootScope) => {
69 function errorFunctionWrapper(){
70 // setup the parent scope
Matteo Scandoloa8ab5ae2016-05-03 15:45:28 -070071 scope = $rootScope.$new();
Matteo Scandolo2b38f622016-05-04 17:22:45 -070072 scope.config = {};
73 compileElement();
74 }
75 expect(errorFunctionWrapper).toThrow(new Error('[xosSmartPie] Please provide a resource or an array of data in the configuration'));
76 }));
77
78 describe('when data are passed in the configuration', () => {
79 beforeEach(inject(function ($compile, $rootScope) {
80 scope = $rootScope.$new();
81
Matteo Scandoloa8ab5ae2016-05-03 15:45:28 -070082 scope.config = {
Matteo Scandolo2b38f622016-05-04 17:22:45 -070083 data: mockData,
Matteo Scandoloa8ab5ae2016-05-03 15:45:28 -070084 groupBy: 'category',
Matteo Scandolo2b38f622016-05-04 17:22:45 -070085 classes: 'my-test-class'
86 };
87
88 compileElement();
89 }));
90
91
92 it('should attach provided classes', () => {
93 expect($(element).find('canvas')).toHaveClass('my-test-class');
94 });
95
96 it('should group elements', () => {
97 let groupedData = [2, 1];
98 expect(isolatedScope.data).toEqual(groupedData);
99 });
100
101 describe('when a labelFormatter function is provided', () => {
102 beforeEach(() => {
103 scope.config.labelFormatter = (labels) => {
Matteo Scandoloa8ab5ae2016-05-03 15:45:28 -0700104 return labels.map(l => l === '1' ? 'First' : 'Second');
Matteo Scandolo2b38f622016-05-04 17:22:45 -0700105 };
106 compileElement();
107 });
108 it('should format labels', () => {
109 expect(isolatedScope.labels).toEqual(['First', 'Second'])
110 });
111 });
Matteo Scandoloa8ab5ae2016-05-03 15:45:28 -0700112
Matteo Scandolo2b38f622016-05-04 17:22:45 -0700113 describe('when provided data changes', () => {
114 beforeEach(() => {
115 scope.config.data.push({
116 id: 2,
117 first_name: 'Danaerys',
118 last_name: 'Targaryen',
119 category: 1
120 });
121 scope.$digest();
122 });
123 it('should calculate again the data', () => {
124 expect(isolatedScope.data).toEqual([3, 1]);
125 });
Matteo Scandoloa8ab5ae2016-05-03 15:45:28 -0700126 });
127 });
128
Matteo Scandolo2b38f622016-05-04 17:22:45 -0700129
130 describe('when a resource is specified in the configuration', () => {
Matteo Scandolo21c1c502016-05-04 09:44:29 -0700131
Matteo Scandolo2b38f622016-05-04 17:22:45 -0700132 beforeEach(inject(function ($compile, $rootScope, $q, MockResource) {
Matteo Scandolofd564432016-05-03 17:07:01 -0700133 scope = $rootScope.$new();
Matteo Scandolo2b38f622016-05-04 17:22:45 -0700134
Matteo Scandolofd564432016-05-03 17:07:01 -0700135 scope.config = {
136 resource: 'MockResource',
137 groupBy: 'category',
Matteo Scandolo2b38f622016-05-04 17:22:45 -0700138 classes: 'my-test-class'
Matteo Scandolofd564432016-05-03 17:07:01 -0700139 };
Matteo Scandolo2b38f622016-05-04 17:22:45 -0700140
141 spy = MockResource;
142
143 spyOn(MockResource, 'query').and.callFake(function() {
144 var deferred = $q.defer();
145 deferred.resolve(mockData);
146 return {$promise: deferred.promise};
147 });
148
149 compileElement();
Matteo Scandolofd564432016-05-03 17:07:01 -0700150 }));
151
Matteo Scandolo2b38f622016-05-04 17:22:45 -0700152
153 it('should call the server and group elements', () => {
154 let groupedData = [2, 1];
Matteo Scandolofd564432016-05-03 17:07:01 -0700155 expect(spy.query).toHaveBeenCalled();
Matteo Scandolo2b38f622016-05-04 17:22:45 -0700156 expect(isolatedScope.data).toEqual(groupedData);
157 });
158
159 describe('when a labelFormatter function is provided', () => {
160 beforeEach(inject(function ($compile, $rootScope){
161 scope = $rootScope.$new();
162 scope.config = {
163 resource: 'MockResource',
164 groupBy: 'category',
165 classes: 'label-formatter-test',
166 labelFormatter: (labels) => {
167 return labels.map(l => l === '1' ? 'First' : 'Second');
168 }
169 };
170 compileElement();
171 }));
172
173 it('should format labels', () => {
174 expect(isolatedScope.labels).toEqual(['First', 'Second'])
175 });
176 });
177
178 describe('when polling is enabled', () => {
179 beforeEach(inject(function ($compile, $rootScope, $interval){
180
181 //mocked $interval (by ngMock)
182 interval = $interval;
183
184 // cleaning the spy
185 spy.query.calls.reset()
186
187 scope = $rootScope.$new();
188 scope.config = {
189 resource: 'MockResource',
190 groupBy: 'category',
191 classes: 'label-formatter-test',
192 poll: 2
193 };
194 compileElement();
195 }));
196
197 it('should call the backend every 2 second', () => {
198 expect(spy.query).toHaveBeenCalled();
199 expect(spy.query.calls.count()).toEqual(1);
200 interval.flush(2000);
201 expect(spy.query.calls.count()).toEqual(2);
202 interval.flush(2000);
203 expect(spy.query.calls.count()).toEqual(3)
204 });
Matteo Scandolofd564432016-05-03 17:07:01 -0700205 });
206 });
207
Matteo Scandolo2b38f622016-05-04 17:22:45 -0700208
Matteo Scandolo6872a0f2016-05-03 15:13:59 -0700209 });
210 });
211})();