|
| 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 | + createGroundOverlayConstructorSpy, |
| 9 | + createGroundOverlaySpy, |
| 10 | + createMapConstructorSpy, |
| 11 | + createMapSpy, |
| 12 | + TestingWindow, |
| 13 | +} from '../testing/fake-google-map-utils'; |
| 14 | + |
| 15 | +import {MapGroundOverlay} from './map-ground-overlay'; |
| 16 | + |
| 17 | +describe('MapGroundOverlay', () => { |
| 18 | + let mapSpy: jasmine.SpyObj<google.maps.Map>; |
| 19 | + const url = 'www.testimg.com/img.jpg'; |
| 20 | + const bounds: google.maps.LatLngBoundsLiteral = {east: 3, north: 5, west: -3, south: -5}; |
| 21 | + const clickable = true; |
| 22 | + const opacity = 0.5; |
| 23 | + const groundOverlayOptions: google.maps.GroundOverlayOptions = {clickable, opacity}; |
| 24 | + |
| 25 | + beforeEach(async(() => { |
| 26 | + TestBed.configureTestingModule({ |
| 27 | + imports: [GoogleMapsModule], |
| 28 | + declarations: [TestApp], |
| 29 | + }); |
| 30 | + })); |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + TestBed.compileComponents(); |
| 34 | + |
| 35 | + mapSpy = createMapSpy(DEFAULT_OPTIONS); |
| 36 | + createMapConstructorSpy(mapSpy).and.callThrough(); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + const testingWindow: TestingWindow = window; |
| 41 | + delete testingWindow.google; |
| 42 | + }); |
| 43 | + |
| 44 | + it('initializes a Google Map Ground Overlay', () => { |
| 45 | + const groundOverlaySpy = createGroundOverlaySpy(url, bounds, groundOverlayOptions); |
| 46 | + const groundOverlayConstructorSpy = |
| 47 | + createGroundOverlayConstructorSpy(groundOverlaySpy).and.callThrough(); |
| 48 | + |
| 49 | + const fixture = TestBed.createComponent(TestApp); |
| 50 | + fixture.componentInstance.url = url; |
| 51 | + fixture.componentInstance.bounds = bounds; |
| 52 | + fixture.componentInstance.clickable = clickable; |
| 53 | + fixture.componentInstance.opacity = opacity; |
| 54 | + fixture.detectChanges(); |
| 55 | + |
| 56 | + expect(groundOverlayConstructorSpy).toHaveBeenCalledWith(url, bounds, groundOverlayOptions); |
| 57 | + expect(groundOverlaySpy.setMap).toHaveBeenCalledWith(mapSpy); |
| 58 | + }); |
| 59 | + |
| 60 | + it('has an error if required url or bounds are not provided', () => { |
| 61 | + expect(() => { |
| 62 | + const fixture = TestBed.createComponent(TestApp); |
| 63 | + fixture.detectChanges(); |
| 64 | + }).toThrow(new Error('An image url is required')); |
| 65 | + }); |
| 66 | + |
| 67 | + it('exposes methods that provide information about the Ground Overlay', () => { |
| 68 | + const groundOverlaySpy = createGroundOverlaySpy(url, bounds, groundOverlayOptions); |
| 69 | + createGroundOverlayConstructorSpy(groundOverlaySpy).and.callThrough(); |
| 70 | + |
| 71 | + const fixture = TestBed.createComponent(TestApp); |
| 72 | + const groundOverlayComponent = fixture.debugElement.query(By.directive( |
| 73 | + MapGroundOverlay))!.injector.get<MapGroundOverlay>(MapGroundOverlay); |
| 74 | + fixture.componentInstance.url = url; |
| 75 | + fixture.componentInstance.bounds = bounds; |
| 76 | + fixture.componentInstance.opacity = opacity; |
| 77 | + fixture.detectChanges(); |
| 78 | + |
| 79 | + groundOverlayComponent.getBounds(); |
| 80 | + expect(groundOverlaySpy.getBounds).toHaveBeenCalled(); |
| 81 | + |
| 82 | + groundOverlaySpy.getOpacity.and.returnValue(opacity); |
| 83 | + expect(groundOverlayComponent.getOpacity()).toBe(opacity); |
| 84 | + |
| 85 | + groundOverlaySpy.getUrl.and.returnValue(url); |
| 86 | + expect(groundOverlayComponent.getUrl()).toBe(url); |
| 87 | + }); |
| 88 | + |
| 89 | + it('initializes Ground Overlay event handlers', () => { |
| 90 | + const groundOverlaySpy = createGroundOverlaySpy(url, bounds, groundOverlayOptions); |
| 91 | + createGroundOverlayConstructorSpy(groundOverlaySpy).and.callThrough(); |
| 92 | + |
| 93 | + const addSpy = groundOverlaySpy.addListener; |
| 94 | + const fixture = TestBed.createComponent(TestApp); |
| 95 | + fixture.componentInstance.url = url; |
| 96 | + fixture.componentInstance.bounds = bounds; |
| 97 | + fixture.detectChanges(); |
| 98 | + |
| 99 | + expect(addSpy).toHaveBeenCalledWith('click', jasmine.any(Function)); |
| 100 | + expect(addSpy).not.toHaveBeenCalledWith('dblclick', jasmine.any(Function)); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should be able to add an event listener after init', () => { |
| 104 | + const groundOverlaySpy = createGroundOverlaySpy(url, bounds, groundOverlayOptions); |
| 105 | + createGroundOverlayConstructorSpy(groundOverlaySpy).and.callThrough(); |
| 106 | + |
| 107 | + const addSpy = groundOverlaySpy.addListener; |
| 108 | + const fixture = TestBed.createComponent(TestApp); |
| 109 | + fixture.componentInstance.url = url; |
| 110 | + fixture.componentInstance.bounds = bounds; |
| 111 | + fixture.detectChanges(); |
| 112 | + |
| 113 | + expect(addSpy).not.toHaveBeenCalledWith('dblclick', jasmine.any(Function)); |
| 114 | + |
| 115 | + // Pick an event that isn't bound in the template. |
| 116 | + const subscription = fixture.componentInstance.groundOverlay.mapDblclick.subscribe(); |
| 117 | + fixture.detectChanges(); |
| 118 | + |
| 119 | + expect(addSpy).toHaveBeenCalledWith('dblclick', jasmine.any(Function)); |
| 120 | + subscription.unsubscribe(); |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
| 124 | +@Component({ |
| 125 | + selector: 'test-app', |
| 126 | + template: `<google-map> |
| 127 | + <map-ground-overlay [url]="url" |
| 128 | + [bounds]="bounds" |
| 129 | + [clickable]="clickable" |
| 130 | + [opacity]="opacity" |
| 131 | + (mapClick)="handleClick()"> |
| 132 | + </map-ground-overlay> |
| 133 | + </google-map>`, |
| 134 | +}) |
| 135 | +class TestApp { |
| 136 | + @ViewChild(MapGroundOverlay) groundOverlay: MapGroundOverlay; |
| 137 | + url!: string; |
| 138 | + bounds!: google.maps.LatLngBoundsLiteral; |
| 139 | + clickable = false; |
| 140 | + opacity = 1; |
| 141 | + |
| 142 | + handleClick() {} |
| 143 | +} |
0 commit comments