Matteo Scandolo | 686547a | 2017-08-08 13:05:25 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | a5d03d5 | 2016-07-21 11:35:46 -0700 | [diff] [blame] | 19 | /** |
| 20 | * © OpenCORD |
| 21 | * |
| 22 | * Created by teone on 3/24/16. |
| 23 | */ |
| 24 | |
| 25 | (function () { |
| 26 | 'use strict'; |
| 27 | |
| 28 | let mockData, compile, rootScope, spy, scope, isolatedScope, element, interval; |
| 29 | |
| 30 | const compileElement = () => { |
| 31 | |
| 32 | if(!scope){ |
| 33 | scope = rootScope.$new(); |
| 34 | } |
| 35 | |
| 36 | element = angular.element('<xos-smart-pie config="config"></xos-smart-pie>'); |
| 37 | compile(element)(scope); |
| 38 | scope.$digest(); |
| 39 | isolatedScope = element.isolateScope().vm; |
| 40 | } |
| 41 | |
| 42 | describe('The xos.helper module', function(){ |
| 43 | describe('The xos-smart-pie component', () => { |
| 44 | |
| 45 | beforeEach(module('xos.helpers')); |
| 46 | |
| 47 | beforeEach(function(){ |
| 48 | module(function($provide){ |
| 49 | $provide.service('MockResource', function(){ |
| 50 | return { |
| 51 | query: '' |
| 52 | } |
| 53 | }); |
| 54 | }); |
| 55 | }); |
| 56 | |
| 57 | beforeEach(inject(function ($compile, $rootScope) { |
| 58 | |
| 59 | // set mockData |
| 60 | mockData = [ |
| 61 | { |
| 62 | id: 1, |
| 63 | first_name: 'Jon', |
| 64 | last_name: 'Snow', |
| 65 | category: 1 |
| 66 | }, |
| 67 | { |
| 68 | id: 2, |
| 69 | first_name: 'Danaerys', |
| 70 | last_name: 'Targaryen', |
| 71 | category: 2 |
| 72 | }, |
| 73 | { |
| 74 | id: 3, |
| 75 | first_name: 'Aria', |
| 76 | last_name: 'Stark', |
| 77 | category: 1 |
| 78 | } |
| 79 | ] |
| 80 | |
| 81 | compile = $compile; |
| 82 | rootScope = $rootScope; |
| 83 | })); |
| 84 | |
| 85 | it('should throw an error if no resource and no data are passed in the config', inject(($compile, $rootScope) => { |
| 86 | function errorFunctionWrapper(){ |
| 87 | // setup the parent scope |
| 88 | scope = $rootScope.$new(); |
| 89 | scope.config = {}; |
| 90 | compileElement(); |
| 91 | } |
| 92 | expect(errorFunctionWrapper).toThrow(new Error('[xosSmartPie] Please provide a resource or an array of data in the configuration')); |
| 93 | })); |
| 94 | |
| 95 | describe('when data are passed in the configuration', () => { |
| 96 | beforeEach(inject(function ($compile, $rootScope) { |
| 97 | scope = $rootScope.$new(); |
| 98 | |
| 99 | scope.config = { |
| 100 | data: mockData, |
| 101 | groupBy: 'category', |
| 102 | classes: 'my-test-class' |
| 103 | }; |
| 104 | |
| 105 | compileElement(); |
| 106 | })); |
| 107 | |
| 108 | |
| 109 | it('should attach provided classes', () => { |
| 110 | expect($(element).find('canvas')).toHaveClass('my-test-class'); |
| 111 | }); |
| 112 | |
| 113 | it('should group elements', () => { |
| 114 | let groupedData = [2, 1]; |
| 115 | expect(isolatedScope.data).toEqual(groupedData); |
| 116 | }); |
| 117 | |
| 118 | describe('when a labelFormatter function is provided', () => { |
| 119 | beforeEach(() => { |
| 120 | scope.config.labelFormatter = (labels) => { |
| 121 | return labels.map(l => l === '1' ? 'First' : 'Second'); |
| 122 | }; |
| 123 | compileElement(); |
| 124 | }); |
| 125 | it('should format labels', () => { |
| 126 | expect(isolatedScope.labels).toEqual(['First', 'Second']) |
| 127 | }); |
| 128 | }); |
| 129 | |
| 130 | describe('when provided data changes', () => { |
| 131 | beforeEach(() => { |
| 132 | scope.config.data.push({ |
| 133 | id: 2, |
| 134 | first_name: 'Danaerys', |
| 135 | last_name: 'Targaryen', |
| 136 | category: 1 |
| 137 | }); |
| 138 | scope.$digest(); |
| 139 | }); |
| 140 | it('should calculate again the data', () => { |
| 141 | expect(isolatedScope.data).toEqual([3, 1]); |
| 142 | }); |
| 143 | }); |
| 144 | }); |
| 145 | |
| 146 | |
| 147 | describe('when a resource is specified in the configuration', () => { |
| 148 | |
| 149 | beforeEach(inject(function ($compile, $rootScope, $q, MockResource) { |
| 150 | scope = $rootScope.$new(); |
| 151 | |
| 152 | scope.config = { |
| 153 | resource: 'MockResource', |
| 154 | groupBy: 'category', |
| 155 | classes: 'my-test-class' |
| 156 | }; |
| 157 | |
| 158 | spy = MockResource; |
| 159 | |
| 160 | spyOn(MockResource, 'query').and.callFake(function() { |
| 161 | const deferred = $q.defer(); |
| 162 | deferred.resolve(mockData); |
| 163 | return {$promise: deferred.promise}; |
| 164 | }); |
| 165 | |
| 166 | compileElement(); |
| 167 | })); |
| 168 | |
| 169 | |
| 170 | it('should call the server and group elements', () => { |
| 171 | let groupedData = [2, 1]; |
| 172 | expect(spy.query).toHaveBeenCalled(); |
| 173 | expect(isolatedScope.data).toEqual(groupedData); |
| 174 | }); |
| 175 | |
| 176 | describe('when a labelFormatter function is provided', () => { |
| 177 | beforeEach(inject(function ($compile, $rootScope){ |
| 178 | scope = $rootScope.$new(); |
| 179 | scope.config = { |
| 180 | resource: 'MockResource', |
| 181 | groupBy: 'category', |
| 182 | classes: 'label-formatter-test', |
| 183 | labelFormatter: (labels) => { |
| 184 | return labels.map(l => l === '1' ? 'First' : 'Second'); |
| 185 | } |
| 186 | }; |
| 187 | compileElement(); |
| 188 | })); |
| 189 | |
| 190 | it('should format labels', () => { |
| 191 | expect(isolatedScope.labels).toEqual(['First', 'Second']) |
| 192 | }); |
| 193 | }); |
| 194 | |
| 195 | describe('when polling is enabled', () => { |
| 196 | beforeEach(inject(function ($compile, $rootScope, $interval){ |
| 197 | |
| 198 | //mocked $interval (by ngMock) |
| 199 | interval = $interval; |
| 200 | |
| 201 | // cleaning the spy |
| 202 | spy.query.calls.reset() |
| 203 | |
| 204 | scope = $rootScope.$new(); |
| 205 | scope.config = { |
| 206 | resource: 'MockResource', |
| 207 | groupBy: 'category', |
| 208 | classes: 'label-formatter-test', |
| 209 | poll: 2 |
| 210 | }; |
| 211 | compileElement(); |
| 212 | })); |
| 213 | |
| 214 | it('should call the backend every 2 second', () => { |
| 215 | expect(spy.query).toHaveBeenCalled(); |
| 216 | expect(spy.query.calls.count()).toEqual(1); |
| 217 | interval.flush(2000); |
| 218 | expect(spy.query.calls.count()).toEqual(2); |
| 219 | interval.flush(2000); |
| 220 | expect(spy.query.calls.count()).toEqual(3) |
| 221 | }); |
| 222 | }); |
| 223 | }); |
| 224 | |
| 225 | |
| 226 | }); |
| 227 | }); |
| 228 | })(); |