Skip to content

Commit cd9f5eb

Browse files
authored
fix(google-maps): throw an error if clustering library hasn't been loaded (#23064)
Clustering in the Google Maps module requires a separate library, but we never check it's actually been loaded. These changes add an error to make it easier to debug. Relates to #23050.
1 parent 653f412 commit cd9f5eb

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ describe('MapMarkerClusterer', () => {
4848

4949
afterEach(() => {
5050
(window.google as any) = undefined;
51+
(window as any).MarkerClusterer = undefined;
52+
});
53+
54+
it('throws an error if the clustering library has not been loaded', () => {
55+
(window as any).MarkerClusterer = undefined;
56+
markerClustererConstructorSpy = createMarkerClustererConstructorSpy(markerClustererSpy, false)
57+
.and.callThrough();
58+
59+
expect(() => fixture.detectChanges())
60+
.toThrow(new Error(
61+
'MarkerClusterer class not found, cannot construct a marker cluster. ' +
62+
'Please install the MarkerClustererPlus library: ' +
63+
'https://github.com/googlemaps/js-markerclustererplus'));
5164
});
5265

5366
it('initializes a Google Map Marker Clusterer', () => {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,16 @@ export class MapMarkerClusterer implements OnInit, AfterContentInit, OnChanges,
198198

199199
ngOnInit() {
200200
if (this._canInitialize) {
201+
const clustererWindow =
202+
window as unknown as typeof globalThis & {MarkerClusterer?: MarkerClusterer};
203+
204+
if (!clustererWindow.MarkerClusterer && (typeof ngDevMode === 'undefined' || ngDevMode)) {
205+
throw Error(
206+
'MarkerClusterer class not found, cannot construct a marker cluster. ' +
207+
'Please install the MarkerClustererPlus library: ' +
208+
'https://github.com/googlemaps/js-markerclustererplus');
209+
}
210+
201211
// Create the object outside the zone so its events don't trigger change detection.
202212
// We'll bring it back in inside the `MapEventManager` only for the events that the
203213
// user has subscribed to.

src/google-maps/testing/fake-google-map-utils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,13 +442,15 @@ export function createMarkerClustererSpy(): jasmine.SpyObj<MarkerClusterer> {
442442

443443
/** Creates a jasmine.Spy to watch for the constructor of a MarkerClusterer */
444444
export function createMarkerClustererConstructorSpy(
445-
markerClustererSpy: jasmine.SpyObj<MarkerClusterer>): jasmine.Spy {
445+
markerClustererSpy: jasmine.SpyObj<MarkerClusterer>, apiLoaded = true): jasmine.Spy {
446446
const markerClustererConstructorSpy = jasmine.createSpy('MarkerClusterer constructor',
447447
() => {
448448
return markerClustererSpy;
449449
});
450-
const testingWindow: TestingWindow = window;
451-
testingWindow['MarkerClusterer'] = markerClustererConstructorSpy;
450+
if (apiLoaded) {
451+
const testingWindow: TestingWindow = window;
452+
testingWindow['MarkerClusterer'] = markerClustererConstructorSpy;
453+
}
452454
return markerClustererConstructorSpy;
453455
}
454456

0 commit comments

Comments
 (0)