Skip to content

Commit 0d1dc02

Browse files
devversionzarend
authored andcommitted
refactor: update ESM incompatible code to work with ESM compilation
There were a couple of code places which started breaking with us switching the devmode/prodmode target and mode to ES2020: * Since devmode is now ES2020, arrow functions are no longer downleveled for tests to actual `function` declarations. This breaks spies in google-maps or the youtube-player package as the tests try to instantiate a spy using `new`. An arrow function cannot be instantiated through `new`.. so we manually update these spies to not use arrow functions. * The MDC-based dialog harness extended from the non-MDC dialog harness and the `with` static member conflicted. It's unclear why this in particular happened but it was straightforward enough to just introduce a base class for the dialog harness (like it is done in other places as well).
1 parent 891841b commit 0d1dc02

File tree

6 files changed

+75
-56
lines changed

6 files changed

+75
-56
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ describe('MapMarkerClusterer', () => {
3535
createMapConstructorSpy(mapSpy).and.callThrough();
3636

3737
const markerSpy = createMarkerSpy({});
38-
createMarkerConstructorSpy(markerSpy).and.callFake(() => {
38+
// The spy target function cannot be an arrow-function as this breaks when created
39+
// through `new`.
40+
createMarkerConstructorSpy(markerSpy).and.callFake(function() {
3941
return createMarkerSpy({});
4042
});
4143

src/google-maps/testing/fake-google-map-utils.ts

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ export function createMapSpy(options: google.maps.MapOptions): jasmine.SpyObj<go
5454
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.Map. */
5555
export function createMapConstructorSpy(
5656
mapSpy: jasmine.SpyObj<google.maps.Map>, apiLoaded = true): jasmine.Spy {
57+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
5758
const mapConstructorSpy =
58-
jasmine.createSpy('Map constructor', (_el: Element, _options: google.maps.MapOptions) => {
59+
jasmine.createSpy('Map constructor', function() {
5960
return mapSpy;
6061
});
6162
const testingWindow: TestingWindow = window;
@@ -84,8 +85,9 @@ export function createMarkerSpy(options: google.maps.MarkerOptions):
8485
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.Marker */
8586
export function createMarkerConstructorSpy(markerSpy: jasmine.SpyObj<google.maps.Marker>):
8687
jasmine.Spy {
88+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
8789
const markerConstructorSpy =
88-
jasmine.createSpy('Marker constructor', (_options: google.maps.MarkerOptions) => {
90+
jasmine.createSpy('Marker constructor', function() {
8991
return markerSpy;
9092
});
9193
const testingWindow: TestingWindow = window;
@@ -118,8 +120,9 @@ export function createInfoWindowSpy(options: google.maps.InfoWindowOptions):
118120
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.InfoWindow */
119121
export function createInfoWindowConstructorSpy(
120122
infoWindowSpy: jasmine.SpyObj<google.maps.InfoWindow>): jasmine.Spy {
123+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
121124
const infoWindowConstructorSpy =
122-
jasmine.createSpy('InfoWindow constructor', (_options: google.maps.InfoWindowOptions) => {
125+
jasmine.createSpy('InfoWindow constructor', function() {
123126
return infoWindowSpy;
124127
});
125128
const testingWindow: TestingWindow = window;
@@ -149,8 +152,9 @@ export function createPolylineSpy(options: google.maps.PolylineOptions):
149152
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.Polyline */
150153
export function createPolylineConstructorSpy(polylineSpy: jasmine.SpyObj<google.maps.Polyline>):
151154
jasmine.Spy {
155+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
152156
const polylineConstructorSpy =
153-
jasmine.createSpy('Polyline constructor', (_options: google.maps.PolylineOptions) => {
157+
jasmine.createSpy('Polyline constructor', function() {
154158
return polylineSpy;
155159
});
156160
const testingWindow: TestingWindow = window;
@@ -180,8 +184,9 @@ export function createPolygonSpy(options: google.maps.PolygonOptions):
180184
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.Polygon */
181185
export function createPolygonConstructorSpy(polygonSpy: jasmine.SpyObj<google.maps.Polygon>):
182186
jasmine.Spy {
187+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
183188
const polygonConstructorSpy =
184-
jasmine.createSpy('Polygon constructor', (_options: google.maps.PolygonOptions) => {
189+
jasmine.createSpy('Polygon constructor', function() {
185190
return polygonSpy;
186191
});
187192
const testingWindow: TestingWindow = window;
@@ -211,8 +216,9 @@ export function createRectangleSpy(options: google.maps.RectangleOptions):
211216
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.Rectangle */
212217
export function createRectangleConstructorSpy(rectangleSpy: jasmine.SpyObj<google.maps.Rectangle>):
213218
jasmine.Spy {
219+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
214220
const rectangleConstructorSpy =
215-
jasmine.createSpy('Rectangle constructor', (_options: google.maps.RectangleOptions) => {
221+
jasmine.createSpy('Rectangle constructor', function() {
216222
return rectangleSpy;
217223
});
218224
const testingWindow: TestingWindow = window;
@@ -242,10 +248,10 @@ export function createCircleSpy(options: google.maps.CircleOptions):
242248
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.Circle */
243249
export function createCircleConstructorSpy(circleSpy: jasmine.SpyObj<google.maps.Circle>):
244250
jasmine.Spy {
245-
const circleConstructorSpy =
246-
jasmine.createSpy('Circle constructor', (_options: google.maps.CircleOptions) => {
247-
return circleSpy;
248-
});
251+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
252+
const circleConstructorSpy = jasmine.createSpy('Circle constructor', function() {
253+
return circleSpy;
254+
});
249255
const testingWindow: TestingWindow = window;
250256
if (testingWindow.google && testingWindow.google.maps) {
251257
testingWindow.google.maps['Circle'] = circleConstructorSpy;
@@ -281,12 +287,10 @@ export function createGroundOverlaySpy(
281287
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.GroundOverlay */
282288
export function createGroundOverlayConstructorSpy(
283289
groundOverlaySpy: jasmine.SpyObj<google.maps.GroundOverlay>): jasmine.Spy {
284-
const groundOverlayConstructorSpy = jasmine.createSpy(
285-
'GroundOverlay constructor',
286-
(_url: string, _bounds: google.maps.LatLngBoundsLiteral,
287-
_options: google.maps.GroundOverlayOptions) => {
288-
return groundOverlaySpy;
289-
});
290+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
291+
const groundOverlayConstructorSpy = jasmine.createSpy('GroundOverlay constructor', function() {
292+
return groundOverlaySpy;
293+
});
290294
const testingWindow: TestingWindow = window;
291295
if (testingWindow.google && testingWindow.google.maps) {
292296
testingWindow.google.maps['GroundOverlay'] = groundOverlayConstructorSpy;
@@ -321,8 +325,9 @@ export function createKmlLayerSpy(options?: google.maps.KmlLayerOptions):
321325
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.KmlLayer */
322326
export function createKmlLayerConstructorSpy(kmlLayerSpy: jasmine.SpyObj<google.maps.KmlLayer>):
323327
jasmine.Spy {
328+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
324329
const kmlLayerConstructorSpy =
325-
jasmine.createSpy('KmlLayer constructor', (_options: google.maps.KmlLayerOptions) => {
330+
jasmine.createSpy('KmlLayer constructor', function() {
326331
return kmlLayerSpy;
327332
});
328333
const testingWindow: TestingWindow = window;
@@ -351,8 +356,9 @@ export function createTrafficLayerSpy(options?: google.maps.TrafficLayerOptions)
351356
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.TrafficLayer */
352357
export function createTrafficLayerConstructorSpy(
353358
trafficLayerSpy: jasmine.SpyObj<google.maps.TrafficLayer>): jasmine.Spy {
359+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
354360
const trafficLayerConstructorSpy =
355-
jasmine.createSpy('TrafficLayer constructor', (_options: google.maps.TrafficLayerOptions) => {
361+
jasmine.createSpy('TrafficLayer constructor', function() {
356362
return trafficLayerSpy;
357363
});
358364
const testingWindow: TestingWindow = window;
@@ -379,7 +385,8 @@ export function createTransitLayerSpy(): jasmine.SpyObj<google.maps.TransitLayer
379385
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.TransitLayer */
380386
export function createTransitLayerConstructorSpy(
381387
transitLayerSpy: jasmine.SpyObj<google.maps.TransitLayer>): jasmine.Spy {
382-
const transitLayerConstructorSpy = jasmine.createSpy('TransitLayer constructor', () => {
388+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
389+
const transitLayerConstructorSpy = jasmine.createSpy('TransitLayer constructor', function() {
383390
return transitLayerSpy;
384391
});
385392
const testingWindow: TestingWindow = window;
@@ -406,7 +413,8 @@ export function createBicyclingLayerSpy(): jasmine.SpyObj<google.maps.BicyclingL
406413
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.BicyclingLayer */
407414
export function createBicyclingLayerConstructorSpy(
408415
bicylingLayerSpy: jasmine.SpyObj<google.maps.BicyclingLayer>): jasmine.Spy {
409-
const bicylingLayerConstructorSpy = jasmine.createSpy('BicyclingLayer constructor', () => {
416+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
417+
const bicylingLayerConstructorSpy = jasmine.createSpy('BicyclingLayer constructor', function() {
410418
return bicylingLayerSpy;
411419
});
412420
const testingWindow: TestingWindow = window;
@@ -443,10 +451,9 @@ export function createMarkerClustererSpy(): jasmine.SpyObj<MarkerClusterer> {
443451
/** Creates a jasmine.Spy to watch for the constructor of a MarkerClusterer */
444452
export function createMarkerClustererConstructorSpy(
445453
markerClustererSpy: jasmine.SpyObj<MarkerClusterer>, apiLoaded = true): jasmine.Spy {
454+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
446455
const markerClustererConstructorSpy = jasmine.createSpy('MarkerClusterer constructor',
447-
() => {
448-
return markerClustererSpy;
449-
});
456+
function() { return markerClustererSpy; });
450457
if (apiLoaded) {
451458
const testingWindow: TestingWindow = window;
452459
testingWindow['MarkerClusterer'] = markerClustererConstructorSpy;
@@ -467,10 +474,9 @@ export function createDirectionsRendererSpy(options: google.maps.DirectionsRende
467474
/** Creates a jasmine.Spy to watch for the constructor of a DirectionsRenderer */
468475
export function createDirectionsRendererConstructorSpy(
469476
directionsRendererSpy: jasmine.SpyObj<google.maps.DirectionsRenderer>): jasmine.Spy {
477+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
470478
const directionsRendererConstructorSpy = jasmine.createSpy('DirectionsRenderer constructor',
471-
(_options: google.maps.DirectionsRendererOptions) => {
472-
return directionsRendererSpy;
473-
});
479+
function () { return directionsRendererSpy; });
474480
const testingWindow: TestingWindow = window;
475481
if (testingWindow.google && testingWindow.google.maps) {
476482
testingWindow.google.maps['DirectionsRenderer'] = directionsRendererConstructorSpy;
@@ -493,8 +499,9 @@ export function createDirectionsServiceSpy(): jasmine.SpyObj<google.maps.Directi
493499
/** Creates a jasmine.Spy to watch for the constructor of the DirectionsService */
494500
export function createDirectionsServiceConstructorSpy(
495501
directionsServiceSpy: jasmine.SpyObj<google.maps.DirectionsService>): jasmine.Spy {
496-
const directionsServiceConstructorSpy =
497-
jasmine.createSpy('DirectionsService constructor', () => directionsServiceSpy);
502+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
503+
const directionsServiceConstructorSpy = jasmine.createSpy('DirectionsService constructor',
504+
function() { return directionsServiceSpy; });
498505
const testingWindow: TestingWindow = window;
499506
if (testingWindow.google && testingWindow.google.maps) {
500507
testingWindow.google.maps['DirectionsService'] = directionsServiceConstructorSpy;
@@ -522,7 +529,8 @@ export function createHeatmapLayerSpy(): jasmine.SpyObj<google.maps.visualizatio
522529
*/
523530
export function createHeatmapLayerConstructorSpy(
524531
heatmapLayerSpy: jasmine.SpyObj<google.maps.visualization.HeatmapLayer>): jasmine.Spy {
525-
const heatmapLayerConstructorSpy = jasmine.createSpy('HeatmapLayer constructor', () => {
532+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
533+
const heatmapLayerConstructorSpy = jasmine.createSpy('HeatmapLayer constructor', function() {
526534
return heatmapLayerSpy;
527535
});
528536
const testingWindow: TestingWindow = window;
@@ -552,7 +560,9 @@ export function createLatLngSpy(): jasmine.SpyObj<google.maps.LatLng> {
552560
/** Creates a jasmine.Spy to watch for the constructor of a google.maps.LatLng */
553561
export function createLatLngConstructorSpy(
554562
latLngSpy: jasmine.SpyObj<google.maps.LatLng>): jasmine.Spy {
555-
const latLngConstructorSpy = jasmine.createSpy('LatLng constructor', () => latLngSpy);
563+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
564+
const latLngConstructorSpy = jasmine.createSpy('LatLng constructor',
565+
function() { return latLngSpy; });
556566
const testingWindow: TestingWindow = window;
557567
if (testingWindow.google && testingWindow.google.maps) {
558568
testingWindow.google.maps['LatLng'] = latLngConstructorSpy;
@@ -574,7 +584,9 @@ export function createGeocoderSpy(): jasmine.SpyObj<google.maps.Geocoder> {
574584
/** Creates a jasmine.Spy to watch for the constructor of the Geocoder. */
575585
export function createGeocoderConstructorSpy(
576586
geocoderSpy: jasmine.SpyObj<google.maps.Geocoder>): jasmine.Spy {
577-
const geocoderConstructorSpy = jasmine.createSpy('Geocoder constructor', () => geocoderSpy);
587+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
588+
const geocoderConstructorSpy = jasmine.createSpy('Geocoder constructor',
589+
function () { return geocoderSpy; });
578590
const testingWindow: TestingWindow = window;
579591
if (testingWindow.google && testingWindow.google.maps) {
580592
testingWindow.google.maps['Geocoder'] = geocoderConstructorSpy;

src/material-experimental/mdc-dialog/testing/dialog-harness.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import {runHarnessTests} from '@angular/material/dialog/testing/shared.spec';
33
import {MatDialogHarness} from './dialog-harness';
44

55
describe('MDC-based MatDialog', () => {
6-
runHarnessTests(MatDialogModule, MatDialogHarness, MatDialog as any);
6+
runHarnessTests(MatDialogModule, MatDialogHarness as any, MatDialog as any);
77
});

src/material-experimental/mdc-dialog/testing/dialog-harness.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {HarnessPredicate} from '@angular/cdk/testing';
1010
import {
1111
DialogHarnessFilters,
12-
MatDialogHarness as NonMdcDialogHarness
12+
_MatDialogHarnessBase,
1313
} from '@angular/material/dialog/testing';
1414

1515
/** Selectors for different sections of the mat-dialog that can contain user content. */
@@ -20,17 +20,17 @@ export const enum MatDialogSection {
2020
}
2121

2222
/** Harness for interacting with a standard `MatDialog` in tests. */
23-
export class MatDialogHarness extends NonMdcDialogHarness {
23+
export class MatDialogHarness extends _MatDialogHarnessBase {
2424
/** The selector for the host element of a `MatDialog` instance. */
25-
static override hostSelector = '.mat-mdc-dialog-container';
25+
static hostSelector = '.mat-mdc-dialog-container';
2626

2727
/**
2828
* Gets a `HarnessPredicate` that can be used to search for a `MatDialogHarness` that meets
2929
* certain criteria.
3030
* @param options Options for filtering which dialog instances are considered a match.
3131
* @return a `HarnessPredicate` configured with the given options.
3232
*/
33-
static override with(options: DialogHarnessFilters = {}): HarnessPredicate<MatDialogHarness> {
33+
static with(options: DialogHarnessFilters = {}): HarnessPredicate<MatDialogHarness> {
3434
return new HarnessPredicate(MatDialogHarness, options);
3535
}
3636

src/material/dialog/testing/dialog-harness.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,15 @@ export const enum MatDialogSection {
1717
ACTIONS = '.mat-dialog-actions'
1818
}
1919

20-
// @breaking-change 14.0.0 change generic type to MatDialogSection.
21-
/** Harness for interacting with a standard `MatDialog` in tests. */
22-
export class MatDialogHarness extends ContentContainerComponentHarness<MatDialogSection | string> {
23-
// Developers can provide a custom component or template for the
24-
// dialog. The canonical dialog parent is the "MatDialogContainer".
25-
/** The selector for the host element of a `MatDialog` instance. */
26-
static hostSelector = '.mat-dialog-container';
20+
/** Base class for the `MatDialogHarness` implementation. */
21+
export class _MatDialogHarnessBase extends
22+
// @breaking-change 14.0.0 change generic type to MatDialogSection.
23+
ContentContainerComponentHarness<MatDialogSection | string> {
2724

2825
protected _title = this.locatorForOptional(MatDialogSection.TITLE);
2926
protected _content = this.locatorForOptional(MatDialogSection.CONTENT);
3027
protected _actions = this.locatorForOptional(MatDialogSection.ACTIONS);
3128

32-
33-
/**
34-
* Gets a `HarnessPredicate` that can be used to search for a `MatDialogHarness` that meets
35-
* certain criteria.
36-
* @param options Options for filtering which dialog instances are considered a match.
37-
* @return a `HarnessPredicate` configured with the given options.
38-
*/
39-
static with(options: DialogHarnessFilters = {}): HarnessPredicate<MatDialogHarness> {
40-
return new HarnessPredicate(MatDialogHarness, options);
41-
}
42-
4329
/** Gets the id of the dialog. */
4430
async getId(): Promise<string|null> {
4531
const id = await (await this.host()).getAttribute('id');
@@ -97,3 +83,21 @@ export class MatDialogHarness extends ContentContainerComponentHarness<MatDialog
9783
return (await this._actions())?.text() ?? '';
9884
}
9985
}
86+
87+
/** Harness for interacting with a standard `MatDialog` in tests. */
88+
export class MatDialogHarness extends _MatDialogHarnessBase {
89+
// Developers can provide a custom component or template for the
90+
// dialog. The canonical dialog parent is the "MatDialogContainer".
91+
/** The selector for the host element of a `MatDialog` instance. */
92+
static hostSelector = '.mat-dialog-container';
93+
94+
/**
95+
* Gets a `HarnessPredicate` that can be used to search for a `MatDialogHarness` that meets
96+
* certain criteria.
97+
* @param options Options for filtering which dialog instances are considered a match.
98+
* @return a `HarnessPredicate` configured with the given options.
99+
*/
100+
static with(options: DialogHarnessFilters = {}): HarnessPredicate<MatDialogHarness> {
101+
return new HarnessPredicate(MatDialogHarness, options);
102+
}
103+
}

src/youtube-player/fake-youtube-player.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ export function createFakeYtNamespace(): FakeYtNamespace {
4444
const boundListeners = new Map<keyof YT.Events, Set<(event: any) => void>>();
4545
const playerCtorSpy = jasmine.createSpy('Player Constructor');
4646

47-
playerCtorSpy.and.callFake((_el: Element, config: YT.PlayerOptions) => {
47+
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
48+
playerCtorSpy.and.callFake(function (_el: Element, config: YT.PlayerOptions) {
4849
playerConfig = config;
4950
return playerSpy;
5051
});

0 commit comments

Comments
 (0)