Skip to content

Commit 0e96402

Browse files
committed
feat(material/icon): New NoopIconModule for silencing unit test errors
Introducing the NoopIconModule to help silence Karma tests throwing warnings about custom icons not found. This added a lot of noise to tests output and it's now gone when `NoopMatIconModule` is installed in tests. The actual error this silences is `Error retrieving icon :xyz! Unable to find icon with the name ":xyz"`
1 parent dbdc1a8 commit 0e96402

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-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+
"index.ts",
9+
"noop-icon-registry.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+
)

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: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 {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+
export class NoopMatIconRegistry implements PublicApi<MatIconRegistry>, OnDestroy {
24+
addSvgIcon(): this {
25+
return this;
26+
}
27+
28+
addSvgIconLiteral(): this {
29+
return this;
30+
}
31+
32+
addSvgIconInNamespace(): this {
33+
return this;
34+
}
35+
36+
addSvgIconLiteralInNamespace(): this {
37+
return this;
38+
}
39+
40+
addSvgIconSet(): this {
41+
return this;
42+
}
43+
44+
addSvgIconSetLiteral(): this {
45+
return this;
46+
}
47+
48+
addSvgIconSetInNamespace(): this {
49+
return this;
50+
}
51+
52+
addSvgIconSetLiteralInNamespace(): this {
53+
return this;
54+
}
55+
56+
registerFontClassAlias(): this {
57+
return this;
58+
}
59+
60+
classNameForFontAlias(alias: string): string {
61+
return alias;
62+
}
63+
64+
getDefaultFontSetClass() {
65+
return 'material-icons';
66+
}
67+
68+
getSvgIconFromUrl(): Observable<SVGElement> {
69+
return observableOf(this._generateEmptySvg());
70+
}
71+
72+
getNamedSvgIcon(): Observable<SVGElement> {
73+
return observableOf(this._generateEmptySvg());
74+
}
75+
76+
setDefaultFontSetClass(): this {
77+
return this;
78+
}
79+
80+
ngOnDestroy() { }
81+
82+
private _generateEmptySvg(): SVGElement {
83+
const emptySvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
84+
emptySvg.classList.add('fake-testing-svg');
85+
return emptySvg;
86+
}
87+
}
88+
89+
/** Import this module in tests to install the null icon registry. */
90+
@NgModule({
91+
providers: [{provide: MatIconRegistry, useClass: NoopMatIconRegistry}]
92+
})
93+
export class MatIconTestingModule {
94+
}
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 './noop-icon-registry';

0 commit comments

Comments
 (0)