blob: 2b3476a76c062a42373a6ece7184a5b8858d14e3 [file] [log] [blame]
Matteo Scandolocc571402016-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 Scandolo5697e452016-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 Scandolocc571402016-05-03 15:13:59 -070023
24 describe('The xos.helper module', function(){
25 describe('The xos-smart-pie component', () => {
Matteo Scandolo160e6ce2016-06-15 15:58:49 -070026
Matteo Scandolocc571402016-05-03 15:13:59 -070027 beforeEach(module('xos.helpers'));
28
Matteo Scandolo5697e452016-05-04 17:22:45 -070029 beforeEach(function(){
30 module(function($provide){
31 $provide.service('MockResource', function(){
32 return {
33 query: ''
34 }
35 });
36 });
37 });
38
39 beforeEach(inject(function ($compile, $rootScope) {
Matteo Scandolocc571402016-05-03 15:13:59 -070040
41 // set mockData
42 mockData = [
43 {
44 id: 1,
45 first_name: 'Jon',
46 last_name: 'Snow',
47 category: 1
48 },
49 {
50 id: 2,
51 first_name: 'Danaerys',
52 last_name: 'Targaryen',
53 category: 2
54 },
55 {
56 id: 3,
57 first_name: 'Aria',
58 last_name: 'Stark',
59 category: 1
60 }
61 ]
62
Matteo Scandolo5697e452016-05-04 17:22:45 -070063 compile = $compile;
64 rootScope = $rootScope;
Matteo Scandolocc571402016-05-03 15:13:59 -070065 }));
66
Matteo Scandolo5697e452016-05-04 17:22:45 -070067 it('should throw an error if no resource and no data are passed in the config', inject(($compile, $rootScope) => {
68 function errorFunctionWrapper(){
69 // setup the parent scope
Matteo Scandolo02d40342016-05-03 15:45:28 -070070 scope = $rootScope.$new();
Matteo Scandolo5697e452016-05-04 17:22:45 -070071 scope.config = {};
72 compileElement();
73 }
74 expect(errorFunctionWrapper).toThrow(new Error('[xosSmartPie] Please provide a resource or an array of data in the configuration'));
75 }));
76
77 describe('when data are passed in the configuration', () => {
78 beforeEach(inject(function ($compile, $rootScope) {
79 scope = $rootScope.$new();
80
Matteo Scandolo02d40342016-05-03 15:45:28 -070081 scope.config = {
Matteo Scandolo5697e452016-05-04 17:22:45 -070082 data: mockData,
Matteo Scandolo02d40342016-05-03 15:45:28 -070083 groupBy: 'category',
Matteo Scandolo5697e452016-05-04 17:22:45 -070084 classes: 'my-test-class'
85 };
86
87 compileElement();
88 }));
89
90
91 it('should attach provided classes', () => {
92 expect($(element).find('canvas')).toHaveClass('my-test-class');
93 });
94
95 it('should group elements', () => {
96 let groupedData = [2, 1];
97 expect(isolatedScope.data).toEqual(groupedData);
98 });
99
100 describe('when a labelFormatter function is provided', () => {
101 beforeEach(() => {
102 scope.config.labelFormatter = (labels) => {
Matteo Scandolo02d40342016-05-03 15:45:28 -0700103 return labels.map(l => l === '1' ? 'First' : 'Second');
Matteo Scandolo5697e452016-05-04 17:22:45 -0700104 };
105 compileElement();
106 });
107 it('should format labels', () => {
108 expect(isolatedScope.labels).toEqual(['First', 'Second'])
109 });
110 });
Matteo Scandolo02d40342016-05-03 15:45:28 -0700111
Matteo Scandolo5697e452016-05-04 17:22:45 -0700112 describe('when provided data changes', () => {
113 beforeEach(() => {
114 scope.config.data.push({
115 id: 2,
116 first_name: 'Danaerys',
117 last_name: 'Targaryen',
118 category: 1
119 });
120 scope.$digest();
121 });
122 it('should calculate again the data', () => {
123 expect(isolatedScope.data).toEqual([3, 1]);
124 });
Matteo Scandolo02d40342016-05-03 15:45:28 -0700125 });
126 });
127
Matteo Scandolo5697e452016-05-04 17:22:45 -0700128
129 describe('when a resource is specified in the configuration', () => {
Matteo Scandolo952c4502016-05-04 09:44:29 -0700130
Matteo Scandolo5697e452016-05-04 17:22:45 -0700131 beforeEach(inject(function ($compile, $rootScope, $q, MockResource) {
Matteo Scandolo68df51b2016-05-03 17:07:01 -0700132 scope = $rootScope.$new();
Matteo Scandolo5697e452016-05-04 17:22:45 -0700133
Matteo Scandolo68df51b2016-05-03 17:07:01 -0700134 scope.config = {
135 resource: 'MockResource',
136 groupBy: 'category',
Matteo Scandolo5697e452016-05-04 17:22:45 -0700137 classes: 'my-test-class'
Matteo Scandolo68df51b2016-05-03 17:07:01 -0700138 };
Matteo Scandolo5697e452016-05-04 17:22:45 -0700139
140 spy = MockResource;
141
142 spyOn(MockResource, 'query').and.callFake(function() {
143 var deferred = $q.defer();
144 deferred.resolve(mockData);
145 return {$promise: deferred.promise};
146 });
147
148 compileElement();
Matteo Scandolo68df51b2016-05-03 17:07:01 -0700149 }));
150
Matteo Scandolo5697e452016-05-04 17:22:45 -0700151
152 it('should call the server and group elements', () => {
153 let groupedData = [2, 1];
Matteo Scandolo68df51b2016-05-03 17:07:01 -0700154 expect(spy.query).toHaveBeenCalled();
Matteo Scandolo5697e452016-05-04 17:22:45 -0700155 expect(isolatedScope.data).toEqual(groupedData);
156 });
157
158 describe('when a labelFormatter function is provided', () => {
159 beforeEach(inject(function ($compile, $rootScope){
160 scope = $rootScope.$new();
161 scope.config = {
162 resource: 'MockResource',
163 groupBy: 'category',
164 classes: 'label-formatter-test',
165 labelFormatter: (labels) => {
166 return labels.map(l => l === '1' ? 'First' : 'Second');
167 }
168 };
169 compileElement();
170 }));
171
172 it('should format labels', () => {
173 expect(isolatedScope.labels).toEqual(['First', 'Second'])
174 });
175 });
176
177 describe('when polling is enabled', () => {
178 beforeEach(inject(function ($compile, $rootScope, $interval){
179
180 //mocked $interval (by ngMock)
181 interval = $interval;
182
183 // cleaning the spy
184 spy.query.calls.reset()
185
186 scope = $rootScope.$new();
187 scope.config = {
188 resource: 'MockResource',
189 groupBy: 'category',
190 classes: 'label-formatter-test',
191 poll: 2
192 };
193 compileElement();
194 }));
195
196 it('should call the backend every 2 second', () => {
197 expect(spy.query).toHaveBeenCalled();
198 expect(spy.query.calls.count()).toEqual(1);
199 interval.flush(2000);
200 expect(spy.query.calls.count()).toEqual(2);
201 interval.flush(2000);
202 expect(spy.query.calls.count()).toEqual(3)
203 });
Matteo Scandolo68df51b2016-05-03 17:07:01 -0700204 });
205 });
206
Matteo Scandolo5697e452016-05-04 17:22:45 -0700207
Matteo Scandolocc571402016-05-03 15:13:59 -0700208 });
209 });
210})();