/
fake-google-map-utils.ts
239 lines (223 loc) · 8.43 KB
/
fake-google-map-utils.ts
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {UpdatedGoogleMap} from '../google-map/google-map';
/** Window interface for testing */
export interface TestingWindow extends Window {
google?: {
maps: {
Map?: jasmine.Spy;
Marker?: jasmine.Spy;
InfoWindow?: jasmine.Spy;
Polyline?: jasmine.Spy;
Polygon?: jasmine.Spy;
Rectangle?: jasmine.Spy;
Circle?: jasmine.Spy;
};
};
}
/** Creates a jasmine.SpyObj for a google.maps.Map. */
export function createMapSpy(options: google.maps.MapOptions): jasmine.SpyObj<UpdatedGoogleMap> {
const mapSpy = jasmine.createSpyObj('google.maps.Map', [
'setOptions', 'setCenter', 'setZoom', 'setMap', 'addListener', 'fitBounds', 'panBy', 'panTo',
'panToBounds', 'getBounds', 'getCenter', 'getClickableIcons', 'getHeading', 'getMapTypeId',
'getProjection', 'getStreetView', 'getTilt', 'getZoom', 'setMapTypeId'
]);
mapSpy.addListener.and.returnValue({remove: () => {}});
return mapSpy;
}
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.Map. */
export function createMapConstructorSpy(
mapSpy: jasmine.SpyObj<UpdatedGoogleMap>, apiLoaded = true): jasmine.Spy {
const mapConstructorSpy =
jasmine.createSpy('Map constructor', (_el: Element, _options: google.maps.MapOptions) => {
return mapSpy;
});
const testingWindow: TestingWindow = window;
if (apiLoaded) {
testingWindow.google = {
maps: {
'Map': mapConstructorSpy,
}
};
}
return mapConstructorSpy;
}
/** Creates a jasmine.SpyObj for a google.maps.Marker */
export function createMarkerSpy(options: google.maps.MarkerOptions):
jasmine.SpyObj<google.maps.Marker> {
const markerSpy = jasmine.createSpyObj('google.maps.Marker', [
'setOptions', 'setMap', 'addListener', 'getAnimation', 'getClickable', 'getCursor',
'getDraggable', 'getIcon', 'getLabel', 'getOpacity', 'getPosition', 'getShape', 'getTitle',
'getVisible', 'getZIndex'
]);
markerSpy.addListener.and.returnValue({remove: () => {}});
return markerSpy;
}
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.Marker */
export function createMarkerConstructorSpy(markerSpy: jasmine.SpyObj<google.maps.Marker>):
jasmine.Spy {
const markerConstructorSpy =
jasmine.createSpy('Marker constructor', (_options: google.maps.MarkerOptions) => {
return markerSpy;
});
const testingWindow: TestingWindow = window;
if (testingWindow.google && testingWindow.google.maps) {
testingWindow.google.maps['Marker'] = markerConstructorSpy;
} else {
testingWindow.google = {
maps: {
'Marker': markerConstructorSpy,
},
};
}
return markerConstructorSpy;
}
/** Creates a jasmine.SpyObj for a google.maps.InfoWindow */
export function createInfoWindowSpy(options: google.maps.InfoWindowOptions):
jasmine.SpyObj<google.maps.InfoWindow> {
const infoWindowSpy = jasmine.createSpyObj(
'google.maps.InfoWindow',
['addListener', 'close', 'getContent', 'getPosition', 'getZIndex', 'open']);
infoWindowSpy.addListener.and.returnValue({remove: () => {}});
return infoWindowSpy;
}
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.InfoWindow */
export function createInfoWindowConstructorSpy(
infoWindowSpy: jasmine.SpyObj<google.maps.InfoWindow>): jasmine.Spy {
const infoWindowConstructorSpy =
jasmine.createSpy('InfoWindow constructor', (_options: google.maps.InfoWindowOptions) => {
return infoWindowSpy;
});
const testingWindow: TestingWindow = window;
if (testingWindow.google && testingWindow.google.maps) {
testingWindow.google.maps['InfoWindow'] = infoWindowConstructorSpy;
} else {
testingWindow.google = {
maps: {
'InfoWindow': infoWindowConstructorSpy,
},
};
}
return infoWindowConstructorSpy;
}
/** Creates a jasmine.SpyObj for a google.maps.Polyline */
export function createPolylineSpy(options: google.maps.PolylineOptions):
jasmine.SpyObj<google.maps.Polyline> {
const polylineSpy = jasmine.createSpyObj('google.maps.Polyline', [
'addListener', 'getDraggable', 'getEditable', 'getPath', 'getVisible', 'setMap', 'setOptions',
'setPath'
]);
polylineSpy.addListener.and.returnValue({remove: () => {}});
return polylineSpy;
}
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.Polyline */
export function createPolylineConstructorSpy(polylineSpy: jasmine.SpyObj<google.maps.Polyline>):
jasmine.Spy {
const polylineConstructorSpy =
jasmine.createSpy('Polyline constructor', (_options: google.maps.PolylineOptions) => {
return polylineSpy;
});
const testingWindow: TestingWindow = window;
if (testingWindow.google && testingWindow.google.maps) {
testingWindow.google.maps['Polyline'] = polylineConstructorSpy;
} else {
testingWindow.google = {
maps: {
'Polyline': polylineConstructorSpy,
},
};
}
return polylineConstructorSpy;
}
/** Creates a jasmine.SpyObj for a google.maps.Polygon */
export function createPolygonSpy(options: google.maps.PolygonOptions):
jasmine.SpyObj<google.maps.Polygon> {
const polygonSpy = jasmine.createSpyObj('google.maps.Polygon', [
'addListener', 'getDraggable', 'getEditable', 'getPath', 'getPaths', 'getVisible', 'setMap',
'setOptions', 'setPath'
]);
polygonSpy.addListener.and.returnValue({remove: () => {}});
return polygonSpy;
}
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.Polygon */
export function createPolygonConstructorSpy(polygonSpy: jasmine.SpyObj<google.maps.Polygon>):
jasmine.Spy {
const polygonConstructorSpy =
jasmine.createSpy('Polygon constructor', (_options: google.maps.PolygonOptions) => {
return polygonSpy;
});
const testingWindow: TestingWindow = window;
if (testingWindow.google && testingWindow.google.maps) {
testingWindow.google.maps['Polygon'] = polygonConstructorSpy;
} else {
testingWindow.google = {
maps: {
'Polygon': polygonConstructorSpy,
},
};
}
return polygonConstructorSpy;
}
/** Creates a jasmine.SpyObj for a google.maps.Rectangle */
export function createRectangleSpy(options: google.maps.RectangleOptions):
jasmine.SpyObj<google.maps.Rectangle> {
const rectangleSpy = jasmine.createSpyObj('google.maps.Rectangle', [
'addListener', 'getBounds', 'getDraggable', 'getEditable', 'getVisible', 'setMap', 'setOptions',
'setBounds'
]);
rectangleSpy.addListener.and.returnValue({remove: () => {}});
return rectangleSpy;
}
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.Rectangle */
export function createRectangleConstructorSpy(rectangleSpy: jasmine.SpyObj<google.maps.Rectangle>):
jasmine.Spy {
const rectangleConstructorSpy =
jasmine.createSpy('Rectangle constructor', (_options: google.maps.RectangleOptions) => {
return rectangleSpy;
});
const testingWindow: TestingWindow = window;
if (testingWindow.google && testingWindow.google.maps) {
testingWindow.google.maps['Rectangle'] = rectangleConstructorSpy;
} else {
testingWindow.google = {
maps: {
'Rectangle': rectangleConstructorSpy,
},
};
}
return rectangleConstructorSpy;
}
/** Creates a jasmine.SpyObj for a google.maps.Circle */
export function createCircleSpy(options: google.maps.CircleOptions):
jasmine.SpyObj<google.maps.Circle> {
const circleSpy = jasmine.createSpyObj('google.maps.Circle', [
'addListener', 'getCenter', 'getRadius', 'getDraggable', 'getEditable', 'getVisible', 'setMap',
'setOptions', 'setCenter', 'setRadius'
]);
circleSpy.addListener.and.returnValue({remove: () => {}});
return circleSpy;
}
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.Circle */
export function createCircleConstructorSpy(circleSpy: jasmine.SpyObj<google.maps.Circle>):
jasmine.Spy {
const circleConstructorSpy =
jasmine.createSpy('Circle constructor', (_options: google.maps.CircleOptions) => {
return circleSpy;
});
const testingWindow: TestingWindow = window;
if (testingWindow.google && testingWindow.google.maps) {
testingWindow.google.maps['Circle'] = circleConstructorSpy;
} else {
testingWindow.google = {
maps: {
'Circle': circleConstructorSpy,
},
};
}
return circleConstructorSpy;
}