Skip to content

Commit 71955d2

Browse files
osuritzjelbourn
authored andcommitted
feat(material/icon): New FakeMatIconRegitry for unit tests (#18151)
Adds `MatIconTestingMdoule` to eliminate warnings of the form `Error retrieving icon :xyz! Unable to find icon with the name ":xyz"`. This warning added noise to tests output. When `MaticonTestingModule` is installed in tests, the fake implementation of the icon registry will always return fake SVG icons.
1 parent 7cced9c commit 71955d2

File tree

6 files changed

+163
-0
lines changed

6 files changed

+163
-0
lines changed

src/material/config.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ entryPoints = [
2626
"grid-list",
2727
"grid-list/testing",
2828
"icon",
29+
"icon/testing",
2930
"input",
3031
"list",
3132
"list/testing",

src/material/icon/testing/BUILD.bazel

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("//tools:defaults.bzl", "ng_module")
4+
5+
ng_module(
6+
name = "testing",
7+
srcs = [
8+
"fake-icon-registry.ts",
9+
"index.ts",
10+
"public-api.ts",
11+
],
12+
module_name = "@angular/material/icon/testing",
13+
deps = [
14+
"//src/cdk/coercion",
15+
"//src/material/icon",
16+
"@npm//@angular/common",
17+
"@npm//@angular/core",
18+
"@npm//rxjs",
19+
],
20+
)
21+
22+
filegroup(
23+
name = "source-files",
24+
srcs = glob(["**/*.ts"]),
25+
)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {Injectable, NgModule, OnDestroy} from '@angular/core';
10+
import {MatIconRegistry} from '@angular/material/icon';
11+
import {Observable, of as observableOf} from 'rxjs';
12+
13+
// tslint:disable:no-any Impossible to tell param types.
14+
type PublicApi<T> = {
15+
[K in keyof T]: T[K] extends (...x: any[]) => T ? (...x: any[]) => PublicApi<T> : T[K]
16+
};
17+
// tslint:enable:no-any
18+
19+
/**
20+
* A null icon registry that must be imported to allow disabling of custom
21+
* icons.
22+
*/
23+
@Injectable()
24+
export class FakeMatIconRegistry implements PublicApi<MatIconRegistry>, OnDestroy {
25+
addSvgIcon(): this {
26+
return this;
27+
}
28+
29+
addSvgIconLiteral(): this {
30+
return this;
31+
}
32+
33+
addSvgIconInNamespace(): this {
34+
return this;
35+
}
36+
37+
addSvgIconLiteralInNamespace(): this {
38+
return this;
39+
}
40+
41+
addSvgIconSet(): this {
42+
return this;
43+
}
44+
45+
addSvgIconSetLiteral(): this {
46+
return this;
47+
}
48+
49+
addSvgIconSetInNamespace(): this {
50+
return this;
51+
}
52+
53+
addSvgIconSetLiteralInNamespace(): this {
54+
return this;
55+
}
56+
57+
registerFontClassAlias(): this {
58+
return this;
59+
}
60+
61+
classNameForFontAlias(alias: string): string {
62+
return alias;
63+
}
64+
65+
getDefaultFontSetClass() {
66+
return 'material-icons';
67+
}
68+
69+
getSvgIconFromUrl(): Observable<SVGElement> {
70+
return observableOf(this._generateEmptySvg());
71+
}
72+
73+
getNamedSvgIcon(): Observable<SVGElement> {
74+
return observableOf(this._generateEmptySvg());
75+
}
76+
77+
setDefaultFontSetClass(): this {
78+
return this;
79+
}
80+
81+
ngOnDestroy() { }
82+
83+
private _generateEmptySvg(): SVGElement {
84+
const emptySvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
85+
emptySvg.classList.add('fake-testing-svg');
86+
return emptySvg;
87+
}
88+
}
89+
90+
/** Import this module in tests to install the null icon registry. */
91+
@NgModule({
92+
providers: [{provide: MatIconRegistry, useClass: FakeMatIconRegistry}]
93+
})
94+
export class MatIconTestingModule {
95+
}

src/material/icon/testing/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export * from './public-api';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export * from './fake-icon-registry';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export declare class FakeMatIconRegistry implements PublicApi<MatIconRegistry>, OnDestroy {
2+
addSvgIcon(): this;
3+
addSvgIconInNamespace(): this;
4+
addSvgIconLiteral(): this;
5+
addSvgIconLiteralInNamespace(): this;
6+
addSvgIconSet(): this;
7+
addSvgIconSetInNamespace(): this;
8+
addSvgIconSetLiteral(): this;
9+
addSvgIconSetLiteralInNamespace(): this;
10+
classNameForFontAlias(alias: string): string;
11+
getDefaultFontSetClass(): string;
12+
getNamedSvgIcon(): Observable<SVGElement>;
13+
getSvgIconFromUrl(): Observable<SVGElement>;
14+
ngOnDestroy(): void;
15+
registerFontClassAlias(): this;
16+
setDefaultFontSetClass(): this;
17+
static ɵfac: i0.ɵɵFactoryDef<FakeMatIconRegistry>;
18+
static ɵprov: i0.ɵɵInjectableDef<FakeMatIconRegistry>;
19+
}
20+
21+
export declare class MatIconTestingModule {
22+
static ɵinj: i0.ɵɵInjectorDef<MatIconTestingModule>;
23+
static ɵmod: i0.ɵɵNgModuleDefWithMeta<MatIconTestingModule, never, never, never>;
24+
}

0 commit comments

Comments
 (0)