|
| 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, UpdatedGoogleMap} from '../google-map/google-map'; |
| 6 | +import {GoogleMapsModule} from '../google-maps-module'; |
| 7 | +import { |
| 8 | + createMapConstructorSpy, |
| 9 | + createMapSpy, |
| 10 | + createPolygonConstructorSpy, |
| 11 | + createPolygonSpy, |
| 12 | + TestingWindow, |
| 13 | +} from '../testing/fake-google-map-utils'; |
| 14 | + |
| 15 | +import {MapPolygon} from './map-polygon'; |
| 16 | + |
| 17 | +describe('MapPolygon', () => { |
| 18 | + let mapSpy: jasmine.SpyObj<UpdatedGoogleMap>; |
| 19 | + let polygonPath: google.maps.LatLngLiteral[]; |
| 20 | + let polygonOptions: google.maps.PolygonOptions; |
| 21 | + |
| 22 | + beforeEach(async(() => { |
| 23 | + polygonPath = [{lat: 25, lng: 26}, {lat: 26, lng: 27}, {lat: 30, lng: 34}]; |
| 24 | + polygonOptions = {paths: polygonPath, strokeColor: 'grey', strokeOpacity: 0.8}; |
| 25 | + TestBed.configureTestingModule({ |
| 26 | + imports: [GoogleMapsModule], |
| 27 | + declarations: [TestApp], |
| 28 | + }); |
| 29 | + })); |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + TestBed.compileComponents(); |
| 33 | + |
| 34 | + mapSpy = createMapSpy(DEFAULT_OPTIONS); |
| 35 | + createMapConstructorSpy(mapSpy).and.callThrough(); |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(() => { |
| 39 | + const testingWindow: TestingWindow = window; |
| 40 | + delete testingWindow.google; |
| 41 | + }); |
| 42 | + |
| 43 | + it('initializes a Google Map Polygon', () => { |
| 44 | + const polygonSpy = createPolygonSpy({}); |
| 45 | + const polygonConstructorSpy = createPolygonConstructorSpy(polygonSpy).and.callThrough(); |
| 46 | + |
| 47 | + const fixture = TestBed.createComponent(TestApp); |
| 48 | + fixture.detectChanges(); |
| 49 | + |
| 50 | + expect(polygonConstructorSpy).toHaveBeenCalledWith({paths: undefined}); |
| 51 | + expect(polygonSpy.setMap).toHaveBeenCalledWith(mapSpy); |
| 52 | + }); |
| 53 | + |
| 54 | + it('sets path from input', () => { |
| 55 | + const paths: google.maps.LatLngLiteral[] = [{lat: 3, lng: 5}]; |
| 56 | + const options: google.maps.PolygonOptions = {paths}; |
| 57 | + const polygonSpy = createPolygonSpy(options); |
| 58 | + const polygonConstructorSpy = createPolygonConstructorSpy(polygonSpy).and.callThrough(); |
| 59 | + |
| 60 | + const fixture = TestBed.createComponent(TestApp); |
| 61 | + fixture.componentInstance.paths = paths; |
| 62 | + fixture.detectChanges(); |
| 63 | + |
| 64 | + expect(polygonConstructorSpy).toHaveBeenCalledWith(options); |
| 65 | + }); |
| 66 | + |
| 67 | + it('gives precedence to path input over options', () => { |
| 68 | + const paths: google.maps.LatLngLiteral[] = [{lat: 3, lng: 5}]; |
| 69 | + const expectedOptions: google.maps.PolygonOptions = {...polygonOptions, paths}; |
| 70 | + const polygonSpy = createPolygonSpy(expectedOptions); |
| 71 | + const polygonConstructorSpy = createPolygonConstructorSpy(polygonSpy).and.callThrough(); |
| 72 | + |
| 73 | + const fixture = TestBed.createComponent(TestApp); |
| 74 | + fixture.componentInstance.options = polygonOptions; |
| 75 | + fixture.componentInstance.paths = paths; |
| 76 | + fixture.detectChanges(); |
| 77 | + |
| 78 | + expect(polygonConstructorSpy).toHaveBeenCalledWith(expectedOptions); |
| 79 | + }); |
| 80 | + |
| 81 | + it('exposes methods that provide information about the Polygon', () => { |
| 82 | + const polygonSpy = createPolygonSpy(polygonOptions); |
| 83 | + createPolygonConstructorSpy(polygonSpy).and.callThrough(); |
| 84 | + |
| 85 | + const fixture = TestBed.createComponent(TestApp); |
| 86 | + const polygonComponent = |
| 87 | + fixture.debugElement.query(By.directive(MapPolygon))!.injector.get<MapPolygon>(MapPolygon); |
| 88 | + fixture.detectChanges(); |
| 89 | + |
| 90 | + polygonSpy.getDraggable.and.returnValue(true); |
| 91 | + expect(polygonComponent.getDraggable()).toBe(true); |
| 92 | + |
| 93 | + polygonSpy.getEditable.and.returnValue(true); |
| 94 | + expect(polygonComponent.getEditable()).toBe(true); |
| 95 | + |
| 96 | + polygonComponent.getPath(); |
| 97 | + expect(polygonSpy.getPath).toHaveBeenCalled(); |
| 98 | + |
| 99 | + polygonComponent.getPaths(); |
| 100 | + expect(polygonSpy.getPaths).toHaveBeenCalled(); |
| 101 | + |
| 102 | + polygonSpy.getVisible.and.returnValue(true); |
| 103 | + expect(polygonComponent.getVisible()).toBe(true); |
| 104 | + }); |
| 105 | + |
| 106 | + it('initializes Polygon event handlers', () => { |
| 107 | + const polygonSpy = createPolygonSpy(polygonOptions); |
| 108 | + createPolygonConstructorSpy(polygonSpy).and.callThrough(); |
| 109 | + |
| 110 | + const addSpy = polygonSpy.addListener; |
| 111 | + const fixture = TestBed.createComponent(TestApp); |
| 112 | + fixture.detectChanges(); |
| 113 | + |
| 114 | + expect(addSpy).toHaveBeenCalledWith('click', jasmine.any(Function)); |
| 115 | + expect(addSpy).not.toHaveBeenCalledWith('dblclick', jasmine.any(Function)); |
| 116 | + expect(addSpy).not.toHaveBeenCalledWith('drag', jasmine.any(Function)); |
| 117 | + expect(addSpy).not.toHaveBeenCalledWith('dragend', jasmine.any(Function)); |
| 118 | + expect(addSpy).not.toHaveBeenCalledWith('dragstart', jasmine.any(Function)); |
| 119 | + expect(addSpy).not.toHaveBeenCalledWith('mousedown', jasmine.any(Function)); |
| 120 | + expect(addSpy).not.toHaveBeenCalledWith('mousemove', jasmine.any(Function)); |
| 121 | + expect(addSpy).not.toHaveBeenCalledWith('mouseout', jasmine.any(Function)); |
| 122 | + expect(addSpy).not.toHaveBeenCalledWith('mouseover', jasmine.any(Function)); |
| 123 | + expect(addSpy).not.toHaveBeenCalledWith('mouseup', jasmine.any(Function)); |
| 124 | + expect(addSpy).toHaveBeenCalledWith('rightclick', jasmine.any(Function)); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should be able to add an event listener after init', () => { |
| 128 | + const polygonSpy = createPolygonSpy(polygonOptions); |
| 129 | + createPolygonConstructorSpy(polygonSpy).and.callThrough(); |
| 130 | + |
| 131 | + const addSpy = polygonSpy.addListener; |
| 132 | + const fixture = TestBed.createComponent(TestApp); |
| 133 | + fixture.detectChanges(); |
| 134 | + |
| 135 | + expect(addSpy).not.toHaveBeenCalledWith('dragend', jasmine.any(Function)); |
| 136 | + |
| 137 | + // Pick an event that isn't bound in the template. |
| 138 | + const subscription = fixture.componentInstance.polygon.polygonDragend.subscribe(); |
| 139 | + fixture.detectChanges(); |
| 140 | + |
| 141 | + expect(addSpy).toHaveBeenCalledWith('dragend', jasmine.any(Function)); |
| 142 | + subscription.unsubscribe(); |
| 143 | + }); |
| 144 | +}); |
| 145 | + |
| 146 | +@Component({ |
| 147 | + selector: 'test-app', |
| 148 | + template: `<google-map> |
| 149 | + <map-polygon [options]="options" |
| 150 | + [paths]="paths" |
| 151 | + (polygonClick)="handleClick()" |
| 152 | + (polygonRightclick)="handleRightclick()"> |
| 153 | + </map-polygon> |
| 154 | + </google-map>`, |
| 155 | +}) |
| 156 | +class TestApp { |
| 157 | + @ViewChild(MapPolygon) polygon: MapPolygon; |
| 158 | + options?: google.maps.PolygonOptions; |
| 159 | + paths?: google.maps.LatLngLiteral[]; |
| 160 | + |
| 161 | + handleClick() {} |
| 162 | + |
| 163 | + handleRightclick() {} |
| 164 | +} |
0 commit comments