|
| 1 | +import {Component} from '@angular/core'; |
| 2 | +import {async, TestBed} from '@angular/core/testing'; |
| 3 | +import {By} from '@angular/platform-browser'; |
| 4 | + |
| 5 | +import {DEFAULT_OPTIONS, GoogleMapModule, UpdatedGoogleMap} from '../google-map/index'; |
| 6 | +import {MapMarker} from '../map-marker/index'; |
| 7 | +import { |
| 8 | + createInfoWindowConstructorSpy, |
| 9 | + createInfoWindowSpy, |
| 10 | + createMapConstructorSpy, |
| 11 | + createMapSpy, |
| 12 | + TestingWindow |
| 13 | +} from '../testing/fake-google-map-utils'; |
| 14 | + |
| 15 | +import {MapInfoWindow, MapInfoWindowModule} from './index'; |
| 16 | + |
| 17 | +describe('MapInfoWindow', () => { |
| 18 | + let mapSpy: jasmine.SpyObj<UpdatedGoogleMap>; |
| 19 | + |
| 20 | + beforeEach(async(() => { |
| 21 | + TestBed.configureTestingModule({ |
| 22 | + imports: [ |
| 23 | + GoogleMapModule, |
| 24 | + MapInfoWindowModule, |
| 25 | + ], |
| 26 | + declarations: [TestApp], |
| 27 | + }); |
| 28 | + })); |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + TestBed.compileComponents(); |
| 32 | + |
| 33 | + mapSpy = createMapSpy(DEFAULT_OPTIONS); |
| 34 | + createMapConstructorSpy(mapSpy).and.callThrough(); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + const testingWindow: TestingWindow = window; |
| 39 | + delete testingWindow.google; |
| 40 | + }); |
| 41 | + |
| 42 | + it('initializes a Google Map Info Window', () => { |
| 43 | + const infoWindowSpy = createInfoWindowSpy({}); |
| 44 | + const infoWindowConstructorSpy = |
| 45 | + createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough(); |
| 46 | + |
| 47 | + const fixture = TestBed.createComponent(TestApp); |
| 48 | + fixture.detectChanges(); |
| 49 | + |
| 50 | + expect(infoWindowConstructorSpy).toHaveBeenCalledWith({ |
| 51 | + position: undefined, |
| 52 | + content: jasmine.any(Node), |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + it('sets position', () => { |
| 57 | + const position: google.maps.LatLngLiteral = {lat: 5, lng: 7}; |
| 58 | + const infoWindowSpy = createInfoWindowSpy({position}); |
| 59 | + const infoWindowConstructorSpy = |
| 60 | + createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough(); |
| 61 | + |
| 62 | + const fixture = TestBed.createComponent(TestApp); |
| 63 | + fixture.componentInstance.position = position; |
| 64 | + fixture.detectChanges(); |
| 65 | + |
| 66 | + expect(infoWindowConstructorSpy).toHaveBeenCalledWith({ |
| 67 | + position, |
| 68 | + content: jasmine.any(Node), |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('sets options', () => { |
| 73 | + const options: google.maps.InfoWindowOptions = { |
| 74 | + position: {lat: 3, lng: 5}, |
| 75 | + maxWidth: 50, |
| 76 | + disableAutoPan: true, |
| 77 | + }; |
| 78 | + const infoWindowSpy = createInfoWindowSpy(options); |
| 79 | + const infoWindowConstructorSpy = |
| 80 | + createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough(); |
| 81 | + |
| 82 | + const fixture = TestBed.createComponent(TestApp); |
| 83 | + fixture.componentInstance.options = options; |
| 84 | + fixture.detectChanges(); |
| 85 | + |
| 86 | + expect(infoWindowConstructorSpy).toHaveBeenCalledWith({ |
| 87 | + ...options, |
| 88 | + content: jasmine.any(Node), |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('gives preference to position over options', () => { |
| 93 | + const position: google.maps.LatLngLiteral = {lat: 5, lng: 7}; |
| 94 | + const options: google.maps.InfoWindowOptions = { |
| 95 | + position: {lat: 3, lng: 5}, |
| 96 | + maxWidth: 50, |
| 97 | + disableAutoPan: true, |
| 98 | + }; |
| 99 | + const infoWindowSpy = createInfoWindowSpy({...options, position}); |
| 100 | + const infoWindowConstructorSpy = |
| 101 | + createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough(); |
| 102 | + |
| 103 | + const fixture = TestBed.createComponent(TestApp); |
| 104 | + fixture.componentInstance.options = options; |
| 105 | + fixture.componentInstance.position = position; |
| 106 | + fixture.detectChanges(); |
| 107 | + |
| 108 | + expect(infoWindowConstructorSpy).toHaveBeenCalledWith({ |
| 109 | + ...options, |
| 110 | + position, |
| 111 | + content: jasmine.any(Node), |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('exposes methods that change the configuration of the info window', () => { |
| 116 | + const fakeMarker = {} as unknown as google.maps.Marker; |
| 117 | + const fakeMarkerComponent = {_marker: fakeMarker} as unknown as MapMarker; |
| 118 | + const infoWindowSpy = createInfoWindowSpy({}); |
| 119 | + createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough(); |
| 120 | + |
| 121 | + const fixture = TestBed.createComponent(TestApp); |
| 122 | + const infoWindowComponent = fixture.debugElement.query(By.directive( |
| 123 | + MapInfoWindow))!.injector.get<MapInfoWindow>(MapInfoWindow); |
| 124 | + fixture.detectChanges(); |
| 125 | + |
| 126 | + infoWindowComponent.close(); |
| 127 | + expect(infoWindowSpy.close).toHaveBeenCalled(); |
| 128 | + |
| 129 | + infoWindowComponent.open(fakeMarkerComponent); |
| 130 | + expect(infoWindowSpy.open).toHaveBeenCalledWith(mapSpy, fakeMarker); |
| 131 | + }); |
| 132 | + |
| 133 | + it('exposes methods that provide information about the info window', () => { |
| 134 | + const infoWindowSpy = createInfoWindowSpy({}); |
| 135 | + createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough(); |
| 136 | + |
| 137 | + const fixture = TestBed.createComponent(TestApp); |
| 138 | + const infoWindowComponent = fixture.debugElement.query(By.directive( |
| 139 | + MapInfoWindow))!.injector.get<MapInfoWindow>(MapInfoWindow); |
| 140 | + fixture.detectChanges(); |
| 141 | + |
| 142 | + infoWindowSpy.getContent.and.returnValue('test content'); |
| 143 | + expect(infoWindowComponent.getContent()).toBe('test content'); |
| 144 | + |
| 145 | + infoWindowComponent.getPosition(); |
| 146 | + expect(infoWindowSpy.getPosition).toHaveBeenCalled(); |
| 147 | + |
| 148 | + infoWindowSpy.getZIndex.and.returnValue(5); |
| 149 | + expect(infoWindowComponent.getZIndex()).toBe(5); |
| 150 | + }); |
| 151 | + |
| 152 | + it('initializes info window event handlers', () => { |
| 153 | + const infoWindowSpy = createInfoWindowSpy({}); |
| 154 | + createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough(); |
| 155 | + |
| 156 | + const fixture = TestBed.createComponent(TestApp); |
| 157 | + fixture.detectChanges(); |
| 158 | + |
| 159 | + expect(infoWindowSpy.addListener).toHaveBeenCalledWith('closeclick', jasmine.any(Function)); |
| 160 | + expect(infoWindowSpy.addListener) |
| 161 | + .not.toHaveBeenCalledWith('content_changed', jasmine.any(Function)); |
| 162 | + expect(infoWindowSpy.addListener).not.toHaveBeenCalledWith('domready', jasmine.any(Function)); |
| 163 | + expect(infoWindowSpy.addListener) |
| 164 | + .not.toHaveBeenCalledWith('position_changed', jasmine.any(Function)); |
| 165 | + expect(infoWindowSpy.addListener) |
| 166 | + .not.toHaveBeenCalledWith('zindex_changed', jasmine.any(Function)); |
| 167 | + }); |
| 168 | +}); |
| 169 | + |
| 170 | +@Component({ |
| 171 | + selector: 'test-app', |
| 172 | + template: `<google-map> |
| 173 | + <map-info-window [position]="position" |
| 174 | + [options]="options" |
| 175 | + (closeclick)="handleClose()"> |
| 176 | + test content |
| 177 | + </map-info-window> |
| 178 | + </google-map>`, |
| 179 | +}) |
| 180 | +class TestApp { |
| 181 | + position?: google.maps.LatLngLiteral; |
| 182 | + options?: google.maps.InfoWindowOptions; |
| 183 | + |
| 184 | + handleClose() {} |
| 185 | +} |
0 commit comments