Skip to content

Commit 01cc137

Browse files
authored
refactor(material/icon): switch tests away from fakeAsync (#33301)
Reworks the icon tests not to depend on `fakeAsync` anymore.
1 parent 11e2eb3 commit 01cc137

1 file changed

Lines changed: 43 additions & 58 deletions

File tree

‎src/material/icon/icon.spec.ts‎

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
ViewChild,
1212
ChangeDetectionStrategy,
1313
} from '@angular/core';
14-
import {TestBed, fakeAsync, tick, waitForAsync} from '@angular/core/testing';
14+
import {TestBed} from '@angular/core/testing';
1515
import {DomSanitizer, SafeHtml, SafeResourceUrl} from '@angular/platform-browser';
1616
import {FAKE_SVGS} from './fake-svgs';
1717
import {MatIcon} from './icon';
@@ -64,7 +64,7 @@ describe('MatIcon', () => {
6464
let sanitizer: DomSanitizer;
6565
let errorHandler: ErrorHandler;
6666

67-
beforeEach(waitForAsync(() => {
67+
beforeEach(() => {
6868
// The $ prefix tells Karma not to try to process the
6969
// request so that we don't get warnings in our logs.
7070
fakePath = '/$fake-path';
@@ -83,7 +83,7 @@ describe('MatIcon', () => {
8383
http = TestBed.inject(HttpTestingController);
8484
sanitizer = TestBed.inject(DomSanitizer);
8585
errorHandler = TestBed.inject(ErrorHandler);
86-
}));
86+
});
8787

8888
it('should apply the correct role', () => {
8989
const fixture = TestBed.createComponent(IconWithColor);
@@ -315,7 +315,7 @@ describe('MatIcon', () => {
315315
});
316316

317317
describe('Icons from URLs', () => {
318-
it('should register icon URLs by name', fakeAsync(() => {
318+
it('should register icon URLs by name', () => {
319319
iconRegistry.addSvgIcon('fluffy', trustUrl('cat.svg'));
320320
iconRegistry.addSvgIcon('fido', trustUrl('dog.svg'));
321321
iconRegistry.addSvgIcon('felix', trustUrl('auth-cat.svg'), {withCredentials: true});
@@ -363,11 +363,9 @@ describe('MatIcon', () => {
363363
iconRegistry.getSvgIconFromUrl(trustUrl('cat.svg')).subscribe((element: SVGElement) => {
364364
verifyPathChildElement(element, 'meow');
365365
});
366+
});
366367

367-
tick();
368-
}));
369-
370-
it('should be able to set the viewBox when registering a single SVG icon', fakeAsync(() => {
368+
it('should be able to set the viewBox when registering a single SVG icon', () => {
371369
iconRegistry.addSvgIcon('fluffy', trustUrl('cat.svg'), {viewBox: '0 0 27 27'});
372370
iconRegistry.addSvgIcon('fido', trustUrl('dog.svg'), {viewBox: '0 0 43 43'});
373371

@@ -390,7 +388,7 @@ describe('MatIcon', () => {
390388
http.expectOne('cat.svg').flush(FAKE_SVGS.cat);
391389
svgElement = verifyAndGetSingleSvgChild(iconElement);
392390
expect(svgElement.getAttribute('viewBox')).toBe('0 0 27 27');
393-
}));
391+
});
394392

395393
it('should throw an error when using an untrusted icon url', () => {
396394
iconRegistry.addSvgIcon('fluffy', 'farm-set-1.svg');
@@ -745,7 +743,7 @@ describe('MatIcon', () => {
745743
expect(icon.querySelector('svg')).toBeFalsy();
746744
});
747745

748-
it('should keep non-SVG user content inside the icon element', fakeAsync(() => {
746+
it('should keep non-SVG user content inside the icon element', () => {
749747
iconRegistry.addSvgIcon('fido', trustUrl('dog.svg'));
750748

751749
const fixture = TestBed.createComponent(SvgIconWithUserContent);
@@ -761,11 +759,9 @@ describe('MatIcon', () => {
761759

762760
expect(userDiv).toBeTruthy();
763761
expect(iconElement.textContent.trim()).toContain('Hello');
762+
});
764763

765-
tick();
766-
}));
767-
768-
it('should cancel in-progress fetches if the icon changes', fakeAsync(() => {
764+
it('should cancel in-progress fetches if the icon changes', () => {
769765
// Register an icon that will resolve immediately.
770766
iconRegistry.addSvgIconLiteral('fluffy', trustHtml(FAKE_SVGS.cat));
771767

@@ -790,9 +786,9 @@ describe('MatIcon', () => {
790786

791787
// Expect the last icon to have been assigned.
792788
verifyPathChildElement(verifyAndGetSingleSvgChild(iconElement), 'meow');
793-
}));
789+
});
794790

795-
it('should cancel in-progress fetches if the component is destroyed', fakeAsync(() => {
791+
it('should cancel in-progress fetches if the component is destroyed', () => {
796792
iconRegistry.addSvgIcon('fido', trustUrl('dog.svg'));
797793

798794
const fixture = TestBed.createComponent(IconFromSvgName);
@@ -802,11 +798,11 @@ describe('MatIcon', () => {
802798
fixture.destroy();
803799

804800
expect(http.expectOne('dog.svg').cancelled).toBe(true);
805-
}));
801+
});
806802
});
807803

808804
describe('Icons from HTML string', () => {
809-
it('should register icon HTML strings by name', fakeAsync(() => {
805+
it('should register icon HTML strings by name', () => {
810806
iconRegistry.addSvgIconLiteral('fluffy', trustHtml(FAKE_SVGS.cat));
811807
iconRegistry.addSvgIconLiteral('fido', trustHtml(FAKE_SVGS.dog));
812808

@@ -831,11 +827,9 @@ describe('MatIcon', () => {
831827
iconRegistry.getNamedSvgIcon('fluffy').subscribe((element: SVGElement) => {
832828
verifyPathChildElement(element, 'meow');
833829
});
830+
});
834831

835-
tick();
836-
}));
837-
838-
it('should be able to configure the icon viewBox', fakeAsync(() => {
832+
it('should be able to configure the icon viewBox', () => {
839833
iconRegistry.addSvgIconLiteral('fluffy', trustHtml(FAKE_SVGS.cat), {viewBox: '0 0 43 43'});
840834
iconRegistry.addSvgIconLiteral('fido', trustHtml(FAKE_SVGS.dog), {viewBox: '0 0 27 27'});
841835

@@ -855,7 +849,7 @@ describe('MatIcon', () => {
855849
fixture.detectChanges();
856850
svgElement = verifyAndGetSingleSvgChild(iconElement);
857851
expect(svgElement.getAttribute('viewBox')).toBe('0 0 43 43');
858-
}));
852+
});
859853

860854
it('should throw an error when using untrusted HTML', () => {
861855
// Stub out console.warn so we don't pollute our logs with Angular's warnings.
@@ -993,7 +987,7 @@ describe('MatIcon', () => {
993987
expect(svgElement.getAttribute('viewBox')).toBe('0 0 43 43');
994988
});
995989

996-
it('should prepend the current path to attributes with `url()` references', fakeAsync(() => {
990+
it('should prepend the current path to attributes with `url()` references', () => {
997991
iconRegistry.addSvgIconLiteral(
998992
'fido',
999993
trustHtml(`
@@ -1016,11 +1010,9 @@ describe('MatIcon', () => {
10161010
// We use a regex to match here, rather than the exact value, because different browsers
10171011
// return different quotes through `getAttribute`, while some even omit the quotes altogether.
10181012
expect(circle.getAttribute('filter')).toMatch(/^url\(['"]?\/\$fake-path#blur['"]?\)$/);
1013+
});
10191014

1020-
tick();
1021-
}));
1022-
1023-
it('should use latest path when prefixing the `url()` references', fakeAsync(() => {
1015+
it('should use latest path when prefixing the `url()` references', () => {
10241016
iconRegistry.addSvgIconLiteral(
10251017
'fido',
10261018
trustHtml(`
@@ -1041,7 +1033,6 @@ describe('MatIcon', () => {
10411033
let circle = fixture.nativeElement.querySelector('mat-icon svg circle');
10421034

10431035
expect(circle.getAttribute('filter')).toMatch(/^url\(['"]?\/\$fake-path#blur['"]?\)$/);
1044-
tick();
10451036
fixture.destroy();
10461037

10471038
fakePath = '/$another-fake-path';
@@ -1054,10 +1045,9 @@ describe('MatIcon', () => {
10541045
expect(circle.getAttribute('filter')).toMatch(
10551046
/^url\(['"]?\/\$another-fake-path#blur['"]?\)$/,
10561047
);
1057-
tick();
1058-
}));
1048+
});
10591049

1060-
it('should update the `url()` references when the path changes', fakeAsync(() => {
1050+
it('should update the `url()` references when the path changes', () => {
10611051
iconRegistry.addSvgIconLiteral(
10621052
'fido',
10631053
trustHtml(`
@@ -1080,14 +1070,13 @@ describe('MatIcon', () => {
10801070
// We use a regex to match here, rather than the exact value, because different browsers
10811071
// return different quotes through `getAttribute`, while some even omit the quotes altogether.
10821072
expect(circle.getAttribute('filter')).toMatch(/^url\(['"]?\/\$fake-path#blur['"]?\)$/);
1083-
tick();
10841073

10851074
fakePath = '/$different-path';
10861075
fixture.changeDetectorRef.markForCheck();
10871076
fixture.detectChanges();
10881077

10891078
expect(circle.getAttribute('filter')).toMatch(/^url\(['"]?\/\$different-path#blur['"]?\)$/);
1090-
}));
1079+
});
10911080
});
10921081

10931082
describe('custom fonts', () => {
@@ -1204,7 +1193,7 @@ describe('MatIcon', () => {
12041193
});
12051194

12061195
describe('Icons resolved through a resolver function', () => {
1207-
it('should resolve icons through a resolver function', fakeAsync(() => {
1196+
it('should resolve icons through a resolver function', () => {
12081197
iconRegistry.addSvgIconResolver(name => {
12091198
if (name === 'fluffy') {
12101199
return trustUrl('cat.svg');
@@ -1259,11 +1248,9 @@ describe('MatIcon', () => {
12591248
iconRegistry.getSvgIconFromUrl(trustUrl('cat.svg')).subscribe((element: SVGElement) => {
12601249
verifyPathChildElement(element, 'meow');
12611250
});
1251+
});
12621252

1263-
tick();
1264-
}));
1265-
1266-
it('should fall back to second resolver if the first one returned null', fakeAsync(() => {
1253+
it('should fall back to second resolver if the first one returned null', () => {
12671254
iconRegistry
12681255
.addSvgIconResolver(() => null)
12691256
.addSvgIconResolver(name => (name === 'fido' ? trustUrl('dog.svg') : null));
@@ -1276,10 +1263,9 @@ describe('MatIcon', () => {
12761263
fixture.detectChanges();
12771264
http.expectOne('dog.svg').flush(FAKE_SVGS.dog);
12781265
verifyPathChildElement(verifyAndGetSingleSvgChild(iconElement), 'woof');
1279-
tick();
1280-
}));
1266+
});
12811267

1282-
it('should be able to set the viewBox when resolving an icon with a function', fakeAsync(() => {
1268+
it('should be able to set the viewBox when resolving an icon with a function', () => {
12831269
iconRegistry.addSvgIconResolver(name => {
12841270
if (name === 'fluffy') {
12851271
return {url: trustUrl('cat.svg'), options: {viewBox: '0 0 27 27'}};
@@ -1308,7 +1294,7 @@ describe('MatIcon', () => {
13081294
http.expectOne('cat.svg').flush(FAKE_SVGS.cat);
13091295
svgElement = verifyAndGetSingleSvgChild(iconElement);
13101296
expect(svgElement.getAttribute('viewBox')).toBe('0 0 27 27');
1311-
}));
1297+
});
13121298

13131299
it('should throw an error when the resolver returns an untrusted URL', () => {
13141300
iconRegistry.addSvgIconResolver(() => 'not-trusted.svg');
@@ -1322,7 +1308,7 @@ describe('MatIcon', () => {
13221308
});
13231309
});
13241310

1325-
it('should handle assigning an icon through the setter', fakeAsync(() => {
1311+
it('should handle assigning an icon through the setter', () => {
13261312
iconRegistry.addSvgIconLiteral('fido', trustHtml(FAKE_SVGS.dog));
13271313

13281314
const fixture = TestBed.createComponent(BlankIcon);
@@ -1336,8 +1322,7 @@ describe('MatIcon', () => {
13361322
fixture.detectChanges();
13371323
svgElement = verifyAndGetSingleSvgChild(iconElement);
13381324
verifyPathChildElement(svgElement, 'woof');
1339-
tick();
1340-
}));
1325+
});
13411326

13421327
/** Marks an SVG icon url as explicitly trusted. */
13431328
function trustUrl(iconUrl: string): SafeResourceUrl {
@@ -1351,27 +1336,27 @@ describe('MatIcon', () => {
13511336
});
13521337

13531338
describe('MatIcon with default options', () => {
1354-
it('should be able to configure color globally', fakeAsync(() => {
1339+
it('should be able to configure color globally', () => {
13551340
const fixture = createComponent(IconWithLigature, [
13561341
{provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {color: 'accent'}},
13571342
]);
13581343
const iconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
13591344
fixture.detectChanges();
13601345
expect(iconElement.classList).not.toContain('mat-icon-no-color');
13611346
expect(iconElement.classList).toContain('mat-accent');
1362-
}));
1347+
});
13631348

1364-
it('should use passed color rather then color provided', fakeAsync(() => {
1349+
it('should use passed color rather then color provided', () => {
13651350
const fixture = createComponent(IconWithColor, [
13661351
{provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {color: 'warn'}},
13671352
]);
13681353
const iconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
13691354
fixture.detectChanges();
13701355
expect(iconElement.classList).not.toContain('mat-warn');
13711356
expect(iconElement.classList).toContain('mat-primary');
1372-
}));
1357+
});
13731358

1374-
it('should use default color if no color passed', fakeAsync(() => {
1359+
it('should use default color if no color passed', () => {
13751360
const fixture = createComponent(IconWithColor, [
13761361
{provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {color: 'accent'}},
13771362
]);
@@ -1382,18 +1367,18 @@ describe('MatIcon with default options', () => {
13821367
expect(iconElement.classList).not.toContain('mat-icon-no-color');
13831368
expect(iconElement.classList).not.toContain('mat-primary');
13841369
expect(iconElement.classList).toContain('mat-accent');
1385-
}));
1370+
});
13861371

1387-
it('should be able to configure font set globally', fakeAsync(() => {
1372+
it('should be able to configure font set globally', () => {
13881373
const fixture = createComponent(IconWithLigature, [
13891374
{provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {fontSet: 'custom-font-set'}},
13901375
]);
13911376
const iconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
13921377
fixture.detectChanges();
13931378
expect(iconElement.classList).toContain('custom-font-set');
1394-
}));
1379+
});
13951380

1396-
it('should use passed fontSet rather then default one', fakeAsync(() => {
1381+
it('should use passed fontSet rather then default one', () => {
13971382
const fixture = createComponent(IconWithCustomFontCss, [
13981383
{provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {fontSet: 'default-font-set'}},
13991384
]);
@@ -1403,16 +1388,16 @@ describe('MatIcon with default options', () => {
14031388
fixture.detectChanges();
14041389
expect(iconElement.classList).not.toContain('default-font-set');
14051390
expect(iconElement.classList).toContain('custom-font-set');
1406-
}));
1391+
});
14071392

1408-
it('should use passed empty fontSet rather then default one', fakeAsync(() => {
1393+
it('should use passed empty fontSet rather then default one', () => {
14091394
const fixture = createComponent(IconWithCustomFontCss, [
14101395
{provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {fontSet: 'default-font-set'}},
14111396
]);
14121397
const iconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
14131398
fixture.detectChanges();
14141399
expect(iconElement.classList).not.toContain('default-font-set');
1415-
}));
1400+
});
14161401
});
14171402

14181403
@Component({

0 commit comments

Comments
 (0)