Matteo Scandolo | a5d03d5 | 2016-07-21 11:35:46 -0700 | [diff] [blame] | 1 | /** |
| 2 | * © OpenCORD |
| 3 | * |
| 4 | * Created by teone on 3/24/16. |
| 5 | */ |
| 6 | |
| 7 | (function () { |
| 8 | 'use strict'; |
| 9 | |
| 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 | } |
| 23 | |
| 24 | describe('The xos.helper module', function(){ |
| 25 | describe('The xos-smart-pie component', () => { |
| 26 | |
| 27 | beforeEach(module('xos.helpers')); |
| 28 | |
| 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) { |
| 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 | |
| 63 | compile = $compile; |
| 64 | rootScope = $rootScope; |
| 65 | })); |
| 66 | |
| 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 |
| 70 | scope = $rootScope.$new(); |
| 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 | |
| 81 | scope.config = { |
| 82 | data: mockData, |
| 83 | groupBy: 'category', |
| 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) => { |
| 103 | return labels.map(l => l === '1' ? 'First' : 'Second'); |
| 104 | }; |
| 105 | compileElement(); |
| 106 | }); |
| 107 | it('should format labels', () => { |
| 108 | expect(isolatedScope.labels).toEqual(['First', 'Second']) |
| 109 | }); |
| 110 | }); |
| 111 | |
| 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 | }); |
| 125 | }); |
| 126 | }); |
| 127 | |
| 128 | |
| 129 | describe('when a resource is specified in the configuration', () => { |
| 130 | |
| 131 | beforeEach(inject(function ($compile, $rootScope, $q, MockResource) { |
| 132 | scope = $rootScope.$new(); |
| 133 | |
| 134 | scope.config = { |
| 135 | resource: 'MockResource', |
| 136 | groupBy: 'category', |
| 137 | classes: 'my-test-class' |
| 138 | }; |
| 139 | |
| 140 | spy = MockResource; |
| 141 | |
| 142 | spyOn(MockResource, 'query').and.callFake(function() { |
| 143 | const deferred = $q.defer(); |
| 144 | deferred.resolve(mockData); |
| 145 | return {$promise: deferred.promise}; |
| 146 | }); |
| 147 | |
| 148 | compileElement(); |
| 149 | })); |
| 150 | |
| 151 | |
| 152 | it('should call the server and group elements', () => { |
| 153 | let groupedData = [2, 1]; |
| 154 | expect(spy.query).toHaveBeenCalled(); |
| 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 | }); |
| 204 | }); |
| 205 | }); |
| 206 | |
| 207 | |
| 208 | }); |
| 209 | }); |
| 210 | })(); |