|
| 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 | + createRectangleConstructorSpy, |
| 11 | + createRectangleSpy, |
| 12 | + TestingWindow, |
| 13 | +} from '../testing/fake-google-map-utils'; |
| 14 | + |
| 15 | +import {MapRectangle} from './map-rectangle'; |
| 16 | + |
| 17 | +describe('MapRectangle', () => { |
| 18 | + let mapSpy: jasmine.SpyObj<UpdatedGoogleMap>; |
| 19 | + let rectangleBounds: google.maps.LatLngBoundsLiteral; |
| 20 | + let rectangleOptions: google.maps.RectangleOptions; |
| 21 | + |
| 22 | + beforeEach(async(() => { |
| 23 | + rectangleBounds = {east: 30, north: 15, west: 10, south: -5}; |
| 24 | + rectangleOptions = {bounds: rectangleBounds, 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 Rectangle', () => { |
| 44 | + const rectangleSpy = createRectangleSpy({}); |
| 45 | + const rectangleConstructorSpy = createRectangleConstructorSpy(rectangleSpy).and.callThrough(); |
| 46 | + |
| 47 | + const fixture = TestBed.createComponent(TestApp); |
| 48 | + fixture.detectChanges(); |
| 49 | + |
| 50 | + expect(rectangleConstructorSpy).toHaveBeenCalledWith({bounds: undefined}); |
| 51 | + expect(rectangleSpy.setMap).toHaveBeenCalledWith(mapSpy); |
| 52 | + }); |
| 53 | + |
| 54 | + it('sets bounds from input', () => { |
| 55 | + const bounds: google.maps.LatLngBoundsLiteral = {east: 3, north: 5, west: -3, south: -5}; |
| 56 | + const options: google.maps.RectangleOptions = {bounds}; |
| 57 | + const rectangleSpy = createRectangleSpy(options); |
| 58 | + const rectangleConstructorSpy = createRectangleConstructorSpy(rectangleSpy).and.callThrough(); |
| 59 | + |
| 60 | + const fixture = TestBed.createComponent(TestApp); |
| 61 | + fixture.componentInstance.bounds = bounds; |
| 62 | + fixture.detectChanges(); |
| 63 | + |
| 64 | + expect(rectangleConstructorSpy).toHaveBeenCalledWith(options); |
| 65 | + }); |
| 66 | + |
| 67 | + it('gives precedence to bounds input over options', () => { |
| 68 | + const bounds: google.maps.LatLngBoundsLiteral = {east: 3, north: 5, west: -3, south: -5}; |
| 69 | + const expectedOptions: google.maps.RectangleOptions = {...rectangleOptions, bounds}; |
| 70 | + const rectangleSpy = createRectangleSpy(expectedOptions); |
| 71 | + const rectangleConstructorSpy = createRectangleConstructorSpy(rectangleSpy).and.callThrough(); |
| 72 | + |
| 73 | + const fixture = TestBed.createComponent(TestApp); |
| 74 | + fixture.componentInstance.options = rectangleOptions; |
| 75 | + fixture.componentInstance.bounds = bounds; |
| 76 | + fixture.detectChanges(); |
| 77 | + |
| 78 | + expect(rectangleConstructorSpy).toHaveBeenCalledWith(expectedOptions); |
| 79 | + }); |
| 80 | + |
| 81 | + it('exposes methods that provide information about the Rectangle', () => { |
| 82 | + const rectangleSpy = createRectangleSpy(rectangleOptions); |
| 83 | + createRectangleConstructorSpy(rectangleSpy).and.callThrough(); |
| 84 | + |
| 85 | + const fixture = TestBed.createComponent(TestApp); |
| 86 | + const rectangleComponent = fixture.debugElement.query(By.directive( |
| 87 | + MapRectangle))!.injector.get<MapRectangle>(MapRectangle); |
| 88 | + fixture.detectChanges(); |
| 89 | + |
| 90 | + rectangleComponent.getBounds(); |
| 91 | + expect(rectangleSpy.getBounds).toHaveBeenCalled(); |
| 92 | + |
| 93 | + rectangleSpy.getDraggable.and.returnValue(true); |
| 94 | + expect(rectangleComponent.getDraggable()).toBe(true); |
| 95 | + |
| 96 | + rectangleSpy.getEditable.and.returnValue(true); |
| 97 | + expect(rectangleComponent.getEditable()).toBe(true); |
| 98 | + |
| 99 | + rectangleSpy.getVisible.and.returnValue(true); |
| 100 | + expect(rectangleComponent.getVisible()).toBe(true); |
| 101 | + }); |
| 102 | + |
| 103 | + it('initializes Rectangle event handlers', () => { |
| 104 | + const rectangleSpy = createRectangleSpy(rectangleOptions); |
| 105 | + createRectangleConstructorSpy(rectangleSpy).and.callThrough(); |
| 106 | + |
| 107 | + const addSpy = rectangleSpy.addListener; |
| 108 | + const fixture = TestBed.createComponent(TestApp); |
| 109 | + fixture.detectChanges(); |
| 110 | + |
| 111 | + expect(addSpy).toHaveBeenCalledWith('bounds_changed', jasmine.any(Function)); |
| 112 | + expect(addSpy).toHaveBeenCalledWith('click', jasmine.any(Function)); |
| 113 | + expect(addSpy).not.toHaveBeenCalledWith('dblclick', jasmine.any(Function)); |
| 114 | + expect(addSpy).not.toHaveBeenCalledWith('drag', jasmine.any(Function)); |
| 115 | + expect(addSpy).not.toHaveBeenCalledWith('dragend', jasmine.any(Function)); |
| 116 | + expect(addSpy).not.toHaveBeenCalledWith('dragstart', jasmine.any(Function)); |
| 117 | + expect(addSpy).not.toHaveBeenCalledWith('mousedown', jasmine.any(Function)); |
| 118 | + expect(addSpy).not.toHaveBeenCalledWith('mousemove', jasmine.any(Function)); |
| 119 | + expect(addSpy).not.toHaveBeenCalledWith('mouseout', jasmine.any(Function)); |
| 120 | + expect(addSpy).not.toHaveBeenCalledWith('mouseover', jasmine.any(Function)); |
| 121 | + expect(addSpy).not.toHaveBeenCalledWith('mouseup', jasmine.any(Function)); |
| 122 | + expect(addSpy).toHaveBeenCalledWith('rightclick', jasmine.any(Function)); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should be able to add an event listener after init', () => { |
| 126 | + const rectangleSpy = createRectangleSpy(rectangleOptions); |
| 127 | + createRectangleConstructorSpy(rectangleSpy).and.callThrough(); |
| 128 | + |
| 129 | + const addSpy = rectangleSpy.addListener; |
| 130 | + const fixture = TestBed.createComponent(TestApp); |
| 131 | + fixture.detectChanges(); |
| 132 | + |
| 133 | + expect(addSpy).not.toHaveBeenCalledWith('dragend', jasmine.any(Function)); |
| 134 | + |
| 135 | + // Pick an event that isn't bound in the template. |
| 136 | + const subscription = fixture.componentInstance.rectangle.rectangleDragend.subscribe(); |
| 137 | + fixture.detectChanges(); |
| 138 | + |
| 139 | + expect(addSpy).toHaveBeenCalledWith('dragend', jasmine.any(Function)); |
| 140 | + subscription.unsubscribe(); |
| 141 | + }); |
| 142 | +}); |
| 143 | + |
| 144 | +@Component({ |
| 145 | + selector: 'test-app', |
| 146 | + template: `<google-map> |
| 147 | + <map-rectangle [options]="options" |
| 148 | + [bounds]="bounds" |
| 149 | + (boundsChanged)="handleBoundsChange()" |
| 150 | + (rectangleClick)="handleClick()" |
| 151 | + (rectangleRightclick)="handleRightclick()"> |
| 152 | + </map-rectangle> |
| 153 | + </google-map>`, |
| 154 | +}) |
| 155 | +class TestApp { |
| 156 | + @ViewChild(MapRectangle) rectangle: MapRectangle; |
| 157 | + options?: google.maps.RectangleOptions; |
| 158 | + bounds?: google.maps.LatLngBoundsLiteral; |
| 159 | + |
| 160 | + handleBoundsChange() {} |
| 161 | + |
| 162 | + handleClick() {} |
| 163 | + |
| 164 | + handleRightclick() {} |
| 165 | +} |
0 commit comments