|
5 | 5 | HttpTestingController,
|
6 | 6 | TestRequest,
|
7 | 7 | } from '@angular/common/http/testing';
|
8 |
| -import {Component, ErrorHandler, ViewChild} from '@angular/core'; |
9 |
| -import {MatIconModule, MAT_ICON_LOCATION} from './index'; |
| 8 | +import {Component, ErrorHandler, Provider, Type, ViewChild} from '@angular/core'; |
| 9 | +import {MAT_ICON_DEFAULT_OPTIONS, MAT_ICON_LOCATION, MatIconModule} from './index'; |
10 | 10 | import {MatIconRegistry, getMatIconNoHttpProviderError} from './icon-registry';
|
11 | 11 | import {FAKE_SVGS} from './fake-svgs';
|
12 | 12 | import {wrappedErrorMessage} from '../../cdk/testing/private';
|
@@ -41,6 +41,19 @@ function verifyPathChildElement(element: Element, attributeValue: string): void
|
41 | 41 | expect(pathElement.getAttribute('name')).toBe(attributeValue);
|
42 | 42 | }
|
43 | 43 |
|
| 44 | +/** Creates a test component fixture. */ |
| 45 | +function createComponent<T>(component: Type<T>, providers: Provider[] = []) { |
| 46 | + TestBed.configureTestingModule({ |
| 47 | + imports: [MatIconModule], |
| 48 | + declarations: [component], |
| 49 | + providers: [...providers], |
| 50 | + }); |
| 51 | + |
| 52 | + TestBed.compileComponents(); |
| 53 | + |
| 54 | + return TestBed.createComponent<T>(component); |
| 55 | +} |
| 56 | + |
44 | 57 | describe('MatIcon', () => {
|
45 | 58 | let fakePath: string;
|
46 | 59 | let errorHandler: jasmine.SpyObj<ErrorHandler>;
|
@@ -1237,6 +1250,71 @@ describe('MatIcon without HttpClientModule', () => {
|
1237 | 1250 | });
|
1238 | 1251 | });
|
1239 | 1252 |
|
| 1253 | +describe('MatIcon with default options', () => { |
| 1254 | + it('should be able to configure color globally', fakeAsync(() => { |
| 1255 | + const fixture = createComponent(IconWithLigature, [ |
| 1256 | + {provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {color: 'accent'}}, |
| 1257 | + ]); |
| 1258 | + const iconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1259 | + fixture.detectChanges(); |
| 1260 | + expect(iconElement.classList).not.toContain('mat-icon-no-color'); |
| 1261 | + expect(iconElement.classList).toContain('mat-accent'); |
| 1262 | + })); |
| 1263 | + |
| 1264 | + it('should use passed color rather then color provided', fakeAsync(() => { |
| 1265 | + const fixture = createComponent(IconWithColor, [ |
| 1266 | + {provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {color: 'warn'}}, |
| 1267 | + ]); |
| 1268 | + const iconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1269 | + fixture.detectChanges(); |
| 1270 | + expect(iconElement.classList).not.toContain('mat-warn'); |
| 1271 | + expect(iconElement.classList).toContain('mat-primary'); |
| 1272 | + })); |
| 1273 | + |
| 1274 | + it('should use default color if no color passed', fakeAsync(() => { |
| 1275 | + const fixture = createComponent(IconWithColor, [ |
| 1276 | + {provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {color: 'accent'}}, |
| 1277 | + ]); |
| 1278 | + const component = fixture.componentInstance; |
| 1279 | + const iconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1280 | + component.iconColor = ''; |
| 1281 | + fixture.detectChanges(); |
| 1282 | + expect(iconElement.classList).not.toContain('mat-icon-no-color'); |
| 1283 | + expect(iconElement.classList).not.toContain('mat-primary'); |
| 1284 | + expect(iconElement.classList).toContain('mat-accent'); |
| 1285 | + })); |
| 1286 | + |
| 1287 | + it('should be able to configure font set globally', fakeAsync(() => { |
| 1288 | + const fixture = createComponent(IconWithLigature, [ |
| 1289 | + {provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {fontSet: 'custom-font-set'}}, |
| 1290 | + ]); |
| 1291 | + const iconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1292 | + fixture.detectChanges(); |
| 1293 | + expect(iconElement.classList).toContain('custom-font-set'); |
| 1294 | + })); |
| 1295 | + |
| 1296 | + it('should use passed fontSet rather then default one', fakeAsync(() => { |
| 1297 | + const fixture = createComponent(IconWithCustomFontCss, [ |
| 1298 | + {provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {fontSet: 'default-font-set'}}, |
| 1299 | + ]); |
| 1300 | + const component = fixture.componentInstance; |
| 1301 | + const iconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1302 | + component.fontSet = 'custom-font-set'; |
| 1303 | + fixture.detectChanges(); |
| 1304 | + expect(iconElement.classList).not.toContain('default-font-set'); |
| 1305 | + expect(iconElement.classList).toContain('custom-font-set'); |
| 1306 | + })); |
| 1307 | + |
| 1308 | + it('should use passed empty fontSet rather then default one', fakeAsync(() => { |
| 1309 | + const fixture = createComponent(IconWithCustomFontCss, [ |
| 1310 | + {provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {fontSet: 'default-font-set'}}, |
| 1311 | + ]); |
| 1312 | + const iconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1313 | + fixture.detectChanges(); |
| 1314 | + expect(iconElement.classList).not.toContain('default-font-set'); |
| 1315 | + })); |
| 1316 | +}); |
| 1317 | + |
1240 | 1318 | @Component({template: `<mat-icon>{{iconName}}</mat-icon>`})
|
1241 | 1319 | class IconWithLigature {
|
1242 | 1320 | iconName = '';
|
|
0 commit comments