UNPKG

8.77 kBJavaScriptView Raw
1const createMockFuncsFromArray = (instance, names = []) => {
2 names.forEach(name => {
3 instance[name] = jest.fn().mockName(name)
4 })
5}
6
7const createGoogleMapsMock = (libraries = []) => {
8 const createMVCObject = instance => {
9 const listeners = {}
10 instance.listeners = listeners
11
12 instance.addListener = jest
13 .fn((event, fn) => {
14 listeners[event] = listeners[event] || []
15 listeners[event].push(fn)
16 return {
17 remove: () => {
18 const index = listeners[event].indexOf(fn)
19
20 if (index !== -1) {
21 listeners[event].splice(index, 1)
22 }
23 },
24 }
25 })
26 .mockName('addListener')
27
28 createMockFuncsFromArray(instance, [
29 'bindTo',
30 'get',
31 'notify',
32 'set',
33 'setValues',
34 'unbind',
35 'unbindAll',
36 ])
37 }
38
39 const maps = {
40 Animation: {
41 BOUNCE: 1,
42 DROP: 2,
43 Lo: 3,
44 Go: 4,
45 },
46 BicyclingLayer: jest.fn().mockImplementation(function() {
47 createMVCObject(this)
48 createMockFuncsFromArray(this, ['setMap'])
49 }),
50 TransitLayer: jest.fn().mockImplementation(function() {
51 createMVCObject(this)
52 createMockFuncsFromArray(this, ['setMap'])
53 }),
54 Circle: jest.fn().mockImplementation(function(opts) {
55 this.opts = opts
56 createMVCObject(this)
57 createMockFuncsFromArray(this, [
58 'setCenter',
59 'setDraggable',
60 'setEditable',
61 'setMap',
62 'setOptions',
63 'setRadius',
64 'setVisible',
65 ])
66 }),
67 ControlPosition: {
68 TOP_LEFT: 1,
69 TOP_CENTER: 2,
70 TOP: 2,
71 TOP_RIGHT: 3,
72 LEFT_CENTER: 4,
73 LEFT: 5,
74 LEFT_TOP: 5,
75 LEFT_BOTTOM: 6,
76 RIGHT: 7,
77 RIGHT_CENTER: 8,
78 RIGHT_BOTTOM: 9,
79 BOTTOM_LEFT: 10,
80 BOTTOM: 11,
81 BOTTOM_CENTER: 11,
82 BOTTOM_RIGHT: 12,
83 CENTER: 13,
84 },
85 Data: jest.fn().mockImplementation(function(options) {
86 this.options = options
87 createMVCObject(this)
88 createMockFuncsFromArray(this, [
89 'setControlPosition',
90 'setControls',
91 'setDrawingMode',
92 'setMap',
93 'setStyle',
94 ])
95 }),
96 DirectionsRenderer: jest.fn().mockImplementation(function(opts) {
97 this.opts = opts
98 createMVCObject(this)
99 createMockFuncsFromArray(this, [
100 'setDirections',
101 'setMap',
102 'setOptions',
103 'setPanel',
104 'setRouteIndex',
105 ])
106 }),
107 DirectionsService: function() {},
108 DirectionsStatus: {
109 INVALID_REQUEST: 'INVALID_REQUEST',
110 MAX_WAYPOINTS_EXCEEDED: 'MAX_WAYPOINTS_EXCEEDED',
111 NOT_FOUND: 'NOT_FOUND',
112 OK: 'OK',
113 OVER_QUERY_LIMIT: 'OVER_QUERY_LIMIT',
114 REQUEST_DENIED: 'REQUEST_DENIED',
115 UNKNOWN_ERROR: 'UNKNOWN_ERROR',
116 ZERO_RESULTS: 'ZERO_RESULTS',
117 },
118 DirectionsTravelMode: {
119 BICYCLING: 'BICYCLING',
120 DRIVING: 'DRIVING',
121 TRANSIT: 'TRANSIT',
122 WALKING: 'WALKING',
123 },
124 DirectionsUnitSystem: {
125 IMPERIAL: 1,
126 METRIC: 0,
127 },
128 DistanceMatrixElementStatus: {
129 NOT_FOUND: 'NOT_FOUND',
130 OK: 'OK',
131 ZERO_RESULTS: 'ZERO_RESULTS',
132 },
133 DistanceMatrixService: function() {},
134 DistanceMatrixStatus: {
135 INVALID_REQUEST: 'INVALID_REQUEST',
136 MAX_DIMENSIONS_EXCEEDED: 'MAX_DIMENSIONS_EXCEEDED',
137 MAX_ELEMENTS_EXCEEDED: 'MAX_ELEMENTS_EXCEEDED',
138 OK: 'OK',
139 OVER_QUERY_LIMIT: 'OVER_QUERY_LIMIT',
140 REQUEST_DENIED: 'REQUEST_DENIED',
141 UNKNOWN_ERROR: 'UNKNOWN_ERROR',
142 },
143 ElevationService: function() {},
144 ElevationStatus: {
145 Co: 'DATA_NOT_AVAILABLE',
146 INVALID_REQUEST: 'INVALID_REQUEST',
147 OK: 'OK',
148 OVER_QUERY_LIMIT: 'OVER_QUERY_LIMIT',
149 REQUEST_DENIED: 'REQUEST_DENIED',
150 UNKNOWN_ERROR: 'UNKNOWN_ERROR',
151 },
152 FusionTablesLayer: jest.fn().mockImplementation(function(options) {
153 this.options = options
154 createMVCObject(this)
155 createMockFuncsFromArray(this, ['setMap', 'setOptions'])
156 }),
157 Geocoder: function() {},
158 GeocoderLocationType: {
159 APPROXIMATE: 'APPROXIMATE',
160 GEOMETRIC_CENTER: 'GEOMETRIC_CENTER',
161 RANGE_INTERPOLATED: 'RANGE_INTERPOLATED',
162 ROOFTOP: 'ROOFTOP',
163 },
164 GeocoderStatus: {
165 ERROR: 'ERROR',
166 INVALID_REQUEST: 'INVALID_REQUEST',
167 OK: 'OK',
168 OVER_QUERY_LIMIT: 'OVER_QUERY_LIMIT',
169 REQUEST_DENIED: 'REQUEST_DENIED',
170 UNKNOWN_ERROR: 'UNKNOWN_ERROR',
171 ZERO_RESULTS: 'ZERO_RESULTS',
172 },
173 GroundOverlay: function() {},
174 ImageMapType: function() {},
175 InfoWindow: function() {},
176 KmlLayer: function() {},
177 KmlLayerStatus: {
178 DOCUMENT_NOT_FOUND: 'DOCUMENT_NOT_FOUND',
179 DOCUMENT_TOO_LARGE: 'DOCUMENT_TOO_LARGE',
180 FETCH_ERROR: 'FETCH_ERROR',
181 INVALID_DOCUMENT: 'INVALID_DOCUMENT',
182 INVALID_REQUEST: 'INVALID_REQUEST',
183 LIMITS_EXCEEDED: 'LIMITS_EXCEEDED',
184 OK: 'OK',
185 TIMED_OUT: 'TIMED_OUT',
186 UNKNOWN: 'UNKNOWN',
187 },
188 LatLng: function() {},
189 LatLngBounds: function() {},
190 MVCArray: function() {},
191 MVCObject: jest.fn().mockImplementation(function() {
192 createMVCObject(this)
193 }),
194 Map: jest.fn().mockImplementation(function(mapDiv, opts) {
195 this.mapDiv = mapDiv
196 this.opts = opts
197 createMVCObject(this)
198 createMockFuncsFromArray(this, [
199 'setCenter',
200 'setClickableIcons',
201 'setHeading',
202 'setMapTypeId',
203 'setOptions',
204 'setStreetView',
205 'setTilt',
206 'setZoom',
207 'fitBounds',
208 'getBounds',
209 'panToBounds',
210 ])
211 }),
212 MapTypeControlStyle: {
213 DEFAULT: 0,
214 DROPDOWN_MENU: 2,
215 HORIZONTAL_BAR: 1,
216 INSET: 3,
217 INSET_LARGE: 4,
218 },
219 MapTypeId: {
220 HYBRID: 'hybrid',
221 ROADMAP: 'roadmap',
222 SATELLITE: 'satellite',
223 TERRAIN: 'terrain',
224 },
225 MapTypeRegistry: function() {},
226 Marker: jest.fn().mockImplementation(function(opts) {
227 this.opts = opts
228 createMVCObject(this)
229 createMockFuncsFromArray(this, [
230 'setMap',
231 'setOpacity',
232 'setOptions',
233 'setPosition',
234 'setShape',
235 'setTitle',
236 'setVisible',
237 'setZIndex',
238 ])
239 }),
240 MarkerImage: function() {},
241 MaxZoomService: function() {
242 return {
243 getMaxZoomAtLatLng: function() {},
244 }
245 },
246 MaxZoomStatus: {
247 ERROR: 'ERROR',
248 OK: 'OK',
249 },
250 NavigationControlStyle: {
251 ANDROID: 2,
252 DEFAULT: 0,
253 Mo: 4,
254 SMALL: 1,
255 ZOOM_PAN: 3,
256 ik: 5,
257 },
258 OverlayView: function() {},
259 Point: function() {},
260 Polygon: function() {},
261 Polyline: function() {},
262 Rectangle: function() {},
263 SaveWidget: function() {},
264 ScaleControlStyle: {
265 DEFAULT: 0,
266 },
267 Size: function() {},
268 StreetViewCoverageLayer: function() {},
269 StreetViewPanorama: function() {},
270 StreetViewPreference: {
271 BEST: 'best',
272 NEAREST: 'nearest',
273 },
274 StreetViewService: function() {},
275 StreetViewSource: {
276 DEFAULT: 'default',
277 OUTDOOR: 'outdoor',
278 },
279 StreetViewStatus: {
280 OK: 'OK',
281 UNKNOWN_ERROR: 'UNKNOWN_ERROR',
282 ZERO_RESULTS: 'ZERO_RESULTS',
283 },
284 StrokePosition: {
285 CENTER: 0,
286 INSIDE: 1,
287 OUTSIDE: 2,
288 },
289 StyledMapType: function() {},
290 SymbolPath: {
291 BACKWARD_CLOSED_ARROW: 3,
292 BACKWARD_OPEN_ARROW: 4,
293 CIRCLE: 0,
294 FORWARD_CLOSED_ARROW: 1,
295 FORWARD_OPEN_ARROW: 2,
296 },
297 TrafficLayer: jest.fn().mockImplementation(function(opts) {
298 this.opts = opts
299 createMVCObject(this)
300 createMockFuncsFromArray(this, ['setMap', 'setOptions'])
301 }),
302 TrafficModel: {
303 BEST_GUESS: 'bestguess',
304 OPTIMISTIC: 'optimistic',
305 PESSIMISTIC: 'pessimistic',
306 },
307 TransitMode: {
308 BUS: 'BUS',
309 RAIL: 'RAIL',
310 SUBWAY: 'SUBWAY',
311 TRAIN: 'TRAIN',
312 TRAM: 'TRAM',
313 },
314 TransitRoutePreference: {
315 FEWER_TRANSFERS: 'FEWER_TRANSFERS',
316 LESS_WALKING: 'LESS_WALKING',
317 },
318 TravelMode: {
319 BICYCLING: 'BICYCLING',
320 DRIVING: 'DRIVING',
321 TRANSIT: 'TRANSIT',
322 WALKING: 'WALKING',
323 },
324 UnitSystem: {
325 IMPERIAL: 1,
326 METRIC: 0,
327 },
328 ZoomControlStyle: {
329 DEFAULT: 0,
330 LARGE: 2,
331 SMALL: 1,
332 ik: 3,
333 },
334 __gjsload__: function() {},
335 event: {
336 clearInstanceListeners: jest.fn().mockName('clearInstanceListeners'),
337 },
338 }
339
340 if (libraries.includes('places')) {
341 maps.places = {
342 AutocompleteService: jest.fn(() => ({
343 getPlacePredictions: jest.fn(),
344 })),
345 }
346 }
347
348 return maps
349}
350
351window.google = {
352 maps: createGoogleMapsMock(),
353}