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