Matteo Scandolo | 6872a0f | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 1 | /** |
| 2 | * © OpenCORD |
| 3 | * |
| 4 | * Created by teone on 3/24/16. |
| 5 | */ |
| 6 | |
| 7 | (function () { |
| 8 | 'use strict'; |
| 9 | |
Matteo Scandolo | 2b38f62 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 10 | 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 Scandolo | 6872a0f | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 23 | |
| 24 | describe('The xos.helper module', function(){ |
| 25 | describe('The xos-smart-pie component', () => { |
Matteo Scandolo | 85aef77 | 2016-06-15 15:58:49 -0700 | [diff] [blame^] | 26 | |
Matteo Scandolo | 6872a0f | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 27 | beforeEach(module('xos.helpers')); |
| 28 | |
Matteo Scandolo | 2b38f62 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 29 | 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 Scandolo | 6872a0f | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 40 | |
| 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 Scandolo | 2b38f62 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 63 | compile = $compile; |
| 64 | rootScope = $rootScope; |
Matteo Scandolo | 6872a0f | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 65 | })); |
| 66 | |
Matteo Scandolo | 2b38f62 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 67 | 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 Scandolo | a8ab5ae | 2016-05-03 15:45:28 -0700 | [diff] [blame] | 70 | scope = $rootScope.$new(); |
Matteo Scandolo | 2b38f62 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 71 | 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 Scandolo | a8ab5ae | 2016-05-03 15:45:28 -0700 | [diff] [blame] | 81 | scope.config = { |
Matteo Scandolo | 2b38f62 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 82 | data: mockData, |
Matteo Scandolo | a8ab5ae | 2016-05-03 15:45:28 -0700 | [diff] [blame] | 83 | groupBy: 'category', |
Matteo Scandolo | 2b38f62 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 84 | 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 Scandolo | a8ab5ae | 2016-05-03 15:45:28 -0700 | [diff] [blame] | 103 | return labels.map(l => l === '1' ? 'First' : 'Second'); |
Matteo Scandolo | 2b38f62 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 104 | }; |
| 105 | compileElement(); |
| 106 | }); |
| 107 | it('should format labels', () => { |
| 108 | expect(isolatedScope.labels).toEqual(['First', 'Second']) |
| 109 | }); |
| 110 | }); |
Matteo Scandolo | a8ab5ae | 2016-05-03 15:45:28 -0700 | [diff] [blame] | 111 | |
Matteo Scandolo | 2b38f62 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 112 | 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 Scandolo | a8ab5ae | 2016-05-03 15:45:28 -0700 | [diff] [blame] | 125 | }); |
| 126 | }); |
| 127 | |
Matteo Scandolo | 2b38f62 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 128 | |
| 129 | describe('when a resource is specified in the configuration', () => { |
Matteo Scandolo | 21c1c50 | 2016-05-04 09:44:29 -0700 | [diff] [blame] | 130 | |
Matteo Scandolo | 2b38f62 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 131 | beforeEach(inject(function ($compile, $rootScope, $q, MockResource) { |
Matteo Scandolo | fd56443 | 2016-05-03 17:07:01 -0700 | [diff] [blame] | 132 | scope = $rootScope.$new(); |
Matteo Scandolo | 2b38f62 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 133 | |
Matteo Scandolo | fd56443 | 2016-05-03 17:07:01 -0700 | [diff] [blame] | 134 | scope.config = { |
| 135 | resource: 'MockResource', |
| 136 | groupBy: 'category', |
Matteo Scandolo | 2b38f62 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 137 | classes: 'my-test-class' |
Matteo Scandolo | fd56443 | 2016-05-03 17:07:01 -0700 | [diff] [blame] | 138 | }; |
Matteo Scandolo | 2b38f62 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 139 | |
| 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 Scandolo | fd56443 | 2016-05-03 17:07:01 -0700 | [diff] [blame] | 149 | })); |
| 150 | |
Matteo Scandolo | 2b38f62 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 151 | |
| 152 | it('should call the server and group elements', () => { |
| 153 | let groupedData = [2, 1]; |
Matteo Scandolo | fd56443 | 2016-05-03 17:07:01 -0700 | [diff] [blame] | 154 | expect(spy.query).toHaveBeenCalled(); |
Matteo Scandolo | 2b38f62 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 155 | 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 Scandolo | fd56443 | 2016-05-03 17:07:01 -0700 | [diff] [blame] | 204 | }); |
| 205 | }); |
| 206 | |
Matteo Scandolo | 2b38f62 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 207 | |
Matteo Scandolo | 6872a0f | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 208 | }); |
| 209 | }); |
| 210 | })(); |