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