diff --git a/src/material/config.bzl b/src/material/config.bzl index a7da1b99c604..ed921eab309c 100644 --- a/src/material/config.bzl +++ b/src/material/config.bzl @@ -26,6 +26,7 @@ entryPoints = [ "grid-list", "grid-list/testing", "icon", + "icon/testing", "input", "list", "list/testing", diff --git a/src/material/icon/testing/BUILD.bazel b/src/material/icon/testing/BUILD.bazel new file mode 100644 index 000000000000..71a160e024eb --- /dev/null +++ b/src/material/icon/testing/BUILD.bazel @@ -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"]), +) diff --git a/src/material/icon/testing/fake-icon-registry.ts b/src/material/icon/testing/fake-icon-registry.ts new file mode 100644 index 000000000000..df7bb09c4208 --- /dev/null +++ b/src/material/icon/testing/fake-icon-registry.ts @@ -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 = { + [K in keyof T]: T[K] extends (...x: any[]) => T ? (...x: any[]) => PublicApi : 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, 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 { + return observableOf(this._generateEmptySvg()); + } + + getNamedSvgIcon(): Observable { + 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 { +} diff --git a/src/material/icon/testing/index.ts b/src/material/icon/testing/index.ts new file mode 100644 index 000000000000..676ca90f1ffa --- /dev/null +++ b/src/material/icon/testing/index.ts @@ -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'; diff --git a/src/material/icon/testing/public-api.ts b/src/material/icon/testing/public-api.ts new file mode 100644 index 000000000000..16d338442041 --- /dev/null +++ b/src/material/icon/testing/public-api.ts @@ -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'; diff --git a/tools/public_api_guard/material/icon/testing.d.ts b/tools/public_api_guard/material/icon/testing.d.ts new file mode 100644 index 000000000000..00a0431b4316 --- /dev/null +++ b/tools/public_api_guard/material/icon/testing.d.ts @@ -0,0 +1,24 @@ +export declare class FakeMatIconRegistry implements PublicApi, 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; + getSvgIconFromUrl(): Observable; + ngOnDestroy(): void; + registerFontClassAlias(): this; + setDefaultFontSetClass(): this; + static ɵfac: i0.ɵɵFactoryDef; + static ɵprov: i0.ɵɵInjectableDef; +} + +export declare class MatIconTestingModule { + static ɵinj: i0.ɵɵInjectorDef; + static ɵmod: i0.ɵɵNgModuleDefWithMeta; +}