|
1 | 1 | 'use strict'; |
2 | 2 |
|
| 3 | +/*jshint -W117 */ |
| 4 | +/*jshint globalstrict: true*/ |
| 5 | +/* jasmine specs for directives go here */ |
| 6 | + |
3 | 7 | describe('Directive: leaflet center', function() { |
4 | | - var $compile; |
5 | | - var $location; |
6 | | - var $rootScope; |
7 | | - var $timeout; |
8 | | - var center; |
9 | | - var directiveName; |
10 | | - var key; |
11 | | - var leafletData; |
12 | | - var ref; |
13 | | - var results; |
14 | | - var scope; |
15 | | - scope = center = leafletData = $location = $timeout = $compile = $rootScope = void 0; |
16 | | - |
17 | | - beforeEach(module('leaflet-directive')); |
18 | | - beforeEach(inject(function(_$compile_, _$rootScope_, _$timeout_, _$location_, _leafletData_) { |
19 | | - $compile = _$compile_; |
20 | | - $rootScope = _$rootScope_; |
21 | | - $timeout = _$timeout_; |
22 | | - window.ngLeafLetTestGlobals.$timeout = $timeout; |
23 | | - $location = _$location_; |
24 | | - leafletData = _leafletData_; |
25 | | - center = { |
26 | | - lat: 0.96658, |
27 | | - lng: 2.02, |
28 | | - zoom: 4, |
| 8 | + var $compile; |
| 9 | + var $rootScope; |
| 10 | + var $timeout; |
| 11 | + var $location; |
| 12 | + var leafletData; |
| 13 | + var center; |
| 14 | + var scope; |
| 15 | + |
| 16 | + beforeEach(module('leaflet-directive')); |
| 17 | + beforeEach(inject(function(_$compile_, _$rootScope_, _$timeout_, _$location_, _leafletData_) { |
| 18 | + $compile = _$compile_; |
| 19 | + $rootScope = _$rootScope_; |
| 20 | + $timeout = _$timeout_; |
| 21 | + $location = _$location_; |
| 22 | + leafletData = _leafletData_; |
| 23 | + |
| 24 | + center = { |
| 25 | + lat: 0.96658, |
| 26 | + lng: 2.02, |
| 27 | + zoom: 4, |
| 28 | + }; |
| 29 | + |
| 30 | + scope = $rootScope.$new(); |
| 31 | + scope.center = center; |
| 32 | + })); |
| 33 | + |
| 34 | + afterEach(inject(function($rootScope) { |
| 35 | + $rootScope.$apply(); |
| 36 | + })); |
| 37 | + |
| 38 | + it('should have default {[0, 0], 1} parameters on the map if not correctly defined', function() { |
| 39 | + scope.center = {}; |
| 40 | + var element = angular.element('<leaflet center="center"></leaflet>'); |
| 41 | + element = $compile(element)(scope); |
| 42 | + scope.$digest(); |
| 43 | + |
| 44 | + leafletData.getMap().then(function(map) { |
| 45 | + expect(map.getZoom()).toEqual(1); |
| 46 | + expect(map.getCenter().lat).toEqual(0); |
| 47 | + expect(map.getCenter().lng).toEqual(0); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should update the map center if the initial center scope properties are set', function() { |
| 52 | + var element = angular.element('<leaflet center="center"></leaflet>'); |
| 53 | + element = $compile(element)(scope); |
| 54 | + scope.$digest(); |
| 55 | + |
| 56 | + leafletData.getMap().then(function(map) { |
| 57 | + expect(map.getZoom()).toEqual(center.zoom); |
| 58 | + expect(map.getCenter().lat).toBeCloseTo(0.96658, 4); |
| 59 | + expect(map.getCenter().lng).toBeCloseTo(2.02, 4); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should update the map center if the scope center properties changes', function() { |
| 64 | + var element = angular.element('<leaflet center="center"></leaflet>'); |
| 65 | + element = $compile(element)(scope); |
| 66 | + var map; |
| 67 | + leafletData.getMap().then(function(leafletMap) { |
| 68 | + map = leafletMap; |
| 69 | + }); |
| 70 | + |
| 71 | + scope.$apply(); |
| 72 | + |
| 73 | + expect(map.getCenter().lat).toBeCloseTo(0.96658, 4); |
| 74 | + expect(map.getCenter().lng).toBeCloseTo(2.02, 4); |
| 75 | + expect(map.getZoom()).toEqual(4); |
| 76 | + |
| 77 | + center.lat = 2.02999; |
| 78 | + center.lng = 4.04; |
| 79 | + center.zoom = 8; |
| 80 | + scope.$digest(); |
| 81 | + |
| 82 | + expect(map.getCenter().lat).toBeCloseTo(2.02999, 4); |
| 83 | + expect(map.getCenter().lng).toBeCloseTo(4.04, 4); |
| 84 | + expect(map.getZoom()).toEqual(8); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('Using url-hash functionality', function() { |
| 88 | + it('should update the center of the map if changes the url', function() { |
| 89 | + var element = angular.element('<leaflet center="center" url-hash-center="yes"></leaflet>'); |
| 90 | + element = $compile(element)(scope); |
| 91 | + var map; |
| 92 | + leafletData.getMap().then(function(leafletMap) { |
| 93 | + map = leafletMap; |
| 94 | + }); |
| 95 | + |
| 96 | + var centerParams = { |
| 97 | + c: '30.1' + ':' + '-9.2' + ':' + '4', |
29 | 98 | }; |
30 | | - })); |
31 | 99 |
|
32 | | - beforeEach(function() { |
33 | | - scope = $rootScope.$new(); |
34 | | - scope.center = center; |
| 100 | + $location.search(centerParams); |
| 101 | + scope.$digest(); |
| 102 | + |
| 103 | + expect(map.getCenter().lat).toBeCloseTo(30.1, 4); |
| 104 | + expect(map.getCenter().lng).toBeCloseTo(-9.2, 4); |
| 105 | + expect(map.getZoom()).toEqual(4); |
35 | 106 | }); |
36 | 107 |
|
37 | | - afterEach(inject(function($rootScope) { |
38 | | - if (!$rootScope.$$phase) { |
39 | | - return $rootScope.$apply(); |
40 | | - } |
41 | | - })); |
42 | | - |
43 | | - ref = ['center', 'lf-center']; |
44 | | - results = []; |
45 | | - for (key in ref) { |
46 | | - directiveName = ref[key]; |
47 | | - results.push(describe(directiveName, function() { |
48 | | - describe('sets leaflet from scope', function() { |
49 | | - it('should have default {[0, 0], 1} parameters on the map if not correctly defined', function() { |
50 | | - var element; |
51 | | - scope.center = {}; |
52 | | - element = angular.element('<leaflet ' + directiveName + '=\'center\'></leaflet>'); |
53 | | - element = $compile(element)(scope); |
54 | | - scope.$digest(); |
55 | | - return leafletData.getMap().then(function(map) { |
56 | | - expect(map.getZoom()).toEqual(1); |
57 | | - expect(map.getCenter().lat).toEqual(0); |
58 | | - return expect(map.getCenter().lng).toEqual(0); |
59 | | - }); |
60 | | - }); |
61 | | - |
62 | | - it('should update the map center if the initial center scope properties are set', function() { |
63 | | - var element; |
64 | | - element = angular.element('<leaflet ' + directiveName + '=\'center\'></leaflet>'); |
65 | | - element = $compile(element)(scope); |
66 | | - scope.$digest(); |
67 | | - return leafletData.getMap().then(function(map) { |
68 | | - expect(map.getZoom()).toEqual(center.zoom); |
69 | | - expect(map.getCenter().lat).toBeCloseTo(0.96658, 4); |
70 | | - return expect(map.getCenter().lng).toBeCloseTo(2.02, 4); |
71 | | - }); |
72 | | - }); |
73 | | - |
74 | | - it('should update the map center if the scope center properties changes', function() { |
75 | | - var element, map; |
76 | | - element = angular.element('<leaflet ' + directiveName + '=\'center\'></leaflet>'); |
77 | | - element = $compile(element)(scope); |
78 | | - map = void 0; |
79 | | - leafletData.getMap().then(function(leafletMap) { |
80 | | - return map = leafletMap; |
81 | | - }); |
82 | | - |
83 | | - scope.$apply(); |
84 | | - expect(map.getCenter().lat).toBeCloseTo(0.96658, 4); |
85 | | - expect(map.getCenter().lng).toBeCloseTo(2.02, 4); |
86 | | - expect(map.getZoom()).toEqual(4); |
87 | | - center.lat = 2.02999; |
88 | | - center.lng = 4.04; |
89 | | - center.zoom = 8; |
90 | | - scope.$digest(); |
91 | | - expect(map.getCenter().lat).toBeCloseTo(2.02999, 4); |
92 | | - expect(map.getCenter().lng).toBeCloseTo(4.04, 4); |
93 | | - return expect(map.getZoom()).toEqual(8); |
94 | | - }); |
95 | | - |
96 | | - return describe('Using url-hash functionality', function() { |
97 | | - return it('should update the center of the map if changes the url', function() { |
98 | | - var centerParams, element, map; |
99 | | - element = angular.element('<leaflet ' + directiveName + '=\'center\' url-hash-center=\'yes\'></leaflet>'); |
100 | | - element = $compile(element)(scope); |
101 | | - map = void 0; |
102 | | - leafletData.getMap().then(function(leafletMap) { |
103 | | - return map = leafletMap; |
104 | | - }); |
105 | | - |
106 | | - centerParams = { |
107 | | - c: '30.1' + ':' + '-9.2' + ':' + '4', |
108 | | - }; |
109 | | - $location.search(centerParams); |
110 | | - scope.$digest(); |
111 | | - expect(map.getCenter().lat).toBeCloseTo(30.1, 4); |
112 | | - expect(map.getCenter().lng).toBeCloseTo(-9.2, 4); |
113 | | - return expect(map.getZoom()).toEqual(4); |
114 | | - }); |
115 | | - }); |
116 | | - }); |
117 | | - |
118 | | - return describe('sets scope from leaflet', function() { |
119 | | - it('should update the url hash if changes the center', function() { |
120 | | - var centerUrlHash, element; |
121 | | - element = angular.element('<leaflet ' + directiveName + '=\'center\' url-hash-center=\'yes\'></leaflet>'); |
122 | | - element = $compile(element)(scope); |
123 | | - scope.center = { |
124 | | - lat: 9.52478, |
125 | | - lng: -1.8, |
126 | | - zoom: 8, |
127 | | - }; |
128 | | - centerUrlHash = void 0; |
129 | | - scope.$on('centerUrlHash', function(event, u) { |
130 | | - return centerUrlHash = u; |
131 | | - }); |
132 | | - |
133 | | - scope.$digest(); |
134 | | - return expect(centerUrlHash).toBe('9.5248:-1.8000:8'); |
135 | | - }); |
136 | | - |
137 | | - return it('should update the scope.center if leaflet map is moved', function(done) { |
138 | | - var element; |
139 | | - element = angular.element('<leaflet ' + directiveName + '=\'center\'></leaflet>'); |
140 | | - element = $compile(element)(scope); |
141 | | - this.digest(scope, function() { |
142 | | - return leafletData.getMap().then(function(map) { |
143 | | - expect(map.getZoom()).toEqual(center.zoom); |
144 | | - expect(map.getCenter().lat).toBeCloseTo(0.96658, 4); |
145 | | - expect(map.getCenter().lng).toBeCloseTo(2.02, 4); |
146 | | - return map.setView(L.latLng(50.5, 30.5)); |
147 | | - }); |
148 | | - }); |
149 | | - |
150 | | - this.digest(scope, function() { |
151 | | - return leafletData.getMap().then(function(map) { |
152 | | - expect(scope.center.lat).toBe(50.5); |
153 | | - expect(scope.center.lng).toBe(30.5); |
154 | | - return map.setView(L.latLng(51.5, 31.5)); |
155 | | - }); |
156 | | - }); |
157 | | - |
158 | | - return this.digest(scope, function() { |
159 | | - return leafletData.getMap().then(function(map) { |
160 | | - expect(scope.center.lat).toBe(51.5); |
161 | | - expect(scope.center.lng).toBe(31.5); |
162 | | - return done(); |
163 | | - }); |
164 | | - }); |
165 | | - }); |
166 | | - }); |
167 | | - })); |
168 | | - } |
169 | | - |
170 | | - return results; |
| 108 | + it('should update the url hash if changes the center', function() { |
| 109 | + var element = angular.element('<leaflet center="center" url-hash-center="yes"></leaflet>'); |
| 110 | + element = $compile(element)(scope); |
| 111 | + scope.center = { lat: 9.52478, lng: -1.8, zoom: 8 }; |
| 112 | + var centerUrlHash; |
| 113 | + scope.$on('centerUrlHash', function(event, u) { |
| 114 | + centerUrlHash = u; |
| 115 | + }); |
| 116 | + |
| 117 | + scope.$digest(); |
| 118 | + expect(centerUrlHash).toBe('9.5248:-1.8000:8'); |
| 119 | + }); |
171 | 120 | }); |
| 121 | +}); |
0 commit comments