|
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,148 @@ describe('MatIcon without HttpClientModule', () => {
|
1237 | 1250 | });
|
1238 | 1251 | });
|
1239 | 1252 |
|
| 1253 | +describe('MatIcon with default options', () => { |
| 1254 | + it('should be able to configure default color globally', fakeAsync(() => { |
| 1255 | + const fixture = createComponent(IconWithLigature, [ |
| 1256 | + {provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {defaultColor: 'accent'}}, |
| 1257 | + ]); |
| 1258 | + const component = fixture.componentInstance; |
| 1259 | + const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1260 | + |
| 1261 | + component.iconName = 'home'; |
| 1262 | + fixture.detectChanges(); |
| 1263 | + expect(sortedClassNames(matIconElement)).toEqual([ |
| 1264 | + 'mat-accent', |
| 1265 | + 'mat-icon', |
| 1266 | + 'material-icons', |
| 1267 | + 'notranslate', |
| 1268 | + ]); |
| 1269 | + })); |
| 1270 | + |
| 1271 | + it('should be able to configure color globally', fakeAsync(() => { |
| 1272 | + const fixture = createComponent(IconWithLigature, [ |
| 1273 | + {provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {color: 'warn'}}, |
| 1274 | + ]); |
| 1275 | + const component = fixture.componentInstance; |
| 1276 | + const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1277 | + |
| 1278 | + component.iconName = 'home'; |
| 1279 | + fixture.detectChanges(); |
| 1280 | + expect(sortedClassNames(matIconElement)).toEqual([ |
| 1281 | + 'mat-icon', |
| 1282 | + 'mat-warn', |
| 1283 | + 'material-icons', |
| 1284 | + 'notranslate', |
| 1285 | + ]); |
| 1286 | + })); |
| 1287 | + |
| 1288 | + it('should be able to configure default color and color globally', fakeAsync(() => { |
| 1289 | + const fixture = createComponent(IconWithLigature, [ |
| 1290 | + {provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {defaultColor: 'accent', color: 'warn'}}, |
| 1291 | + ]); |
| 1292 | + const component = fixture.componentInstance; |
| 1293 | + const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1294 | + |
| 1295 | + component.iconName = 'home'; |
| 1296 | + fixture.detectChanges(); |
| 1297 | + expect(sortedClassNames(matIconElement)).toEqual([ |
| 1298 | + 'mat-icon', |
| 1299 | + 'mat-warn', |
| 1300 | + 'material-icons', |
| 1301 | + 'notranslate', |
| 1302 | + ]); |
| 1303 | + })); |
| 1304 | + |
| 1305 | + it('should use passed color rather then default color or color provided', fakeAsync(() => { |
| 1306 | + const fixture = createComponent(IconWithColor, [ |
| 1307 | + {provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {defaultColor: 'accent', color: 'warn'}}, |
| 1308 | + ]); |
| 1309 | + const component = fixture.componentInstance; |
| 1310 | + const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1311 | + |
| 1312 | + component.iconName = 'home'; |
| 1313 | + fixture.detectChanges(); |
| 1314 | + expect(sortedClassNames(matIconElement)).toEqual([ |
| 1315 | + 'mat-icon', |
| 1316 | + 'mat-primary', |
| 1317 | + 'material-icons', |
| 1318 | + 'notranslate', |
| 1319 | + ]); |
| 1320 | + })); |
| 1321 | + |
| 1322 | + it('should use default color if no passed color', fakeAsync(() => { |
| 1323 | + const fixture = createComponent(IconWithColor, [ |
| 1324 | + {provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {defaultColor: 'accent', color: 'warn'}}, |
| 1325 | + ]); |
| 1326 | + const component = fixture.componentInstance; |
| 1327 | + const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1328 | + |
| 1329 | + component.iconName = 'home'; |
| 1330 | + component.iconColor = ''; |
| 1331 | + fixture.detectChanges(); |
| 1332 | + expect(sortedClassNames(matIconElement)).toEqual([ |
| 1333 | + 'mat-accent', |
| 1334 | + 'mat-icon', |
| 1335 | + 'material-icons', |
| 1336 | + 'notranslate', |
| 1337 | + ]); |
| 1338 | + })); |
| 1339 | + |
| 1340 | + it('should be able to configure font set globally', fakeAsync(() => { |
| 1341 | + const fixture = createComponent(IconWithLigature, [ |
| 1342 | + {provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {fontSet: 'custom-font-set'}}, |
| 1343 | + ]); |
| 1344 | + const component = fixture.componentInstance; |
| 1345 | + const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1346 | + |
| 1347 | + component.iconName = 'home'; |
| 1348 | + fixture.detectChanges(); |
| 1349 | + expect(sortedClassNames(matIconElement)).toEqual([ |
| 1350 | + 'custom-font-set', |
| 1351 | + 'mat-icon', |
| 1352 | + 'mat-icon-no-color', |
| 1353 | + 'notranslate', |
| 1354 | + ]); |
| 1355 | + })); |
| 1356 | + |
| 1357 | + it('should use passed fontSet rather then default one', fakeAsync(() => { |
| 1358 | + const fixture = createComponent(IconWithCustomFontCss, [ |
| 1359 | + {provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {fontSet: 'default-font-set'}}, |
| 1360 | + ]); |
| 1361 | + const component = fixture.componentInstance; |
| 1362 | + const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1363 | + |
| 1364 | + component.fontSet = 'custom-font-set'; |
| 1365 | + component.fontIcon = 'house'; |
| 1366 | + fixture.detectChanges(); |
| 1367 | + expect(sortedClassNames(matIconElement)).toEqual([ |
| 1368 | + 'custom-font-set', |
| 1369 | + 'house', |
| 1370 | + 'mat-icon', |
| 1371 | + 'mat-icon-no-color', |
| 1372 | + 'notranslate', |
| 1373 | + ]); |
| 1374 | + })); |
| 1375 | + |
| 1376 | + it('should use passed empty fontSet rather then default one', fakeAsync(() => { |
| 1377 | + const fixture = createComponent(IconWithCustomFontCss, [ |
| 1378 | + {provide: MAT_ICON_DEFAULT_OPTIONS, useValue: {fontSet: 'default-font-set'}}, |
| 1379 | + ]); |
| 1380 | + const component = fixture.componentInstance; |
| 1381 | + const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1382 | + |
| 1383 | + component.fontIcon = 'house'; |
| 1384 | + fixture.detectChanges(); |
| 1385 | + expect(sortedClassNames(matIconElement)).toEqual([ |
| 1386 | + 'house', |
| 1387 | + 'mat-icon', |
| 1388 | + 'mat-icon-no-color', |
| 1389 | + 'material-icons', |
| 1390 | + 'notranslate', |
| 1391 | + ]); |
| 1392 | + })); |
| 1393 | +}); |
| 1394 | + |
1240 | 1395 | @Component({template: `<mat-icon>{{iconName}}</mat-icon>`})
|
1241 | 1396 | class IconWithLigature {
|
1242 | 1397 | iconName = '';
|
|
0 commit comments