-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathmap-rectangle.spec.ts
More file actions
161 lines (128 loc) · 5.87 KB
/
Copy pathmap-rectangle.spec.ts
File metadata and controls
161 lines (128 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import {Component, ViewChild, ChangeDetectionStrategy} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
import {DEFAULT_OPTIONS, GoogleMap} from '../google-map/google-map';
import {
createMapConstructorSpy,
createMapSpy,
createRectangleConstructorSpy,
createRectangleSpy,
} from '../testing/fake-google-map-utils';
import {MapRectangle} from './map-rectangle';
describe('MapRectangle', () => {
let mapSpy: jasmine.SpyObj<google.maps.Map>;
let rectangleBounds: google.maps.LatLngBoundsLiteral;
let rectangleOptions: google.maps.RectangleOptions;
beforeEach(() => {
rectangleBounds = {east: 30, north: 15, west: 10, south: -5};
rectangleOptions = {bounds: rectangleBounds, strokeColor: 'grey', strokeOpacity: 0.8};
});
beforeEach(() => {
mapSpy = createMapSpy(DEFAULT_OPTIONS);
createMapConstructorSpy(mapSpy);
});
afterEach(() => {
(window.google as any) = undefined;
});
it('initializes a Google Map Rectangle', () => {
const rectangleSpy = createRectangleSpy({});
const rectangleConstructorSpy = createRectangleConstructorSpy(rectangleSpy);
const fixture = TestBed.createComponent(TestApp);
fixture.detectChanges();
expect(rectangleConstructorSpy).toHaveBeenCalledWith({bounds: undefined});
expect(rectangleSpy.setMap).toHaveBeenCalledWith(mapSpy);
});
it('sets bounds from input', () => {
const bounds: google.maps.LatLngBoundsLiteral = {east: 3, north: 5, west: -3, south: -5};
const options: google.maps.RectangleOptions = {bounds};
const rectangleSpy = createRectangleSpy(options);
const rectangleConstructorSpy = createRectangleConstructorSpy(rectangleSpy);
const fixture = TestBed.createComponent(TestApp);
fixture.componentInstance.bounds = bounds;
fixture.detectChanges();
expect(rectangleConstructorSpy).toHaveBeenCalledWith(options);
});
it('gives precedence to bounds input over options', () => {
const bounds: google.maps.LatLngBoundsLiteral = {east: 3, north: 5, west: -3, south: -5};
const expectedOptions: google.maps.RectangleOptions = {...rectangleOptions, bounds};
const rectangleSpy = createRectangleSpy(expectedOptions);
const rectangleConstructorSpy = createRectangleConstructorSpy(rectangleSpy);
const fixture = TestBed.createComponent(TestApp);
fixture.componentInstance.options = rectangleOptions;
fixture.componentInstance.bounds = bounds;
fixture.detectChanges();
expect(rectangleConstructorSpy).toHaveBeenCalledWith(expectedOptions);
});
it('exposes methods that provide information about the Rectangle', () => {
const rectangleSpy = createRectangleSpy(rectangleOptions);
createRectangleConstructorSpy(rectangleSpy);
const fixture = TestBed.createComponent(TestApp);
const rectangleComponent = fixture.debugElement
.query(By.directive(MapRectangle))!
.injector.get<MapRectangle>(MapRectangle);
fixture.detectChanges();
rectangleComponent.getBounds();
expect(rectangleSpy.getBounds).toHaveBeenCalled();
rectangleSpy.getDraggable.and.returnValue(true);
expect(rectangleComponent.getDraggable()).toBe(true);
rectangleSpy.getEditable.and.returnValue(true);
expect(rectangleComponent.getEditable()).toBe(true);
rectangleSpy.getVisible.and.returnValue(true);
expect(rectangleComponent.getVisible()).toBe(true);
});
it('initializes Rectangle event handlers', () => {
const rectangleSpy = createRectangleSpy(rectangleOptions);
createRectangleConstructorSpy(rectangleSpy);
const addSpy = rectangleSpy.addListener;
const fixture = TestBed.createComponent(TestApp);
fixture.detectChanges();
expect(addSpy).toHaveBeenCalledWith('bounds_changed', jasmine.any(Function));
expect(addSpy).toHaveBeenCalledWith('click', jasmine.any(Function));
expect(addSpy).not.toHaveBeenCalledWith('dblclick', jasmine.any(Function));
expect(addSpy).not.toHaveBeenCalledWith('drag', jasmine.any(Function));
expect(addSpy).not.toHaveBeenCalledWith('dragend', jasmine.any(Function));
expect(addSpy).not.toHaveBeenCalledWith('dragstart', jasmine.any(Function));
expect(addSpy).not.toHaveBeenCalledWith('mousedown', jasmine.any(Function));
expect(addSpy).not.toHaveBeenCalledWith('mousemove', jasmine.any(Function));
expect(addSpy).not.toHaveBeenCalledWith('mouseout', jasmine.any(Function));
expect(addSpy).not.toHaveBeenCalledWith('mouseover', jasmine.any(Function));
expect(addSpy).not.toHaveBeenCalledWith('mouseup', jasmine.any(Function));
expect(addSpy).toHaveBeenCalledWith('rightclick', jasmine.any(Function));
});
it('should be able to add an event listener after init', () => {
const rectangleSpy = createRectangleSpy(rectangleOptions);
createRectangleConstructorSpy(rectangleSpy);
const addSpy = rectangleSpy.addListener;
const fixture = TestBed.createComponent(TestApp);
fixture.detectChanges();
expect(addSpy).not.toHaveBeenCalledWith('dragend', jasmine.any(Function));
// Pick an event that isn't bound in the template.
const subscription = fixture.componentInstance.rectangle.rectangleDragend.subscribe();
fixture.detectChanges();
expect(addSpy).toHaveBeenCalledWith('dragend', jasmine.any(Function));
subscription.unsubscribe();
});
});
@Component({
selector: 'test-app',
template: `
<google-map>
<map-rectangle
[options]="options"
[bounds]="bounds"
(boundsChanged)="handleBoundsChange()"
(rectangleClick)="handleClick()"
(rectangleRightclick)="handleRightclick()" />
</google-map>
`,
imports: [GoogleMap, MapRectangle],
changeDetection: ChangeDetectionStrategy.Eager,
})
class TestApp {
@ViewChild(MapRectangle) rectangle!: MapRectangle;
options!: google.maps.RectangleOptions;
bounds!: google.maps.LatLngBoundsLiteral;
handleBoundsChange() {}
handleClick() {}
handleRightclick() {}
}