Skip to content

feat(material/icon): New NoopIconModule for silencing unit test errors #18151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/material/config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ entryPoints = [
"grid-list",
"grid-list/testing",
"icon",
"icon/testing",
"input",
"list",
"list/testing",
Expand Down
25 changes: 25 additions & 0 deletions src/material/icon/testing/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package(default_visibility = ["//visibility:public"])

load("//tools:defaults.bzl", "ng_module")

ng_module(
name = "testing",
srcs = [
"fake-icon-registry.ts",
"index.ts",
"public-api.ts",
],
module_name = "@angular/material/icon/testing",
deps = [
"//src/cdk/coercion",
"//src/material/icon",
"@npm//@angular/common",
"@npm//@angular/core",
"@npm//rxjs",
],
)

filegroup(
name = "source-files",
srcs = glob(["**/*.ts"]),
)
95 changes: 95 additions & 0 deletions src/material/icon/testing/fake-icon-registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {Injectable, NgModule, OnDestroy} from '@angular/core';
import {MatIconRegistry} from '@angular/material/icon';
import {Observable, of as observableOf} from 'rxjs';

// tslint:disable:no-any Impossible to tell param types.
type PublicApi<T> = {
[K in keyof T]: T[K] extends (...x: any[]) => T ? (...x: any[]) => PublicApi<T> : T[K]
};
// tslint:enable:no-any

/**
* A null icon registry that must be imported to allow disabling of custom
* icons.
*/
@Injectable()
export class FakeMatIconRegistry implements PublicApi<MatIconRegistry>, OnDestroy {
addSvgIcon(): this {
return this;
}

addSvgIconLiteral(): this {
return this;
}

addSvgIconInNamespace(): this {
return this;
}

addSvgIconLiteralInNamespace(): this {
return this;
}

addSvgIconSet(): this {
return this;
}

addSvgIconSetLiteral(): this {
return this;
}

addSvgIconSetInNamespace(): this {
return this;
}

addSvgIconSetLiteralInNamespace(): this {
return this;
}

registerFontClassAlias(): this {
return this;
}

classNameForFontAlias(alias: string): string {
return alias;
}

getDefaultFontSetClass() {
return 'material-icons';
}

getSvgIconFromUrl(): Observable<SVGElement> {
return observableOf(this._generateEmptySvg());
}

getNamedSvgIcon(): Observable<SVGElement> {
return observableOf(this._generateEmptySvg());
}

setDefaultFontSetClass(): this {
return this;
}

ngOnDestroy() { }

private _generateEmptySvg(): SVGElement {
const emptySvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
emptySvg.classList.add('fake-testing-svg');
return emptySvg;
}
}

/** Import this module in tests to install the null icon registry. */
@NgModule({
providers: [{provide: MatIconRegistry, useClass: FakeMatIconRegistry}]
})
export class MatIconTestingModule {
}
9 changes: 9 additions & 0 deletions src/material/icon/testing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

export * from './public-api';
9 changes: 9 additions & 0 deletions src/material/icon/testing/public-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

export * from './fake-icon-registry';
24 changes: 24 additions & 0 deletions tools/public_api_guard/material/icon/testing.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export declare class FakeMatIconRegistry implements PublicApi<MatIconRegistry>, OnDestroy {
addSvgIcon(): this;
addSvgIconInNamespace(): this;
addSvgIconLiteral(): this;
addSvgIconLiteralInNamespace(): this;
addSvgIconSet(): this;
addSvgIconSetInNamespace(): this;
addSvgIconSetLiteral(): this;
addSvgIconSetLiteralInNamespace(): this;
classNameForFontAlias(alias: string): string;
getDefaultFontSetClass(): string;
getNamedSvgIcon(): Observable<SVGElement>;
getSvgIconFromUrl(): Observable<SVGElement>;
ngOnDestroy(): void;
registerFontClassAlias(): this;
setDefaultFontSetClass(): this;
static ɵfac: i0.ɵɵFactoryDef<FakeMatIconRegistry>;
static ɵprov: i0.ɵɵInjectableDef<FakeMatIconRegistry>;
}

export declare class MatIconTestingModule {
static ɵinj: i0.ɵɵInjectorDef<MatIconTestingModule>;
static ɵmod: i0.ɵɵNgModuleDefWithMeta<MatIconTestingModule, never, never, never>;
}