Skip to content
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

fix(AgmMarker): animationChange output #1683

Merged
2 commits merged into from Jul 11, 2019
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
18 changes: 17 additions & 1 deletion packages/core/directives/marker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,14 @@ export class AgmMarker implements OnDestroy, OnChanges, AfterContentInit, FitBou
* Which animation to play when marker is added to a map.
* This can be 'BOUNCE' or 'DROP'
*/
animation: 'BOUNCE' | 'DROP' | null;
@Input() animation: Animation;

/**
* This event is fired when the marker's animation property changes.
*
* @memberof AgmMarker
*/
@Output() animationChange = new EventEmitter<Animation>();

/**
* This event emitter gets emitted when the user clicks on the marker.
Expand Down Expand Up @@ -279,6 +286,13 @@ export class AgmMarker implements OnDestroy, OnChanges, AfterContentInit, FitBou
this.mouseOut.emit(<MouseEvent>{coords: {lat: e.latLng.lat(), lng: e.latLng.lng()}});
});
this._observableSubscriptions.push(mout);

const anChng =
this._markerManager.createEventObservable<void>('animation_changed', this)
.subscribe(() => {
this.animationChange.emit(this.animation);
});
this._observableSubscriptions.push(anChng);
}

/** @internal */
Expand All @@ -294,3 +308,5 @@ export class AgmMarker implements OnDestroy, OnChanges, AfterContentInit, FitBou
this._observableSubscriptions.forEach((s) => s.unsubscribe());
}
}

export type Animation = 'BOUNCE' | 'DROP' | null;
86 changes: 71 additions & 15 deletions packages/core/services/managers/marker-manager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import {NgZone} from '@angular/core';
import {TestBed, async, inject} from '@angular/core/testing';
import {TestBed, inject, fakeAsync, flushMicrotasks, tick} from '@angular/core/testing';

import {AgmMarker} from './../../directives/marker';
import {GoogleMapsAPIWrapper} from './../google-maps-api-wrapper';
import {Marker} from './../google-maps-types';
import {MarkerManager} from './../managers/marker-manager';

describe('MarkerManager', () => {
const animMap = {
BOUNCE: 1,
DROP: 2,
};

beforeAll(() => {
(<any>window).google = {
maps: {
Animation: animMap,
}
};
});

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{provide: NgZone, useFactory: () => new NgZone({enableLongStackTrace: true})},
MarkerManager, {
provide: GoogleMapsAPIWrapper,
useValue: {
createMarker: jest.fn()
createMarker: jest.fn().mockReturnValue(Promise.resolve()),
getNativeMap: jest.fn().mockReturnValue(Promise.resolve()),
}
}
]
Expand All @@ -23,16 +36,16 @@ describe('MarkerManager', () => {

describe('Create a new marker', () => {
it('should call the mapsApiWrapper when creating a new marker',
inject(
fakeAsync(inject(
[MarkerManager, GoogleMapsAPIWrapper],
(markerManager: MarkerManager, apiWrapper: GoogleMapsAPIWrapper) => {
const newMarker = new AgmMarker(markerManager);
newMarker.latitude = 34.4;
newMarker.longitude = 22.3;
newMarker.label = 'A';
markerManager.addMarker(newMarker);

expect(apiWrapper.createMarker).toHaveBeenCalledWith({
flushMicrotasks();
expect(apiWrapper.createMarker).toHaveBeenCalledWith({
position: {lat: 34.4, lng: 22.3},
label: 'A',
draggable: false,
Expand All @@ -44,12 +57,12 @@ describe('MarkerManager', () => {
clickable: true,
animation: undefined
});
}));
})));
});

describe('Delete a marker', () => {
it('should set the map to null when deleting a existing marker',
inject(
fakeAsync(inject(
[MarkerManager, GoogleMapsAPIWrapper],
(markerManager: MarkerManager, apiWrapper: GoogleMapsAPIWrapper) => {
const newMarker = new AgmMarker(markerManager);
Expand All @@ -65,12 +78,12 @@ describe('MarkerManager', () => {
markerManager.addMarker(newMarker);
markerManager.deleteMarker(newMarker).then(
() => { expect(markerInstance.setMap).toHaveBeenCalledWith(null); });
}));
})));
});

describe('set marker icon', () => {
it('should update that marker via setIcon method when the markerUrl changes',
async(inject(
fakeAsync(inject(
[MarkerManager, GoogleMapsAPIWrapper],
(markerManager: MarkerManager, apiWrapper: GoogleMapsAPIWrapper) => {
const newMarker = new AgmMarker(markerManager);
Expand All @@ -85,6 +98,7 @@ describe('MarkerManager', () => {
(<jest.Mock>apiWrapper.createMarker).mockReturnValue(Promise.resolve(markerInstance));

markerManager.addMarker(newMarker);
flushMicrotasks();
expect(apiWrapper.createMarker).toHaveBeenCalledWith({
position: {lat: 34.4, lng: 22.3},
label: 'A',
Expand All @@ -106,7 +120,7 @@ describe('MarkerManager', () => {

describe('set marker opacity', () => {
it('should update that marker via setOpacity method when the markerOpacity changes',
async(inject(
fakeAsync(inject(
[MarkerManager, GoogleMapsAPIWrapper],
(markerManager: MarkerManager, apiWrapper: GoogleMapsAPIWrapper) => {
const newMarker = new AgmMarker(markerManager);
Expand All @@ -120,9 +134,8 @@ describe('MarkerManager', () => {
};
(<jest.Mock>apiWrapper.createMarker).mockReturnValue(Promise.resolve(markerInstance));

(<jest.Mock>apiWrapper.createMarker).mockReturnValue(Promise.resolve(markerInstance));

markerManager.addMarker(newMarker);
flushMicrotasks();
expect(apiWrapper.createMarker).toHaveBeenCalledWith({
position: {lat: 34.4, lng: 22.3},
label: 'A',
Expand All @@ -144,7 +157,7 @@ describe('MarkerManager', () => {

describe('set visible option', () => {
it('should update that marker via setVisible method when the visible changes',
async(inject(
fakeAsync(inject(
[MarkerManager, GoogleMapsAPIWrapper],
(markerManager: MarkerManager, apiWrapper: GoogleMapsAPIWrapper) => {
const newMarker = new AgmMarker(markerManager);
Expand All @@ -160,6 +173,7 @@ describe('MarkerManager', () => {
(<jest.Mock>apiWrapper.createMarker).mockReturnValue(Promise.resolve(markerInstance));

markerManager.addMarker(newMarker);
flushMicrotasks();
expect(apiWrapper.createMarker).toHaveBeenCalledWith({
position: {lat: 34.4, lng: 22.3},
label: 'A',
Expand All @@ -180,7 +194,7 @@ describe('MarkerManager', () => {

describe('set zIndex option', () => {
it('should update that marker via setZIndex method when the zIndex changes',
async(inject(
fakeAsync(inject(
[MarkerManager, GoogleMapsAPIWrapper],
(markerManager: MarkerManager, apiWrapper: GoogleMapsAPIWrapper) => {
const newMarker = new AgmMarker(markerManager);
Expand All @@ -196,6 +210,7 @@ describe('MarkerManager', () => {
(<jest.Mock>apiWrapper.createMarker).mockReturnValue(Promise.resolve(markerInstance));

markerManager.addMarker(newMarker);
flushMicrotasks();
expect(apiWrapper.createMarker).toHaveBeenCalledWith({
position: {lat: 34.4, lng: 22.3},
label: 'A',
Expand All @@ -214,4 +229,45 @@ describe('MarkerManager', () => {
() => { expect(markerInstance.setZIndex).toHaveBeenCalledWith(zIndex); });
})));
});

describe('set animation option', () => {
it('should update that marker via setAnimation method when the animation changes',
fakeAsync(inject(
[MarkerManager, GoogleMapsAPIWrapper],
(markerManager: MarkerManager, apiWrapper: GoogleMapsAPIWrapper) => {
const newMarker = new AgmMarker(markerManager);
newMarker.latitude = 34.4;
newMarker.longitude = 22.3;
newMarker.label = 'A';
newMarker.visible = false;
newMarker.animation = null;

const markerInstance: any = {
setMap: jest.fn(),
setAnimation: jest.fn().mockReturnValue(new Promise(resolve => setTimeout(resolve, 500)))
};
(<jest.Mock>apiWrapper.createMarker).mockReturnValue(Promise.resolve(markerInstance));

markerManager.addMarker(newMarker);
flushMicrotasks();
expect(apiWrapper.createMarker).toHaveBeenCalledWith({
position: {lat: 34.4, lng: 22.3},
label: 'A',
draggable: false,
icon: undefined,
visible: false,
opacity: 1,
zIndex: 1,
title: undefined,
clickable: true,
animation: null
});
const animation = 'BOUNCE';
newMarker.animation = animation;
const updatePromise = markerManager.updateAnimation(newMarker);
tick(600);
updatePromise.then(
() => expect(markerInstance.setAnimation).toHaveBeenCalledWith(animMap.BOUNCE));
})));
});
});
44 changes: 24 additions & 20 deletions packages/core/services/managers/marker-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ export class MarkerManager {

constructor(protected _mapsWrapper: GoogleMapsAPIWrapper, protected _zone: NgZone) {}

async convertAnimation(uiAnim: 'BOUNCE' | 'DROP' | null): Promise<any>{
if (uiAnim === null) {
return null;
} else {
return this._mapsWrapper.getNativeMap().then(() => google.maps.Animation[uiAnim]);
}
}

deleteMarker(marker: AgmMarker): Promise<void> {
const m = this._markers.get(marker);
if (m == null) {
Expand Down Expand Up @@ -66,30 +74,26 @@ export class MarkerManager {
return this._markers.get(marker).then((m: Marker) => m.setClickable(marker.clickable));
}

updateAnimation(marker: AgmMarker): Promise<void> {
return this._markers.get(marker).then((m: Marker) => {
if (typeof marker.animation === 'string') {
m.setAnimation(google.maps.Animation[marker.animation]);
} else {
m.setAnimation(marker.animation);
}
});
async updateAnimation(marker: AgmMarker) {
const m = await this._markers.get(marker);
m.setAnimation(await this.convertAnimation(marker.animation));
}

addMarker(marker: AgmMarker) {
const markerPromise = this._mapsWrapper.createMarker({
position: {lat: marker.latitude, lng: marker.longitude},
label: marker.label,
draggable: marker.draggable,
icon: marker.iconUrl,
opacity: marker.opacity,
visible: marker.visible,
zIndex: marker.zIndex,
title: marker.title,
clickable: marker.clickable,
animation: (typeof marker.animation === 'string') ? google.maps.Animation[marker.animation] : marker.animation
});

const markerPromise = new Promise<Marker>(async (resolve) =>
this._mapsWrapper.createMarker({
position: {lat: marker.latitude, lng: marker.longitude},
label: marker.label,
draggable: marker.draggable,
icon: marker.iconUrl,
opacity: marker.opacity,
visible: marker.visible,
zIndex: marker.zIndex,
title: marker.title,
clickable: marker.clickable,
animation: await this.convertAnimation(marker.animation)
}).then(resolve));
this._markers.set(marker, markerPromise);
}

Expand Down