UNPKG

6.58 kBJavaScriptView Raw
1import { ComponentHarness, HarnessPredicate } from '@angular/cdk/testing';
2import * as i0 from '@angular/core';
3import { Injectable, NgModule } from '@angular/core';
4import { MatIconRegistry } from '@angular/material/icon';
5import { of } from 'rxjs';
6
7/**
8 * @license
9 * Copyright Google LLC All Rights Reserved.
10 *
11 * Use of this source code is governed by an MIT-style license that can be
12 * found in the LICENSE file at https://angular.io/license
13 */
14/** Harness for interacting with a standard mat-icon in tests. */
15class MatIconHarness extends ComponentHarness {
16 /**
17 * Gets a `HarnessPredicate` that can be used to search for a `MatIconHarness` that meets
18 * certain criteria.
19 * @param options Options for filtering which icon instances are considered a match.
20 * @return a `HarnessPredicate` configured with the given options.
21 */
22 static with(options = {}) {
23 return new HarnessPredicate(MatIconHarness, options)
24 .addOption('type', options.type, async (harness, type) => (await harness.getType()) === type)
25 .addOption('name', options.name, (harness, text) => HarnessPredicate.stringMatches(harness.getName(), text))
26 .addOption('namespace', options.namespace, (harness, text) => HarnessPredicate.stringMatches(harness.getNamespace(), text));
27 }
28 /** Gets the type of the icon. */
29 async getType() {
30 const type = await (await this.host()).getAttribute('data-mat-icon-type');
31 return type === 'svg' ? 0 /* IconType.SVG */ : 1 /* IconType.FONT */;
32 }
33 /** Gets the name of the icon. */
34 async getName() {
35 const host = await this.host();
36 const nameFromDom = await host.getAttribute('data-mat-icon-name');
37 // If we managed to figure out the name from the attribute, use it.
38 if (nameFromDom) {
39 return nameFromDom;
40 }
41 // Some icons support defining the icon as a ligature.
42 // As a fallback, try to extract it from the DOM text.
43 if ((await this.getType()) === 1 /* IconType.FONT */) {
44 return host.text();
45 }
46 return null;
47 }
48 /** Gets the namespace of the icon. */
49 async getNamespace() {
50 return (await this.host()).getAttribute('data-mat-icon-namespace');
51 }
52 /** Gets whether the icon is inline. */
53 async isInline() {
54 return (await this.host()).hasClass('mat-icon-inline');
55 }
56}
57/** The selector for the host element of a `MatIcon` instance. */
58MatIconHarness.hostSelector = '.mat-icon';
59
60/**
61 * @license
62 * Copyright Google LLC All Rights Reserved.
63 *
64 * Use of this source code is governed by an MIT-style license that can be
65 * found in the LICENSE file at https://angular.io/license
66 */
67
68/**
69 * @license
70 * Copyright Google LLC All Rights Reserved.
71 *
72 * Use of this source code is governed by an MIT-style license that can be
73 * found in the LICENSE file at https://angular.io/license
74 */
75/**
76 * A null icon registry that must be imported to allow disabling of custom
77 * icons.
78 */
79class FakeMatIconRegistry {
80 addSvgIcon() {
81 return this;
82 }
83 addSvgIconLiteral() {
84 return this;
85 }
86 addSvgIconInNamespace() {
87 return this;
88 }
89 addSvgIconLiteralInNamespace() {
90 return this;
91 }
92 addSvgIconSet() {
93 return this;
94 }
95 addSvgIconSetLiteral() {
96 return this;
97 }
98 addSvgIconSetInNamespace() {
99 return this;
100 }
101 addSvgIconSetLiteralInNamespace() {
102 return this;
103 }
104 registerFontClassAlias() {
105 return this;
106 }
107 classNameForFontAlias(alias) {
108 return alias;
109 }
110 getDefaultFontSetClass() {
111 return ['material-icons'];
112 }
113 getSvgIconFromUrl() {
114 return of(this._generateEmptySvg());
115 }
116 getNamedSvgIcon() {
117 return of(this._generateEmptySvg());
118 }
119 setDefaultFontSetClass() {
120 return this;
121 }
122 addSvgIconResolver() {
123 return this;
124 }
125 ngOnDestroy() { }
126 _generateEmptySvg() {
127 const emptySvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
128 emptySvg.classList.add('fake-testing-svg');
129 // Emulate real icon characteristics from `MatIconRegistry` so size remains consistent in tests.
130 emptySvg.setAttribute('fit', '');
131 emptySvg.setAttribute('height', '100%');
132 emptySvg.setAttribute('width', '100%');
133 emptySvg.setAttribute('preserveAspectRatio', 'xMidYMid meet');
134 emptySvg.setAttribute('focusable', 'false');
135 return emptySvg;
136 }
137}
138FakeMatIconRegistry.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.1", ngImport: i0, type: FakeMatIconRegistry, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
139FakeMatIconRegistry.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.0.1", ngImport: i0, type: FakeMatIconRegistry });
140i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.1", ngImport: i0, type: FakeMatIconRegistry, decorators: [{
141 type: Injectable
142 }] });
143/** Import this module in tests to install the null icon registry. */
144class MatIconTestingModule {
145}
146MatIconTestingModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.1", ngImport: i0, type: MatIconTestingModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
147MatIconTestingModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.0.1", ngImport: i0, type: MatIconTestingModule });
148MatIconTestingModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.0.1", ngImport: i0, type: MatIconTestingModule, providers: [{ provide: MatIconRegistry, useClass: FakeMatIconRegistry }] });
149i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.1", ngImport: i0, type: MatIconTestingModule, decorators: [{
150 type: NgModule,
151 args: [{
152 providers: [{ provide: MatIconRegistry, useClass: FakeMatIconRegistry }],
153 }]
154 }] });
155
156/**
157 * @license
158 * Copyright Google LLC All Rights Reserved.
159 *
160 * Use of this source code is governed by an MIT-style license that can be
161 * found in the LICENSE file at https://angular.io/license
162 */
163
164/**
165 * @license
166 * Copyright Google LLC All Rights Reserved.
167 *
168 * Use of this source code is governed by an MIT-style license that can be
169 * found in the LICENSE file at https://angular.io/license
170 */
171
172/**
173 * Generated bundle index. Do not edit.
174 */
175
176export { FakeMatIconRegistry, MatIconHarness, MatIconTestingModule };
177//# sourceMappingURL=testing.mjs.map