Skip to content

fix(google-maps): throw an error if clustering library hasn't been loaded #23064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/google-maps/map-marker-clusterer/map-marker-clusterer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ describe('MapMarkerClusterer', () => {

afterEach(() => {
(window.google as any) = undefined;
(window as any).MarkerClusterer = undefined;
});

it('throws an error if the clustering library has not been loaded', () => {
(window as any).MarkerClusterer = undefined;
markerClustererConstructorSpy = createMarkerClustererConstructorSpy(markerClustererSpy, false)
.and.callThrough();

expect(() => fixture.detectChanges())
.toThrow(new Error(
'MarkerClusterer class not found, cannot construct a marker cluster. ' +
'Please install the MarkerClustererPlus library: ' +
'https://github.com/googlemaps/js-markerclustererplus'));
});

it('initializes a Google Map Marker Clusterer', () => {
Expand Down
10 changes: 10 additions & 0 deletions src/google-maps/map-marker-clusterer/map-marker-clusterer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ export class MapMarkerClusterer implements OnInit, AfterContentInit, OnChanges,

ngOnInit() {
if (this._canInitialize) {
const clustererWindow =
window as unknown as typeof globalThis & {MarkerClusterer?: MarkerClusterer};

if (!clustererWindow.MarkerClusterer && (typeof ngDevMode === 'undefined' || ngDevMode)) {
throw Error(
'MarkerClusterer class not found, cannot construct a marker cluster. ' +
'Please install the MarkerClustererPlus library: ' +
'https://github.com/googlemaps/js-markerclustererplus');
}

// Create the object outside the zone so its events don't trigger change detection.
// We'll bring it back in inside the `MapEventManager` only for the events that the
// user has subscribed to.
Expand Down
8 changes: 5 additions & 3 deletions src/google-maps/testing/fake-google-map-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,13 +442,15 @@ export function createMarkerClustererSpy(): jasmine.SpyObj<MarkerClusterer> {

/** Creates a jasmine.Spy to watch for the constructor of a MarkerClusterer */
export function createMarkerClustererConstructorSpy(
markerClustererSpy: jasmine.SpyObj<MarkerClusterer>): jasmine.Spy {
markerClustererSpy: jasmine.SpyObj<MarkerClusterer>, apiLoaded = true): jasmine.Spy {
const markerClustererConstructorSpy = jasmine.createSpy('MarkerClusterer constructor',
() => {
return markerClustererSpy;
});
const testingWindow: TestingWindow = window;
testingWindow['MarkerClusterer'] = markerClustererConstructorSpy;
if (apiLoaded) {
const testingWindow: TestingWindow = window;
testingWindow['MarkerClusterer'] = markerClustererConstructorSpy;
}
return markerClustererConstructorSpy;
}

Expand Down