Skip to content

Commit

Permalink
fix(PolylinePoint): prevent accessing undefined variable on change
Browse files Browse the repository at this point in the history
make every update to polyline point update lat & lng even when only one of them changed

fix #1597
  • Loading branch information
Mathieu St-Louis authored and doom777 committed Jun 3, 2019
1 parent 7922546 commit 21efc4f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
49 changes: 49 additions & 0 deletions packages/core/directives/polyline-point.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {SimpleChange, SimpleChanges} from '@angular/core';
import {AgmPolylinePoint} from './polyline-point';

describe('AgmPolylinePoint', () => {
describe('ngOnChanges', () => {
it('should emit positionChanged on latitude change', () => {
const polylinePoint = new AgmPolylinePoint();
polylinePoint.latitude = 50;
polylinePoint.longitude = -50;
polylinePoint.positionChanged.emit = jest.fn();

const positionChanges:
SimpleChanges = {'latitude': new SimpleChange('previousLat', 'newLat', false)};

polylinePoint.ngOnChanges(positionChanges);

expect(polylinePoint.positionChanged.emit).toHaveBeenCalledWith({lat: 'newLat', lng: -50});
});
it('should emit positionChanged on longitude change', () => {
const polylinePoint = new AgmPolylinePoint();
polylinePoint.latitude = 50;
polylinePoint.longitude = -50;
polylinePoint.positionChanged.emit = jest.fn();

const positionChanges:
SimpleChanges = {'longitude': new SimpleChange('previousLng', 'newLng', false)};

polylinePoint.ngOnChanges(positionChanges);

expect(polylinePoint.positionChanged.emit).toHaveBeenCalledWith({lat: 50, lng: 'newLng'});
});
it('should emit positionChanged on latitude and longitude change', () => {
const polylinePoint = new AgmPolylinePoint();
polylinePoint.latitude = 50;
polylinePoint.longitude = -50;
polylinePoint.positionChanged.emit = jest.fn();

const positionChanges: SimpleChanges = {
'latitude': new SimpleChange('previousLat', 'newLat', false),
'longitude': new SimpleChange('previousLng', 'newLng', false)
};

polylinePoint.ngOnChanges(positionChanges);

expect(polylinePoint.positionChanged.emit)
.toHaveBeenCalledWith({lat: 'newLat', lng: 'newLng'});
});
});
});
4 changes: 2 additions & 2 deletions packages/core/directives/polyline-point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export class AgmPolylinePoint implements OnChanges {
ngOnChanges(changes: SimpleChanges): any {
if (changes['latitude'] || changes['longitude']) {
const position: LatLngLiteral = <LatLngLiteral>{
lat: changes['latitude'].currentValue,
lng: changes['longitude'].currentValue
lat: changes['latitude'] ? changes['latitude'].currentValue : this.latitude,
lng: changes['longitude'] ? changes['longitude'].currentValue : this.longitude
};
this.positionChanged.emit(position);
}
Expand Down

0 comments on commit 21efc4f

Please sign in to comment.