Skip to content

Commit 52a1ef3

Browse files
authored
fix(google-maps): use console.error instead of hard error (#33917)
Switches from throwing an error in a promise to using `console.error` since the former basically guarantees that the promise will be unhandled.
1 parent cb45083 commit 52a1ef3

4 files changed

Lines changed: 55 additions & 41 deletions

File tree

‎src/google-maps/deprecated-map-marker-clusterer/deprecated-map-marker-clusterer.spec.ts‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Component, ViewChild, ChangeDetectionStrategy} from '@angular/core';
2-
import {ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing';
2+
import {ComponentFixture, TestBed} from '@angular/core/testing';
33

44
import {DEFAULT_OPTIONS, GoogleMap} from '../google-map/google-map';
55
import {MapMarker} from '../map-marker/map-marker';
@@ -51,19 +51,23 @@ describe('DeprecatedMapMarkerClusterer', () => {
5151
(window as any).MarkerClusterer = undefined;
5252
});
5353

54-
// We can't test this one easily without `fakeAsync`.
55-
it('throws an error if the clustering library has not been loaded', fakeAsync(() => {
54+
it('throws an error if the clustering library has not been loaded', async () => {
55+
const spy = spyOn(console, 'error');
5656
(window as any).MarkerClusterer = undefined;
5757
markerClustererConstructorSpy = createDeprecatedMarkerClustererConstructorSpy(
5858
markerClustererSpy,
5959
false,
6060
);
6161

62-
expect(() => {
63-
fixture.detectChanges();
64-
flush();
65-
}).toThrowError(/MarkerClusterer class not found, cannot construct a marker cluster/);
66-
}));
62+
fixture.detectChanges();
63+
await fixture.whenStable();
64+
65+
expect(spy).toHaveBeenCalledWith(
66+
jasmine.stringContaining(
67+
'MarkerClusterer class not found, cannot construct a marker cluster',
68+
),
69+
);
70+
});
6771

6872
it('initializes a Google Map Marker Clusterer', async () => {
6973
fixture.detectChanges();

‎src/google-maps/deprecated-map-marker-clusterer/deprecated-map-marker-clusterer.ts‎

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -215,33 +215,35 @@ export class DeprecatedMapMarkerClusterer
215215
new EventEmitter<MarkerClustererInstance>();
216216

217217
ngOnInit() {
218-
if (this._canInitialize) {
219-
this._ngZone.runOutsideAngular(() => {
220-
this._googleMap._resolveMap().then(map => {
221-
if (
222-
typeof MarkerClusterer !== 'function' &&
223-
(typeof ngDevMode === 'undefined' || ngDevMode)
224-
) {
225-
throw Error(
218+
if (!this._canInitialize) {
219+
return;
220+
}
221+
222+
this._ngZone.runOutsideAngular(() => {
223+
this._googleMap._resolveMap().then(map => {
224+
if (typeof MarkerClusterer !== 'function') {
225+
if (typeof ngDevMode === 'undefined' || ngDevMode) {
226+
console.error(
226227
'MarkerClusterer class not found, cannot construct a marker cluster. ' +
227228
'Please install the MarkerClustererPlus library: ' +
228229
'https://github.com/googlemaps/js-markerclustererplus',
229230
);
230231
}
232+
return;
233+
}
231234

232-
// Create the object outside the zone so its events don't trigger change detection.
233-
// We'll bring it back in inside the `MapEventManager` only for the events that the
234-
// user has subscribed to.
235-
this.markerClusterer = this._ngZone.runOutsideAngular(() => {
236-
return new MarkerClusterer(map, [], this._combineOptions());
237-
});
238-
239-
this._assertInitialized();
240-
this._eventManager.setTarget(this.markerClusterer);
241-
this.markerClustererInitialized.emit(this.markerClusterer);
235+
// Create the object outside the zone so its events don't trigger change detection.
236+
// We'll bring it back in inside the `MapEventManager` only for the events that the
237+
// user has subscribed to.
238+
this.markerClusterer = this._ngZone.runOutsideAngular(() => {
239+
return new MarkerClusterer(map, [], this._combineOptions());
242240
});
241+
242+
this._assertInitialized();
243+
this._eventManager.setTarget(this.markerClusterer);
244+
this.markerClustererInitialized.emit(this.markerClusterer);
243245
});
244-
}
246+
});
245247
}
246248

247249
ngAfterContentInit() {

‎src/google-maps/map-marker-clusterer/map-marker-clusterer.spec.ts‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Component, ViewChild, ChangeDetectionStrategy} from '@angular/core';
2-
import {ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing';
2+
import {ComponentFixture, TestBed} from '@angular/core/testing';
33
import type {MarkerClusterer, Renderer, Algorithm} from './map-marker-clusterer-types';
44

55
import {DEFAULT_OPTIONS} from '../google-map/google-map';
@@ -46,19 +46,23 @@ describe('MapMarkerClusterer', () => {
4646
(window as any).markerClusterer = undefined;
4747
});
4848

49-
// We can't test this easily without `fakeAsync`.
50-
it('throws an error if the clustering library has not been loaded', fakeAsync(() => {
49+
it('throws an error if the clustering library has not been loaded', async () => {
50+
const spy = spyOn(console, 'error');
5151
(window as any).markerClusterer = undefined;
5252
markerClustererConstructorSpy = createMarkerClustererConstructorSpy(
5353
markerClustererSpy,
5454
false,
5555
).and.callThrough();
5656

57-
expect(() => {
58-
fixture.detectChanges();
59-
flush();
60-
}).toThrowError(/MarkerClusterer class not found, cannot construct a marker cluster/);
61-
}));
57+
fixture.detectChanges();
58+
await fixture.whenStable();
59+
60+
expect(spy).toHaveBeenCalledWith(
61+
jasmine.stringContaining(
62+
'MarkerClusterer class not found, cannot construct a marker cluster',
63+
),
64+
);
65+
});
6266

6367
it('initializes a Google Map Marker Clusterer', async () => {
6468
fixture.detectChanges();

‎src/google-maps/map-marker-clusterer/map-marker-clusterer.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,16 @@ export class MapMarkerClusterer implements OnInit, OnChanges, OnDestroy {
125125
}
126126

127127
private async _createCluster() {
128-
if (!markerClusterer?.MarkerClusterer && (typeof ngDevMode === 'undefined' || ngDevMode)) {
129-
throw Error(
130-
'MarkerClusterer class not found, cannot construct a marker cluster. ' +
131-
'Please install the MarkerClusterer library: ' +
132-
'https://github.com/googlemaps/js-markerclusterer',
133-
);
128+
if (!markerClusterer?.MarkerClusterer) {
129+
if (typeof ngDevMode === 'undefined' || ngDevMode) {
130+
console.error(
131+
'MarkerClusterer class not found, cannot construct a marker cluster. ' +
132+
'Please install the MarkerClusterer library: ' +
133+
'https://github.com/googlemaps/js-markerclusterer',
134+
);
135+
}
136+
137+
return;
134138
}
135139

136140
const map = await this._googleMap._resolveMap();

0 commit comments

Comments
 (0)