|
| 1 | +import {Component, ViewChild} from '@angular/core'; |
| 2 | +import {TestBed, fakeAsync, flush} from '@angular/core/testing'; |
| 3 | + |
| 4 | +import {DEFAULT_OPTIONS, GoogleMap} from '../google-map/google-map'; |
| 5 | +import { |
| 6 | + createAdvancedMarkerConstructorSpy, |
| 7 | + createAdvancedMarkerSpy, |
| 8 | + createMapConstructorSpy, |
| 9 | + createMapSpy, |
| 10 | +} from '../testing/fake-google-map-utils'; |
| 11 | +import {DEFAULT_MARKER_OPTIONS, MapAdvancedMarker} from './map-advanced-marker'; |
| 12 | + |
| 13 | +describe('MapAdvancedMarker', () => { |
| 14 | + let mapSpy: jasmine.SpyObj<google.maps.Map>; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + mapSpy = createMapSpy(DEFAULT_OPTIONS); |
| 18 | + createMapConstructorSpy(mapSpy); |
| 19 | + }); |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + (window.google as any) = undefined; |
| 23 | + }); |
| 24 | + |
| 25 | + it('initializes a Google Map advanced marker', fakeAsync(() => { |
| 26 | + const advancedMarkerSpy = createAdvancedMarkerSpy(DEFAULT_MARKER_OPTIONS); |
| 27 | + const advancedMarkerConstructorSpy = createAdvancedMarkerConstructorSpy(advancedMarkerSpy); |
| 28 | + |
| 29 | + const fixture = TestBed.createComponent(TestApp); |
| 30 | + fixture.detectChanges(); |
| 31 | + flush(); |
| 32 | + expect(advancedMarkerConstructorSpy).toHaveBeenCalledWith({ |
| 33 | + ...DEFAULT_MARKER_OPTIONS, |
| 34 | + title: undefined, |
| 35 | + content: undefined, |
| 36 | + gmpDraggable: undefined, |
| 37 | + zIndex: undefined, |
| 38 | + map: mapSpy, |
| 39 | + }); |
| 40 | + })); |
| 41 | + |
| 42 | + it('sets advanced marker inputs', fakeAsync(() => { |
| 43 | + const options: google.maps.marker.AdvancedMarkerElementOptions = { |
| 44 | + position: {lat: 3, lng: 5}, |
| 45 | + title: 'marker title', |
| 46 | + map: mapSpy, |
| 47 | + content: undefined, |
| 48 | + gmpDraggable: true, |
| 49 | + zIndex: 1, |
| 50 | + }; |
| 51 | + const advancedMarkerSpy = createAdvancedMarkerSpy(options); |
| 52 | + const advancedMarkerConstructorSpy = createAdvancedMarkerConstructorSpy(advancedMarkerSpy); |
| 53 | + |
| 54 | + const fixture = TestBed.createComponent(TestApp); |
| 55 | + fixture.componentInstance.position = options.position; |
| 56 | + fixture.componentInstance.title = options.title; |
| 57 | + fixture.componentInstance.content = options.content; |
| 58 | + fixture.componentInstance.gmpDraggable = options.gmpDraggable; |
| 59 | + fixture.componentInstance.zIndex = options.zIndex; |
| 60 | + |
| 61 | + fixture.detectChanges(); |
| 62 | + flush(); |
| 63 | + |
| 64 | + expect(advancedMarkerConstructorSpy).toHaveBeenCalledWith(options); |
| 65 | + })); |
| 66 | + |
| 67 | + it('sets marker options, ignoring map', fakeAsync(() => { |
| 68 | + const options: google.maps.marker.AdvancedMarkerElementOptions = { |
| 69 | + position: {lat: 3, lng: 5}, |
| 70 | + title: 'marker title', |
| 71 | + content: undefined, |
| 72 | + gmpDraggable: true, |
| 73 | + zIndex: 1, |
| 74 | + }; |
| 75 | + const advancedMarkerSpy = createAdvancedMarkerSpy(options); |
| 76 | + const advancedMarkerConstructorSpy = createAdvancedMarkerConstructorSpy(advancedMarkerSpy); |
| 77 | + |
| 78 | + const fixture = TestBed.createComponent(TestApp); |
| 79 | + fixture.componentInstance.options = options; |
| 80 | + fixture.detectChanges(); |
| 81 | + flush(); |
| 82 | + |
| 83 | + expect(advancedMarkerConstructorSpy).toHaveBeenCalledWith({...options, map: mapSpy}); |
| 84 | + })); |
| 85 | + |
| 86 | + it('gives precedence to specific inputs over options', fakeAsync(() => { |
| 87 | + const options: google.maps.marker.AdvancedMarkerElementOptions = { |
| 88 | + position: {lat: 3, lng: 5}, |
| 89 | + title: 'marker title', |
| 90 | + content: undefined, |
| 91 | + gmpDraggable: true, |
| 92 | + zIndex: 1, |
| 93 | + }; |
| 94 | + |
| 95 | + const expectedOptions: google.maps.marker.AdvancedMarkerElementOptions = { |
| 96 | + position: {lat: 4, lng: 6}, |
| 97 | + title: 'marker title 2', |
| 98 | + content: undefined, |
| 99 | + gmpDraggable: false, |
| 100 | + zIndex: 999, |
| 101 | + map: mapSpy, |
| 102 | + }; |
| 103 | + const advancedMarkerSpy = createAdvancedMarkerSpy(options); |
| 104 | + const advancedMarkerConstructorSpy = createAdvancedMarkerConstructorSpy(advancedMarkerSpy); |
| 105 | + |
| 106 | + const fixture = TestBed.createComponent(TestApp); |
| 107 | + fixture.componentInstance.position = expectedOptions.position; |
| 108 | + fixture.componentInstance.title = expectedOptions.title; |
| 109 | + fixture.componentInstance.content = expectedOptions.content; |
| 110 | + fixture.componentInstance.gmpDraggable = expectedOptions.gmpDraggable; |
| 111 | + fixture.componentInstance.zIndex = expectedOptions.zIndex; |
| 112 | + fixture.componentInstance.options = options; |
| 113 | + |
| 114 | + fixture.detectChanges(); |
| 115 | + flush(); |
| 116 | + |
| 117 | + expect(advancedMarkerConstructorSpy).toHaveBeenCalledWith(expectedOptions); |
| 118 | + })); |
| 119 | + |
| 120 | + it('initializes marker event handlers', fakeAsync(() => { |
| 121 | + const advancedMarkerSpy = createAdvancedMarkerSpy(DEFAULT_MARKER_OPTIONS); |
| 122 | + createAdvancedMarkerConstructorSpy(advancedMarkerSpy); |
| 123 | + |
| 124 | + const addSpy = advancedMarkerSpy.addListener; |
| 125 | + const fixture = TestBed.createComponent(TestApp); |
| 126 | + fixture.detectChanges(); |
| 127 | + flush(); |
| 128 | + |
| 129 | + expect(addSpy).toHaveBeenCalledWith('click', jasmine.any(Function)); |
| 130 | + expect(addSpy).not.toHaveBeenCalledWith('drag', jasmine.any(Function)); |
| 131 | + expect(addSpy).not.toHaveBeenCalledWith('dragend', jasmine.any(Function)); |
| 132 | + expect(addSpy).not.toHaveBeenCalledWith('dragstart', jasmine.any(Function)); |
| 133 | + })); |
| 134 | + |
| 135 | + it('should be able to add an event listener after init', fakeAsync(() => { |
| 136 | + const advancedMarkerSpy = createAdvancedMarkerSpy(DEFAULT_MARKER_OPTIONS); |
| 137 | + createAdvancedMarkerConstructorSpy(advancedMarkerSpy); |
| 138 | + |
| 139 | + const addSpy = advancedMarkerSpy.addListener; |
| 140 | + const fixture = TestBed.createComponent(TestApp); |
| 141 | + fixture.detectChanges(); |
| 142 | + flush(); |
| 143 | + |
| 144 | + expect(addSpy).not.toHaveBeenCalledWith('drag', jasmine.any(Function)); |
| 145 | + |
| 146 | + // Pick an event that isn't bound in the template. |
| 147 | + const subscription = fixture.componentInstance.advancedMarker.mapDrag.subscribe(); |
| 148 | + fixture.detectChanges(); |
| 149 | + |
| 150 | + expect(addSpy).toHaveBeenCalledWith('drag', jasmine.any(Function)); |
| 151 | + subscription.unsubscribe(); |
| 152 | + })); |
| 153 | +}); |
| 154 | + |
| 155 | +@Component({ |
| 156 | + selector: 'test-app', |
| 157 | + template: ` |
| 158 | + <google-map> |
| 159 | + <map-advanced-marker |
| 160 | + [title]="title" |
| 161 | + [position]="position" |
| 162 | + [content]="content" |
| 163 | + [gmpDraggable]="gmpDraggable" |
| 164 | + [zIndex]="zIndex" |
| 165 | + (mapClick)="handleClick()" |
| 166 | + [options]="options" /> |
| 167 | + </google-map> |
| 168 | + `, |
| 169 | + standalone: true, |
| 170 | + imports: [GoogleMap, MapAdvancedMarker], |
| 171 | +}) |
| 172 | +class TestApp { |
| 173 | + @ViewChild(MapAdvancedMarker) advancedMarker: MapAdvancedMarker; |
| 174 | + title?: string | null; |
| 175 | + position?: google.maps.LatLng | google.maps.LatLngLiteral | null; |
| 176 | + content?: Node | google.maps.marker.PinElement | null; |
| 177 | + gmpDraggable?: boolean | null; |
| 178 | + zIndex?: number | null; |
| 179 | + options: google.maps.marker.AdvancedMarkerElementOptions; |
| 180 | + |
| 181 | + handleClick() {} |
| 182 | +} |
0 commit comments