-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathmap-kml-layer.spec.ts
167 lines (134 loc) · 5.25 KB
/
map-kml-layer.spec.ts
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
162
163
164
165
166
167
import {Component, ViewChild} from '@angular/core';
import {TestBed, fakeAsync, flush} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
import {DEFAULT_OPTIONS, GoogleMap} from '../google-map/google-map';
import {
createKmlLayerConstructorSpy,
createKmlLayerSpy,
createMapConstructorSpy,
createMapSpy,
} from '../testing/fake-google-map-utils';
import {MapKmlLayer} from './map-kml-layer';
const DEMO_URL = 'www.test.kml';
const DEFAULT_KML_OPTIONS: google.maps.KmlLayerOptions = {
clickable: true,
preserveViewport: true,
url: DEMO_URL,
};
describe('MapKmlLayer', () => {
let mapSpy: jasmine.SpyObj<google.maps.Map>;
beforeEach(() => {
mapSpy = createMapSpy(DEFAULT_OPTIONS);
createMapConstructorSpy(mapSpy);
});
afterEach(() => {
(window.google as any) = undefined;
});
it('initializes a Google Map Kml Layer', fakeAsync(() => {
const kmlLayerSpy = createKmlLayerSpy({});
const kmlLayerConstructorSpy = createKmlLayerConstructorSpy(kmlLayerSpy);
const fixture = TestBed.createComponent(TestApp);
fixture.detectChanges();
flush();
expect(kmlLayerConstructorSpy).toHaveBeenCalledWith({url: undefined});
expect(kmlLayerSpy.setMap).toHaveBeenCalledWith(mapSpy);
}));
it('sets url from input', fakeAsync(() => {
const options: google.maps.KmlLayerOptions = {url: DEMO_URL};
const kmlLayerSpy = createKmlLayerSpy(options);
const kmlLayerConstructorSpy = createKmlLayerConstructorSpy(kmlLayerSpy);
const fixture = TestBed.createComponent(TestApp);
fixture.componentInstance.url = DEMO_URL;
fixture.detectChanges();
flush();
expect(kmlLayerConstructorSpy).toHaveBeenCalledWith(options);
}));
it('gives precedence to url input over options', fakeAsync(() => {
const expectedUrl = 'www.realurl.kml';
const expectedOptions: google.maps.KmlLayerOptions = {...DEFAULT_KML_OPTIONS, url: expectedUrl};
const kmlLayerSpy = createKmlLayerSpy(expectedOptions);
const kmlLayerConstructorSpy = createKmlLayerConstructorSpy(kmlLayerSpy);
const fixture = TestBed.createComponent(TestApp);
fixture.componentInstance.options = DEFAULT_KML_OPTIONS;
fixture.componentInstance.url = expectedUrl;
fixture.detectChanges();
flush();
expect(kmlLayerConstructorSpy).toHaveBeenCalledWith(expectedOptions);
}));
it('exposes methods that provide information about the KmlLayer', fakeAsync(() => {
const kmlLayerSpy = createKmlLayerSpy(DEFAULT_KML_OPTIONS);
createKmlLayerConstructorSpy(kmlLayerSpy);
const fixture = TestBed.createComponent(TestApp);
const kmlLayerComponent = fixture.debugElement
.query(By.directive(MapKmlLayer))!
.injector.get<MapKmlLayer>(MapKmlLayer);
fixture.detectChanges();
flush();
kmlLayerComponent.getDefaultViewport();
expect(kmlLayerSpy.getDefaultViewport).toHaveBeenCalled();
const metadata: google.maps.KmlLayerMetadata = {
author: {
email: 'test@test.com',
name: 'author',
uri: 'www.author.com',
},
description: 'test',
hasScreenOverlays: true,
name: 'metadata',
snippet: '...',
};
kmlLayerSpy.getMetadata.and.returnValue(metadata);
expect(kmlLayerComponent.getMetadata()).toBe(metadata);
kmlLayerComponent.getStatus();
expect(kmlLayerSpy.getStatus).toHaveBeenCalled();
kmlLayerSpy.getUrl.and.returnValue(DEMO_URL);
expect(kmlLayerComponent.getUrl()).toBe(DEMO_URL);
kmlLayerSpy.getZIndex.and.returnValue(3);
expect(kmlLayerComponent.getZIndex()).toBe(3);
}));
it('initializes KmlLayer event handlers', fakeAsync(() => {
const kmlLayerSpy = createKmlLayerSpy(DEFAULT_KML_OPTIONS);
createKmlLayerConstructorSpy(kmlLayerSpy);
const addSpy = kmlLayerSpy.addListener;
const fixture = TestBed.createComponent(TestApp);
fixture.detectChanges();
flush();
expect(addSpy).toHaveBeenCalledWith('click', jasmine.any(Function));
expect(addSpy).not.toHaveBeenCalledWith('defaultviewport_changed', jasmine.any(Function));
expect(addSpy).toHaveBeenCalledWith('status_changed', jasmine.any(Function));
}));
it('should be able to add an event listener after init', fakeAsync(() => {
const kmlLayerSpy = createKmlLayerSpy(DEFAULT_KML_OPTIONS);
createKmlLayerConstructorSpy(kmlLayerSpy);
const addSpy = kmlLayerSpy.addListener;
const fixture = TestBed.createComponent(TestApp);
fixture.detectChanges();
flush();
expect(addSpy).not.toHaveBeenCalledWith('defaultviewport_changed', jasmine.any(Function));
// Pick an event that isn't bound in the template.
const subscription = fixture.componentInstance.kmlLayer.defaultviewportChanged.subscribe();
fixture.detectChanges();
expect(addSpy).toHaveBeenCalledWith('defaultviewport_changed', jasmine.any(Function));
subscription.unsubscribe();
}));
});
@Component({
selector: 'test-app',
template: `
<google-map>
<map-kml-layer
[options]="options"
[url]="url"
(kmlClick)="handleClick()"
(statusChanged)="handleStatusChange()" />
</google-map>
`,
imports: [GoogleMap, MapKmlLayer],
})
class TestApp {
@ViewChild(MapKmlLayer) kmlLayer: MapKmlLayer;
options?: google.maps.KmlLayerOptions;
url?: string;
handleClick() {}
handleStatusChange() {}
}