Matteo Scandolo | cc57140 | 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 | 5697e45 | 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 | cc57140 | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 23 | |
| 24 | describe('The xos.helper module', function(){ |
| 25 | describe('The xos-smart-pie component', () => { |
| 26 | |
Matteo Scandolo | cc57140 | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 27 | |
| 28 | beforeEach(module('xos.helpers')); |
| 29 | |
Matteo Scandolo | 5697e45 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 30 | 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 Scandolo | cc57140 | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 41 | |
| 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 Scandolo | 5697e45 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 64 | compile = $compile; |
| 65 | rootScope = $rootScope; |
Matteo Scandolo | cc57140 | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 66 | })); |
| 67 | |
Matteo Scandolo | 5697e45 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 68 | 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 Scandolo | 02d4034 | 2016-05-03 15:45:28 -0700 | [diff] [blame] | 71 | scope = $rootScope.$new(); |
Matteo Scandolo | 5697e45 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 72 | 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 Scandolo | 02d4034 | 2016-05-03 15:45:28 -0700 | [diff] [blame] | 82 | scope.config = { |
Matteo Scandolo | 5697e45 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 83 | data: mockData, |
Matteo Scandolo | 02d4034 | 2016-05-03 15:45:28 -0700 | [diff] [blame] | 84 | groupBy: 'category', |
Matteo Scandolo | 5697e45 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 85 | 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 Scandolo | 02d4034 | 2016-05-03 15:45:28 -0700 | [diff] [blame] | 104 | return labels.map(l => l === '1' ? 'First' : 'Second'); |
Matteo Scandolo | 5697e45 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 105 | }; |
| 106 | compileElement(); |
| 107 | }); |
| 108 | it('should format labels', () => { |
| 109 | expect(isolatedScope.labels).toEqual(['First', 'Second']) |
| 110 | }); |
| 111 | }); |
Matteo Scandolo | 02d4034 | 2016-05-03 15:45:28 -0700 | [diff] [blame] | 112 | |
Matteo Scandolo | 5697e45 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 113 | 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 Scandolo | 02d4034 | 2016-05-03 15:45:28 -0700 | [diff] [blame] | 126 | }); |
| 127 | }); |
| 128 | |
Matteo Scandolo | 5697e45 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 129 | |
| 130 | describe('when a resource is specified in the configuration', () => { |
Matteo Scandolo | 952c450 | 2016-05-04 09:44:29 -0700 | [diff] [blame] | 131 | |
Matteo Scandolo | 5697e45 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 132 | beforeEach(inject(function ($compile, $rootScope, $q, MockResource) { |
Matteo Scandolo | 68df51b | 2016-05-03 17:07:01 -0700 | [diff] [blame] | 133 | scope = $rootScope.$new(); |
Matteo Scandolo | 5697e45 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 134 | |
Matteo Scandolo | 68df51b | 2016-05-03 17:07:01 -0700 | [diff] [blame] | 135 | scope.config = { |
| 136 | resource: 'MockResource', |
| 137 | groupBy: 'category', |
Matteo Scandolo | 5697e45 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 138 | classes: 'my-test-class' |
Matteo Scandolo | 68df51b | 2016-05-03 17:07:01 -0700 | [diff] [blame] | 139 | }; |
Matteo Scandolo | 5697e45 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 140 | |
| 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 Scandolo | 68df51b | 2016-05-03 17:07:01 -0700 | [diff] [blame] | 150 | })); |
| 151 | |
Matteo Scandolo | 5697e45 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 152 | |
| 153 | it('should call the server and group elements', () => { |
| 154 | let groupedData = [2, 1]; |
Matteo Scandolo | 68df51b | 2016-05-03 17:07:01 -0700 | [diff] [blame] | 155 | expect(spy.query).toHaveBeenCalled(); |
Matteo Scandolo | 5697e45 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 156 | 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 Scandolo | 68df51b | 2016-05-03 17:07:01 -0700 | [diff] [blame] | 205 | }); |
| 206 | }); |
| 207 | |
Matteo Scandolo | 5697e45 | 2016-05-04 17:22:45 -0700 | [diff] [blame] | 208 | |
Matteo Scandolo | cc57140 | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 209 | }); |
| 210 | }); |
| 211 | })(); |