forked from RumataEstor/ngGeolocation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngGeolocation.test.js
197 lines (173 loc) · 7.24 KB
/
ngGeolocation.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
describe('$geolocation', function() {
var $geolocation;
var $rootScope;
var $window;
beforeEach(function() {
angular.module('test',[]);
module('ngGeolocation', 'test');
inject(function($injector) {
$geolocation = $injector.get('$geolocation');
$rootScope = $injector.get('$rootScope');
$window = $injector.get('$window');
});
});
it('should be defined', function() {
expect($geolocation).toBeDefined();
});
describe('getCurrentPosition', function() {
var successCallback;
var errorCallback;
var optionsObj;
beforeEach(function() {
$window.navigator.geolocation = {
getCurrentPosition: function(success, error, options) {}
}
spyOn($window.navigator.geolocation, 'getCurrentPosition')
.andCallFake(function(success, error, options) {
successCallback = success;
errorCallback = error;
optionsObj = options;
});
});
it('should be defined', function() {
expect($geolocation.getCurrentPosition).toBeDefined();
});
it('should pass along the options object', function() {
var expected = {badger: 'goat'};
$geolocation.getCurrentPosition(expected);
expect(optionsObj).toBe(expected);
});
it('should resolve the promise upon success', function() {
var result = null;
var expected = {
coords: {},
timestamp: {}
};
$geolocation
.getCurrentPosition()
.then(function(position) { result = position; });
successCallback(expected);
$rootScope.$apply();
expect(result).toBe(expected);
});
it('should reject the promise upon error', function() {
var result = null;
var expected = {
code: {},
message: {}
};
$geolocation
.getCurrentPosition()
.then(function() {}, function(error) { result = error; });
errorCallback(expected);
$rootScope.$apply();
expect(result.error).toBe(expected);
});
it('should reject the promise if geolocation is unsupported', function() {
delete $window.navigator.geolocation;
var result;
$geolocation
.getCurrentPosition()
.then(function() {}, function(error) { result = error; });
$rootScope.$apply();
expect(result.error.code).toBe(2);
expect(result.error.message).toBe('This web browser does not support HTML5 Geolocation');
});
});
describe('watchPosition', function() {
var successCallback;
var errorCallback;
var optionsObj;
beforeEach(function() {
$window.navigator.geolocation = {
watchPosition: function(success, error, options) {}
}
spyOn($window.navigator.geolocation, 'watchPosition')
.andCallFake(function(success, error, options) {
successCallback = success;
errorCallback = error;
optionsObj = options;
return 6748678934;
});
});
it('should be defined', function() {
expect($geolocation.watchPosition).toBeDefined();
});
it('should pass along the options object', function() {
var expected = {badger: 'goat'};
$geolocation.watchPosition(expected);
expect(optionsObj).toBe(expected);
});
it('should only set up one watch at a time', function() {
$geolocation.watchPosition();
$geolocation.watchPosition();
expect($window.navigator.geolocation.watchPosition.calls.length).toBe(1);
});
it('should expose the error on position if an error occurs', function() {
var expected = {code: 1, message: "badger"};
$geolocation.watchPosition();
errorCallback(expected);
expect($geolocation.position.error.code).toBe(expected.code);
expect($geolocation.position.error.message).toBe(expected.message);
});
it('should expose the error on position if geolocation is unsupported', function() {
delete $window.navigator.geolocation;
$geolocation.watchPosition();
expect($geolocation.position.error.code).toBe(2);
expect($geolocation.position.error.message).toBe('This web browser does not support HTML5 Geolocation');
});
it('should expose the position when updated', function() {
var expected = {coords: 'badger', timestamp: 'goat'};
$geolocation.watchPosition();
successCallback(expected);
expect($geolocation.position.coords).toBe(expected.coords);
expect($geolocation.position.timestamp).toBe(expected.timestamp);
});
it('should broadcast \'$geolocation.position.changed\' when updated', function() {
var expected = {
result: {coords: 'badger', timestamp: 'goat'},
callback: function() {}
};
spyOn(expected, 'callback');
$rootScope.$on('$geolocation.position.changed', expected.callback);
$geolocation.watchPosition();
successCallback(expected.result);
expect(expected.callback).toHaveBeenCalledWith(jasmine.any(Object), expected.result);
});
it('should broadcast \'$geolocation.position.error\' when an error occurs', function() {
var expected = {
result: {coords: 'badger', timestamp: 'goat'},
callback: function() {}
};
spyOn(expected, 'callback');
$rootScope.$on('$geolocation.position.error', expected.callback);
$geolocation.watchPosition();
errorCallback(expected.result);
expect(expected.callback).toHaveBeenCalledWith(jasmine.any(Object), expected.result);
});
});
describe('clearWatch', function() {
var expected = 648639;
beforeEach(function() {
$window.navigator.geolocation = {
watchPosition: function(success, error, options) {},
clearWatch: function(watchId) {}
}
spyOn($window.navigator.geolocation, 'watchPosition').andReturn(expected);
spyOn($window.navigator.geolocation, 'clearWatch');
$geolocation.watchPosition();
});
it('should be defined', function() {
expect($geolocation.clearWatch).toBeDefined();
});
it('should cancel the current watch', function() {
$geolocation.clearWatch();
expect($window.navigator.geolocation.clearWatch).toHaveBeenCalledWith(expected);
});
it('should not cancel if there is no current watch', function() {
$geolocation.clearWatch();
$geolocation.clearWatch();
expect($window.navigator.geolocation.clearWatch.calls.length).toBe(1);
})
});
});