|
| 1 | +import {Component, ViewChild} from '@angular/core'; |
| 2 | +import {async, TestBed} from '@angular/core/testing'; |
| 3 | +import {By} from '@angular/platform-browser'; |
| 4 | + |
| 5 | +import {DEFAULT_OPTIONS} from '../google-map/google-map'; |
| 6 | +import {GoogleMapsModule} from '../google-maps-module'; |
| 7 | +import { |
| 8 | + createKmlLayerConstructorSpy, |
| 9 | + createKmlLayerSpy, |
| 10 | + createMapConstructorSpy, |
| 11 | + createMapSpy, |
| 12 | +} from '../testing/fake-google-map-utils'; |
| 13 | + |
| 14 | +import {MapKmlLayer} from './map-kml-layer'; |
| 15 | + |
| 16 | +const DEMO_URL = 'www.test.kml'; |
| 17 | +const DEFAULT_KML_OPTIONS: google.maps.KmlLayerOptions = { |
| 18 | + clickable: true, |
| 19 | + preserveViewport: true, |
| 20 | + url: DEMO_URL, |
| 21 | +}; |
| 22 | + |
| 23 | +describe('MapKmlLayer', () => { |
| 24 | + let mapSpy: jasmine.SpyObj<google.maps.Map>; |
| 25 | + |
| 26 | + beforeEach(async(() => { |
| 27 | + TestBed.configureTestingModule({ |
| 28 | + imports: [GoogleMapsModule], |
| 29 | + declarations: [TestApp], |
| 30 | + }); |
| 31 | + })); |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + TestBed.compileComponents(); |
| 35 | + |
| 36 | + mapSpy = createMapSpy(DEFAULT_OPTIONS); |
| 37 | + createMapConstructorSpy(mapSpy).and.callThrough(); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + delete window.google; |
| 42 | + }); |
| 43 | + |
| 44 | + it('initializes a Google Map Kml Layer', () => { |
| 45 | + const kmlLayerSpy = createKmlLayerSpy({}); |
| 46 | + const kmlLayerConstructorSpy = createKmlLayerConstructorSpy(kmlLayerSpy).and.callThrough(); |
| 47 | + |
| 48 | + const fixture = TestBed.createComponent(TestApp); |
| 49 | + fixture.detectChanges(); |
| 50 | + |
| 51 | + expect(kmlLayerConstructorSpy).toHaveBeenCalledWith({url: undefined}); |
| 52 | + expect(kmlLayerSpy.setMap).toHaveBeenCalledWith(mapSpy); |
| 53 | + }); |
| 54 | + |
| 55 | + it('sets url from input', () => { |
| 56 | + const options: google.maps.KmlLayerOptions = {url: DEMO_URL}; |
| 57 | + const kmlLayerSpy = createKmlLayerSpy(options); |
| 58 | + const kmlLayerConstructorSpy = createKmlLayerConstructorSpy(kmlLayerSpy).and.callThrough(); |
| 59 | + |
| 60 | + const fixture = TestBed.createComponent(TestApp); |
| 61 | + fixture.componentInstance.url = DEMO_URL; |
| 62 | + fixture.detectChanges(); |
| 63 | + |
| 64 | + expect(kmlLayerConstructorSpy).toHaveBeenCalledWith(options); |
| 65 | + }); |
| 66 | + |
| 67 | + it('gives precedence to url input over options', () => { |
| 68 | + const expectedUrl = 'www.realurl.kml'; |
| 69 | + const expectedOptions: google.maps.KmlLayerOptions = {...DEFAULT_KML_OPTIONS, url: expectedUrl}; |
| 70 | + const kmlLayerSpy = createKmlLayerSpy(expectedOptions); |
| 71 | + const kmlLayerConstructorSpy = createKmlLayerConstructorSpy(kmlLayerSpy).and.callThrough(); |
| 72 | + |
| 73 | + const fixture = TestBed.createComponent(TestApp); |
| 74 | + fixture.componentInstance.options = DEFAULT_KML_OPTIONS; |
| 75 | + fixture.componentInstance.url = expectedUrl; |
| 76 | + fixture.detectChanges(); |
| 77 | + |
| 78 | + expect(kmlLayerConstructorSpy).toHaveBeenCalledWith(expectedOptions); |
| 79 | + }); |
| 80 | + |
| 81 | + it('exposes methods that provide information about the KmlLayer', () => { |
| 82 | + const kmlLayerSpy = createKmlLayerSpy(DEFAULT_KML_OPTIONS); |
| 83 | + createKmlLayerConstructorSpy(kmlLayerSpy).and.callThrough(); |
| 84 | + |
| 85 | + const fixture = TestBed.createComponent(TestApp); |
| 86 | + const kmlLayerComponent = fixture.debugElement.query(By.directive( |
| 87 | + MapKmlLayer))!.injector.get<MapKmlLayer>(MapKmlLayer); |
| 88 | + fixture.detectChanges(); |
| 89 | + |
| 90 | + kmlLayerComponent.getDefaultViewport(); |
| 91 | + expect(kmlLayerSpy.getDefaultViewport).toHaveBeenCalled(); |
| 92 | + |
| 93 | + const metadata: google.maps.KmlLayerMetadata = { |
| 94 | + author: { |
| 95 | + email: 'test@test.com', |
| 96 | + name: 'author', |
| 97 | + uri: 'www.author.com', |
| 98 | + }, |
| 99 | + description: 'test', |
| 100 | + hasScreenOverlays: true, |
| 101 | + name: 'metadata', |
| 102 | + snippet: '...', |
| 103 | + }; |
| 104 | + kmlLayerSpy.getMetadata.and.returnValue(metadata); |
| 105 | + expect(kmlLayerComponent.getMetadata()).toBe(metadata); |
| 106 | + |
| 107 | + kmlLayerComponent.getStatus(); |
| 108 | + expect(kmlLayerSpy.getStatus).toHaveBeenCalled(); |
| 109 | + |
| 110 | + kmlLayerSpy.getUrl.and.returnValue(DEMO_URL); |
| 111 | + expect(kmlLayerComponent.getUrl()).toBe(DEMO_URL); |
| 112 | + |
| 113 | + kmlLayerSpy.getZIndex.and.returnValue(3); |
| 114 | + expect(kmlLayerComponent.getZIndex()).toBe(3); |
| 115 | + }); |
| 116 | + |
| 117 | + it('initializes KmlLayer event handlers', () => { |
| 118 | + const kmlLayerSpy = createKmlLayerSpy(DEFAULT_KML_OPTIONS); |
| 119 | + createKmlLayerConstructorSpy(kmlLayerSpy).and.callThrough(); |
| 120 | + |
| 121 | + const addSpy = kmlLayerSpy.addListener; |
| 122 | + const fixture = TestBed.createComponent(TestApp); |
| 123 | + fixture.detectChanges(); |
| 124 | + |
| 125 | + expect(addSpy).toHaveBeenCalledWith('click', jasmine.any(Function)); |
| 126 | + expect(addSpy).not.toHaveBeenCalledWith('defaultviewport_changed', jasmine.any(Function)); |
| 127 | + expect(addSpy).toHaveBeenCalledWith('status_changed', jasmine.any(Function)); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should be able to add an event listener after init', () => { |
| 131 | + const kmlLayerSpy = createKmlLayerSpy(DEFAULT_KML_OPTIONS); |
| 132 | + createKmlLayerConstructorSpy(kmlLayerSpy).and.callThrough(); |
| 133 | + |
| 134 | + const addSpy = kmlLayerSpy.addListener; |
| 135 | + const fixture = TestBed.createComponent(TestApp); |
| 136 | + fixture.detectChanges(); |
| 137 | + |
| 138 | + expect(addSpy).not.toHaveBeenCalledWith('defaultviewport_changed', jasmine.any(Function)); |
| 139 | + |
| 140 | + // Pick an event that isn't bound in the template. |
| 141 | + const subscription = fixture.componentInstance.kmlLayer.defaultviewportChanged.subscribe(); |
| 142 | + fixture.detectChanges(); |
| 143 | + |
| 144 | + expect(addSpy).toHaveBeenCalledWith('defaultviewport_changed', jasmine.any(Function)); |
| 145 | + subscription.unsubscribe(); |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +@Component({ |
| 150 | + selector: 'test-app', |
| 151 | + template: `<google-map> |
| 152 | + <map-kml-layer [options]="options" |
| 153 | + [url]="url" |
| 154 | + (kmlClick)="handleClick()" |
| 155 | + (statusChanged)="handleStatusChange()"> |
| 156 | + </map-kml-layer> |
| 157 | + </google-map>`, |
| 158 | +}) |
| 159 | +class TestApp { |
| 160 | + @ViewChild(MapKmlLayer) kmlLayer: MapKmlLayer; |
| 161 | + options?: google.maps.KmlLayerOptions; |
| 162 | + url?: string; |
| 163 | + |
| 164 | + handleClick() {} |
| 165 | + |
| 166 | + handleStatusChange() {} |
| 167 | +} |
0 commit comments