Skip to content

Commit fbc814b

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 fbc814b

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("//tools:defaults.bzl", "ng_test_library", "ng_web_test_suite", "ts_library")
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/core",
16+
"@npm//@angular/common",
17+
"@npm//@angular/core",
18+
"@npm//rxjs",
19+
],
20+
)

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