Skip to content

Commit

Permalink
fix(FitBounds): add null check for null fit-bounds input
Browse files Browse the repository at this point in the history
Don't try to fit bounds if provided bounds are empty (was 
causing GM APIs to throw an error)
  • Loading branch information
mpienkowski authored and doom777 committed Sep 4, 2019
1 parent 23c42cd commit 2bf73de
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/core/directives/map.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CUSTOM_ELEMENTS_SCHEMA, ElementRef, Injectable } from '@angular/core';
import { CUSTOM_ELEMENTS_SCHEMA, ElementRef, Injectable, SimpleChange } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { Subject } from 'rxjs';
Expand All @@ -17,6 +17,8 @@ class MockGoogleMapsAPIWrapper {
clearInstanceListeners = jest.fn();
subscribeToMapEvent = jest.fn().mockReturnValue(new Subject());
getNativeMap = jest.fn().mockReturnValue(Promise.resolve({}));
fitBounds = jest.fn().mockReturnValue(Promise.resolve({}));
setMapOptions = jest.fn();
}

@Injectable()
Expand Down Expand Up @@ -58,4 +60,15 @@ describe('AgmMap', () => {
expect(mockApiWrapper.createMap.mock.calls[0][1].zoomControl).not.toBe(true);
});

it('should not fit bounds if provided bounds are empty', () => {
component.fitBounds = null;
component.ngOnChanges({
latitude: new SimpleChange(null, 1, false),
longitude: new SimpleChange(null, 2, false),
fitBounds: new SimpleChange({}, null, false),
});

const mockApiWrapper: MockGoogleMapsAPIWrapper = fixture.debugElement.injector.get(GoogleMapsAPIWrapper) as any;
expect(mockApiWrapper.fitBounds.mock.calls).toHaveLength(0);
});
});
3 changes: 3 additions & 0 deletions packages/core/directives/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,9 @@ export class AgmMap implements OnChanges, OnInit, OnDestroy {
}

protected _updateBounds(bounds: LatLngBounds | LatLngBoundsLiteral) {
if (!bounds) {
return;
}
if (this._isLatLngBoundsLiteral(bounds) && typeof google !== 'undefined' && google && google.maps && google.maps.LatLngBounds) {
const newBounds = new google.maps.LatLngBounds();
newBounds.union(bounds);
Expand Down

0 comments on commit 2bf73de

Please sign in to comment.