Skip to content

Commit c895595

Browse files
authored
test(google-maps): move more tests away from fakeAsync (#33521)
Reworks more of the Google Maps tests not to depend on `fakeAsync`. There are a couple that can't be switched easily.
1 parent def7bc2 commit c895595

2 files changed

Lines changed: 88 additions & 53 deletions

File tree

‎src/google-maps/deprecated-map-marker-clusterer/deprecated-map-marker-clusterer.spec.ts‎

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Component, ViewChild, ChangeDetectionStrategy} from '@angular/core';
2-
import {ComponentFixture, TestBed, fakeAsync, flush} from '@angular/core/testing';
2+
import {ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing';
33

44
import {DEFAULT_OPTIONS, GoogleMap} from '../google-map/google-map';
55
import {MapMarker} from '../map-marker/map-marker';
@@ -51,6 +51,7 @@ describe('DeprecatedMapMarkerClusterer', () => {
5151
(window as any).MarkerClusterer = undefined;
5252
});
5353

54+
// We can't test this one easily without `fakeAsync`.
5455
it('throws an error if the clustering library has not been loaded', fakeAsync(() => {
5556
(window as any).MarkerClusterer = undefined;
5657
markerClustererConstructorSpy = createDeprecatedMarkerClustererConstructorSpy(
@@ -64,9 +65,10 @@ describe('DeprecatedMapMarkerClusterer', () => {
6465
}).toThrowError(/MarkerClusterer class not found, cannot construct a marker cluster/);
6566
}));
6667

67-
it('initializes a Google Map Marker Clusterer', fakeAsync(() => {
68+
it('initializes a Google Map Marker Clusterer', async () => {
6869
fixture.detectChanges();
69-
flush();
70+
await fixture.whenStable();
71+
await wait(50);
7072

7173
expect(markerClustererConstructorSpy).toHaveBeenCalledWith(mapSpy, [], {
7274
ariaLabelFn: undefined,
@@ -88,9 +90,9 @@ describe('DeprecatedMapMarkerClusterer', () => {
8890
zIndex: undefined,
8991
zoomOnClick: undefined,
9092
});
91-
}));
93+
});
9294

93-
it('sets marker clusterer inputs', fakeAsync(() => {
95+
it('sets marker clusterer inputs', async () => {
9496
fixture.componentInstance.ariaLabelFn = (testString: string) => testString;
9597
fixture.componentInstance.averageCenter = true;
9698
fixture.componentInstance.batchSize = 1;
@@ -109,7 +111,8 @@ describe('DeprecatedMapMarkerClusterer', () => {
109111
fixture.componentInstance.zoomOnClick = true;
110112
fixture.changeDetectorRef.markForCheck();
111113
fixture.detectChanges();
112-
flush();
114+
await fixture.whenStable();
115+
await wait(50);
113116

114117
expect(markerClustererConstructorSpy).toHaveBeenCalledWith(mapSpy, [], {
115118
ariaLabelFn: jasmine.any(Function),
@@ -131,11 +134,12 @@ describe('DeprecatedMapMarkerClusterer', () => {
131134
zIndex: 6,
132135
zoomOnClick: true,
133136
});
134-
}));
137+
});
135138

136-
it('sets marker clusterer options', fakeAsync(() => {
139+
it('sets marker clusterer options', async () => {
137140
fixture.detectChanges();
138-
flush();
141+
await fixture.whenStable();
142+
await wait(50);
139143
const options: MarkerClustererOptions = {
140144
enableRetinaIcons: true,
141145
gridSize: 1337,
@@ -145,12 +149,15 @@ describe('DeprecatedMapMarkerClusterer', () => {
145149
fixture.componentInstance.options = options;
146150
fixture.changeDetectorRef.markForCheck();
147151
fixture.detectChanges();
152+
await fixture.whenStable();
153+
await wait(50);
148154
expect(markerClustererSpy.setOptions).toHaveBeenCalledWith(jasmine.objectContaining(options));
149-
}));
155+
});
150156

151-
it('gives precedence to specific inputs over options', fakeAsync(() => {
157+
it('gives precedence to specific inputs over options', async () => {
152158
fixture.detectChanges();
153-
flush();
159+
await fixture.whenStable();
160+
await wait(50);
154161
const options: MarkerClustererOptions = {
155162
enableRetinaIcons: true,
156163
gridSize: 1337,
@@ -170,25 +177,29 @@ describe('DeprecatedMapMarkerClusterer', () => {
170177
fixture.componentInstance.options = options;
171178
fixture.changeDetectorRef.markForCheck();
172179
fixture.detectChanges();
180+
await fixture.whenStable();
181+
await wait(50);
173182

174183
expect(markerClustererSpy.setOptions).toHaveBeenCalledWith(
175184
jasmine.objectContaining(expectedOptions),
176185
);
177-
}));
186+
});
178187

179-
it('sets Google Maps Markers in the MarkerClusterer', fakeAsync(() => {
188+
it('sets Google Maps Markers in the MarkerClusterer', async () => {
180189
fixture.detectChanges();
181-
flush();
190+
await fixture.whenStable();
191+
await wait(50);
182192

183193
expect(markerClustererSpy.addMarkers).toHaveBeenCalledWith([
184194
anyMarkerMatcher,
185195
anyMarkerMatcher,
186196
]);
187-
}));
197+
});
188198

189-
it('updates Google Maps Markers in the Marker Clusterer', fakeAsync(() => {
199+
it('updates Google Maps Markers in the Marker Clusterer', async () => {
190200
fixture.detectChanges();
191-
flush();
201+
await fixture.whenStable();
202+
await wait(50);
192203

193204
expect(markerClustererSpy.addMarkers).toHaveBeenCalledWith([
194205
anyMarkerMatcher,
@@ -198,7 +209,8 @@ describe('DeprecatedMapMarkerClusterer', () => {
198209
fixture.componentInstance.state = 'state2';
199210
fixture.changeDetectorRef.markForCheck();
200211
fixture.detectChanges();
201-
flush();
212+
await fixture.whenStable();
213+
await wait(50);
202214

203215
expect(markerClustererSpy.addMarkers).toHaveBeenCalledWith([anyMarkerMatcher], true);
204216
expect(markerClustererSpy.removeMarkers).toHaveBeenCalledWith([anyMarkerMatcher], true);
@@ -207,19 +219,21 @@ describe('DeprecatedMapMarkerClusterer', () => {
207219
fixture.componentInstance.state = 'state0';
208220
fixture.changeDetectorRef.markForCheck();
209221
fixture.detectChanges();
210-
flush();
222+
await fixture.whenStable();
223+
await wait(50);
211224

212225
expect(markerClustererSpy.addMarkers).toHaveBeenCalledWith([], true);
213226
expect(markerClustererSpy.removeMarkers).toHaveBeenCalledWith(
214227
[anyMarkerMatcher, anyMarkerMatcher],
215228
true,
216229
);
217230
expect(markerClustererSpy.repaint).toHaveBeenCalledTimes(2);
218-
}));
231+
});
219232

220-
it('exposes marker clusterer methods', fakeAsync(() => {
233+
it('exposes marker clusterer methods', async () => {
221234
fixture.detectChanges();
222-
flush();
235+
await fixture.whenStable();
236+
await wait(50);
223237
const markerClustererComponent = fixture.componentInstance.markerClusterer;
224238

225239
markerClustererComponent.fitMapToMarkers(5);
@@ -286,11 +300,12 @@ describe('DeprecatedMapMarkerClusterer', () => {
286300

287301
markerClustererSpy.getZoomOnClick.and.returnValue(true);
288302
expect(markerClustererComponent.getZoomOnClick()).toBe(true);
289-
}));
303+
});
290304

291-
it('initializes marker clusterer event handlers', fakeAsync(() => {
305+
it('initializes marker clusterer event handlers', async () => {
292306
fixture.detectChanges();
293-
flush();
307+
await fixture.whenStable();
308+
await wait(50);
294309

295310
expect(markerClustererSpy.addListener).toHaveBeenCalledWith(
296311
'clusteringbegin',
@@ -301,9 +316,13 @@ describe('DeprecatedMapMarkerClusterer', () => {
301316
jasmine.any(Function),
302317
);
303318
expect(markerClustererSpy.addListener).toHaveBeenCalledWith('click', jasmine.any(Function));
304-
}));
319+
});
305320
});
306321

322+
function wait(milliseconds: number) {
323+
return new Promise(resolve => setTimeout(resolve, milliseconds));
324+
}
325+
307326
@Component({
308327
selector: 'test-app',
309328
template: `

‎src/google-maps/map-marker-clusterer/map-marker-clusterer.spec.ts‎

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Component, ViewChild, ChangeDetectionStrategy} from '@angular/core';
2-
import {ComponentFixture, TestBed, fakeAsync, flush} from '@angular/core/testing';
2+
import {ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing';
33
import type {MarkerClusterer, Renderer, Algorithm} from './map-marker-clusterer-types';
44

55
import {DEFAULT_OPTIONS} from '../google-map/google-map';
@@ -46,6 +46,7 @@ describe('MapMarkerClusterer', () => {
4646
(window as any).markerClusterer = undefined;
4747
});
4848

49+
// We can't test this easily without `fakeAsync`.
4950
it('throws an error if the clustering library has not been loaded', fakeAsync(() => {
5051
(window as any).markerClusterer = undefined;
5152
markerClustererConstructorSpy = createMarkerClustererConstructorSpy(
@@ -59,37 +60,40 @@ describe('MapMarkerClusterer', () => {
5960
}).toThrowError(/MarkerClusterer class not found, cannot construct a marker cluster/);
6061
}));
6162

62-
it('initializes a Google Map Marker Clusterer', fakeAsync(() => {
63+
it('initializes a Google Map Marker Clusterer', async () => {
6364
fixture.detectChanges();
64-
flush();
65+
await fixture.whenStable();
66+
await wait(50);
6567

6668
expect(markerClustererConstructorSpy).toHaveBeenCalledWith({
6769
map: mapSpy,
6870
renderer: undefined,
6971
algorithm: undefined,
7072
onClusterClick: jasmine.any(Function),
7173
});
72-
}));
74+
});
7375

74-
it('sets marker clusterer inputs', fakeAsync(() => {
76+
it('sets marker clusterer inputs', async () => {
7577
fixture.componentInstance.algorithm = {name: 'custom'} as any;
7678
fixture.componentInstance.renderer = {render: () => null!};
7779
fixture.detectChanges();
78-
flush();
80+
await fixture.whenStable();
81+
await wait(50);
7982

8083
expect(markerClustererConstructorSpy).toHaveBeenCalledWith({
8184
map: mapSpy,
8285
algorithm: fixture.componentInstance.algorithm,
8386
renderer: fixture.componentInstance.renderer,
8487
onClusterClick: jasmine.any(Function),
8588
});
86-
}));
89+
});
8790

88-
it('recreates the clusterer if the options change', fakeAsync(() => {
91+
it('recreates the clusterer if the options change', async () => {
8992
fixture.componentInstance.algorithm = {name: 'custom1'} as any;
9093
fixture.changeDetectorRef.markForCheck();
9194
fixture.detectChanges();
92-
flush();
95+
await fixture.whenStable();
96+
await wait(50);
9397

9498
expect(markerClustererConstructorSpy).toHaveBeenCalledWith({
9599
map: mapSpy,
@@ -101,29 +105,32 @@ describe('MapMarkerClusterer', () => {
101105
fixture.componentInstance.algorithm = {name: 'custom2'} as any;
102106
fixture.changeDetectorRef.markForCheck();
103107
fixture.detectChanges();
104-
flush();
108+
await fixture.whenStable();
109+
await wait(50);
105110

106111
expect(markerClustererConstructorSpy).toHaveBeenCalledWith({
107112
map: mapSpy,
108113
algorithm: jasmine.objectContaining({name: 'custom2'}),
109114
renderer: undefined,
110115
onClusterClick: jasmine.any(Function),
111116
});
112-
}));
117+
});
113118

114-
it('sets Google Maps Markers in the MarkerClusterer', fakeAsync(() => {
119+
it('sets Google Maps Markers in the MarkerClusterer', async () => {
115120
fixture.detectChanges();
116-
flush();
121+
await fixture.whenStable();
122+
await wait(50);
117123

118124
expect(markerClustererSpy.addMarkers).toHaveBeenCalledWith([
119125
anyMarkerMatcher,
120126
anyMarkerMatcher,
121127
]);
122-
}));
128+
});
123129

124-
it('updates Google Maps Markers in the Marker Clusterer', fakeAsync(() => {
130+
it('updates Google Maps Markers in the Marker Clusterer', async () => {
125131
fixture.detectChanges();
126-
flush();
132+
await fixture.whenStable();
133+
await wait(50);
127134

128135
expect(markerClustererSpy.addMarkers).toHaveBeenCalledWith([
129136
anyMarkerMatcher,
@@ -133,7 +140,8 @@ describe('MapMarkerClusterer', () => {
133140
fixture.componentInstance.state = 'state2';
134141
fixture.changeDetectorRef.markForCheck();
135142
fixture.detectChanges();
136-
flush();
143+
await fixture.whenStable();
144+
await wait(50);
137145

138146
expect(markerClustererSpy.addMarkers).toHaveBeenCalledWith([anyMarkerMatcher], true);
139147
expect(markerClustererSpy.removeMarkers).toHaveBeenCalledWith([anyMarkerMatcher], true);
@@ -142,39 +150,47 @@ describe('MapMarkerClusterer', () => {
142150
fixture.componentInstance.state = 'state0';
143151
fixture.changeDetectorRef.markForCheck();
144152
fixture.detectChanges();
145-
flush();
153+
await fixture.whenStable();
154+
await wait(50);
146155

147156
expect(markerClustererSpy.addMarkers).toHaveBeenCalledWith([], true);
148157
expect(markerClustererSpy.removeMarkers).toHaveBeenCalledWith(
149158
[anyMarkerMatcher, anyMarkerMatcher],
150159
true,
151160
);
152161
expect(markerClustererSpy.render).toHaveBeenCalledTimes(2);
153-
}));
162+
});
154163

155-
it('initializes event handlers on the map related to clustering', fakeAsync(() => {
164+
it('initializes event handlers on the map related to clustering', async () => {
156165
fixture.detectChanges();
157-
flush();
166+
await fixture.whenStable();
167+
await wait(50);
158168

159169
expect(mapSpy.addListener).toHaveBeenCalledWith('clusteringbegin', jasmine.any(Function));
160170
expect(mapSpy.addListener).not.toHaveBeenCalledWith('clusteringend', jasmine.any(Function));
161-
}));
171+
});
162172

163-
it('emits to clusterClick when the `onClusterClick` callback is invoked', fakeAsync(() => {
173+
it('emits to clusterClick when the `onClusterClick` callback is invoked', async () => {
164174
fixture.detectChanges();
165-
flush();
175+
await fixture.whenStable();
176+
await wait(50);
166177

167178
expect(fixture.componentInstance.onClusterClick).not.toHaveBeenCalled();
168179

169180
const callback = markerClustererConstructorSpy.calls.mostRecent().args[0].onClusterClick;
170181
callback({}, {}, {});
171182
fixture.detectChanges();
172-
flush();
183+
await fixture.whenStable();
184+
await wait(50);
173185

174186
expect(fixture.componentInstance.onClusterClick).toHaveBeenCalledTimes(1);
175-
}));
187+
});
176188
});
177189

190+
function wait(milliseconds: number) {
191+
return new Promise(resolve => setTimeout(resolve, milliseconds));
192+
}
193+
178194
@Component({
179195
selector: 'test-app',
180196
imports: [GoogleMapsModule],

0 commit comments

Comments
 (0)