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 | |
| 10 | let mockData; |
| 11 | |
| 12 | describe('The xos.helper module', function(){ |
| 13 | describe('The xos-smart-pie component', () => { |
| 14 | |
| 15 | var spy, scope, isolatedScope, element; |
| 16 | |
| 17 | beforeEach(module('xos.helpers')); |
| 18 | |
| 19 | beforeEach(function() { |
| 20 | |
| 21 | // set mockData |
| 22 | mockData = [ |
| 23 | { |
| 24 | id: 1, |
| 25 | first_name: 'Jon', |
| 26 | last_name: 'Snow', |
| 27 | category: 1 |
| 28 | }, |
| 29 | { |
| 30 | id: 2, |
| 31 | first_name: 'Danaerys', |
| 32 | last_name: 'Targaryen', |
| 33 | category: 2 |
| 34 | }, |
| 35 | { |
| 36 | id: 3, |
| 37 | first_name: 'Aria', |
| 38 | last_name: 'Stark', |
| 39 | category: 1 |
| 40 | } |
| 41 | ] |
| 42 | |
| 43 | }); |
| 44 | |
| 45 | // mock the service |
| 46 | beforeEach(function(){ |
| 47 | module(function($provide){ |
| 48 | $provide.service('MockResource', function(){ |
| 49 | return { |
| 50 | query: '' |
| 51 | } |
| 52 | }); |
| 53 | }); |
| 54 | }) |
| 55 | |
| 56 | beforeEach(inject(function ($compile, $rootScope, $q, MockResource) { |
| 57 | scope = $rootScope.$new(); |
| 58 | |
| 59 | scope.config = { |
| 60 | resource: 'MockResource', |
| 61 | groupBy: 'category', |
| 62 | classes: 'my-test-class' |
| 63 | }; |
| 64 | |
| 65 | spy = MockResource; |
| 66 | |
| 67 | spyOn(MockResource, 'query').and.callFake(function() { |
| 68 | var deferred = $q.defer(); |
| 69 | deferred.resolve(mockData); |
| 70 | return {$promise: deferred.promise}; |
| 71 | }); |
| 72 | |
| 73 | element = angular.element('<xos-smart-pie config="config"></xos-smart-pie>'); |
| 74 | $compile(element)(scope); |
| 75 | scope.$digest(); |
| 76 | isolatedScope = element.isolateScope().vm; |
| 77 | })); |
| 78 | |
| 79 | it('should attach provided classes', () => { |
| 80 | expect($(element).find('canvas')).toHaveClass('my-test-class'); |
| 81 | }); |
| 82 | |
| 83 | it('should group elements', () => { |
| 84 | let groupedData = [2, 1]; |
| 85 | expect(spy.query).toHaveBeenCalled(); |
| 86 | expect(isolatedScope.data).toEqual(groupedData); |
| 87 | }); |
| 88 | |
Matteo Scandolo | 02d4034 | 2016-05-03 15:45:28 -0700 | [diff] [blame] | 89 | describe('when a labelFormatter function is provided', () => { |
| 90 | beforeEach(inject(function ($compile, $rootScope){ |
| 91 | scope = $rootScope.$new(); |
| 92 | scope.config = { |
| 93 | resource: 'MockResource', |
| 94 | groupBy: 'category', |
| 95 | classes: 'label-formatter-test', |
| 96 | labelFormatter: (labels) => { |
| 97 | return labels.map(l => l === '1' ? 'First' : 'Second'); |
| 98 | } |
| 99 | }; |
| 100 | element = angular.element('<xos-smart-pie config="config"></xos-smart-pie>'); |
| 101 | $compile(element)(scope); |
| 102 | scope.$digest(); |
| 103 | isolatedScope = element.isolateScope().vm; |
| 104 | })); |
| 105 | |
| 106 | it('should format labels', () => { |
| 107 | expect(isolatedScope.labels).toEqual(['First', 'Second']) |
| 108 | }); |
| 109 | }); |
| 110 | |
Matteo Scandolo | 68df51b | 2016-05-03 17:07:01 -0700 | [diff] [blame] | 111 | describe('when polling is enabled', () => { |
| 112 | beforeEach(inject(function ($compile, $rootScope){ |
| 113 | scope = $rootScope.$new(); |
| 114 | scope.config = { |
| 115 | resource: 'MockResource', |
| 116 | groupBy: 'category', |
| 117 | classes: 'label-formatter-test', |
| 118 | poll: 2 |
| 119 | }; |
| 120 | element = angular.element('<xos-smart-pie config="config"></xos-smart-pie>'); |
| 121 | $compile(element)(scope); |
| 122 | scope.$digest(); |
| 123 | isolatedScope = element.isolateScope().vm; |
| 124 | })); |
| 125 | |
| 126 | it('should call the backend every 2 second', () => { |
| 127 | expect(spy.query).toHaveBeenCalled(); |
| 128 | // $interval |
| 129 | }); |
| 130 | }); |
| 131 | |
Matteo Scandolo | cc57140 | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 132 | }); |
| 133 | }); |
| 134 | })(); |