|
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,21 @@ 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: [ |
| 50 | + ...providers |
| 51 | + ] |
| 52 | + }); |
| 53 | + |
| 54 | + TestBed.compileComponents(); |
| 55 | + |
| 56 | + return TestBed.createComponent<T>(component); |
| 57 | +} |
| 58 | + |
44 | 59 | describe('MatIcon', () => {
|
45 | 60 | let fakePath: string;
|
46 | 61 | let errorHandler: jasmine.SpyObj<ErrorHandler>;
|
@@ -1237,6 +1252,115 @@ describe('MatIcon without HttpClientModule', () => {
|
1237 | 1252 | });
|
1238 | 1253 | });
|
1239 | 1254 |
|
| 1255 | + |
| 1256 | +describe('MatIcon with default options', () => { |
| 1257 | + it('should be able to configure default color globally', fakeAsync(() => { |
| 1258 | + const fixture = createComponent(IconWithLigature, [ |
| 1259 | + { provide: MAT_ICON_DEFAULT_OPTIONS, useValue: { defaultColor: 'accent' } } |
| 1260 | + ]); |
| 1261 | + const component = fixture.componentInstance; |
| 1262 | + const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1263 | + |
| 1264 | + component.iconName = 'home'; |
| 1265 | + fixture.detectChanges(); |
| 1266 | + expect(sortedClassNames(matIconElement)) |
| 1267 | + .toEqual(['mat-accent', 'mat-icon', 'material-icons', 'notranslate']); |
| 1268 | + })); |
| 1269 | + |
| 1270 | + it('should be able to configure color globally', fakeAsync(() => { |
| 1271 | + const fixture = createComponent(IconWithLigature, [ |
| 1272 | + { provide: MAT_ICON_DEFAULT_OPTIONS, useValue: { color: 'warn' } } |
| 1273 | + ]); |
| 1274 | + const component = fixture.componentInstance; |
| 1275 | + const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1276 | + |
| 1277 | + component.iconName = 'home'; |
| 1278 | + fixture.detectChanges(); |
| 1279 | + expect(sortedClassNames(matIconElement)) |
| 1280 | + .toEqual(['mat-icon', 'mat-warn', 'material-icons', 'notranslate']); |
| 1281 | + })); |
| 1282 | + |
| 1283 | + it('should be able to configure default color and color globally', fakeAsync(() => { |
| 1284 | + const fixture = createComponent(IconWithLigature, [ |
| 1285 | + { provide: MAT_ICON_DEFAULT_OPTIONS, useValue: { defaultColor: 'accent', color: 'warn' } } |
| 1286 | + ]); |
| 1287 | + const component = fixture.componentInstance; |
| 1288 | + const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1289 | + |
| 1290 | + component.iconName = 'home'; |
| 1291 | + fixture.detectChanges(); |
| 1292 | + expect(sortedClassNames(matIconElement)) |
| 1293 | + .toEqual(['mat-icon', 'mat-warn', 'material-icons', 'notranslate']); |
| 1294 | + })); |
| 1295 | + |
| 1296 | + it('should use passed color rather then default color or color provided', fakeAsync(() => { |
| 1297 | + const fixture = createComponent(IconWithColor, [ |
| 1298 | + { provide: MAT_ICON_DEFAULT_OPTIONS, useValue: { defaultColor: 'accent', color: 'warn' } } |
| 1299 | + ]); |
| 1300 | + const component = fixture.componentInstance; |
| 1301 | + const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1302 | + |
| 1303 | + component.iconName = 'home'; |
| 1304 | + fixture.detectChanges(); |
| 1305 | + expect(sortedClassNames(matIconElement)) |
| 1306 | + .toEqual(['mat-icon', 'mat-primary', 'material-icons', 'notranslate']); |
| 1307 | + })); |
| 1308 | + |
| 1309 | + it('should use default color if no passed color', fakeAsync(() => { |
| 1310 | + const fixture = createComponent(IconWithColor, [ |
| 1311 | + { provide: MAT_ICON_DEFAULT_OPTIONS, useValue: { defaultColor: 'accent', color: 'warn' } } |
| 1312 | + ]); |
| 1313 | + const component = fixture.componentInstance; |
| 1314 | + const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1315 | + |
| 1316 | + component.iconName = 'home'; |
| 1317 | + component.iconColor = ''; |
| 1318 | + fixture.detectChanges(); |
| 1319 | + expect(sortedClassNames(matIconElement)) |
| 1320 | + .toEqual(['mat-accent', 'mat-icon', 'material-icons', 'notranslate']); |
| 1321 | + })); |
| 1322 | + |
| 1323 | + it('should be able to configure font set globally', fakeAsync(() => { |
| 1324 | + const fixture = createComponent(IconWithLigature, [ |
| 1325 | + { provide: MAT_ICON_DEFAULT_OPTIONS, useValue: { fontSet: 'custom-font-set' } } |
| 1326 | + ]); |
| 1327 | + const component = fixture.componentInstance; |
| 1328 | + const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1329 | + |
| 1330 | + component.iconName = 'home'; |
| 1331 | + fixture.detectChanges(); |
| 1332 | + expect(sortedClassNames(matIconElement)) |
| 1333 | + .toEqual(['custom-font-set', 'mat-icon', 'mat-icon-no-color', 'notranslate']); |
| 1334 | + })); |
| 1335 | + |
| 1336 | + it('should use passed fontSet rather then default one', fakeAsync(() => { |
| 1337 | + const fixture = createComponent(IconWithCustomFontCss, [ |
| 1338 | + { provide: MAT_ICON_DEFAULT_OPTIONS, useValue: { fontSet: 'default-font-set' } } |
| 1339 | + ]); |
| 1340 | + const component = fixture.componentInstance; |
| 1341 | + const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1342 | + |
| 1343 | + component.fontSet = 'custom-font-set'; |
| 1344 | + component.fontIcon = 'house'; |
| 1345 | + fixture.detectChanges(); |
| 1346 | + expect(sortedClassNames(matIconElement)) |
| 1347 | + .toEqual(['custom-font-set', 'house', 'mat-icon', 'mat-icon-no-color', 'notranslate']); |
| 1348 | + })); |
| 1349 | + |
| 1350 | + it('should use passed empty fontSet rather then default one', fakeAsync(() => { |
| 1351 | + const fixture = createComponent(IconWithCustomFontCss, [ |
| 1352 | + { provide: MAT_ICON_DEFAULT_OPTIONS, useValue: { fontSet: 'default-font-set' } } |
| 1353 | + ]); |
| 1354 | + const component = fixture.componentInstance; |
| 1355 | + const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon'); |
| 1356 | + |
| 1357 | + component.fontIcon = 'house'; |
| 1358 | + fixture.detectChanges(); |
| 1359 | + expect(sortedClassNames(matIconElement)) |
| 1360 | + .toEqual(['house', 'mat-icon', 'mat-icon-no-color', 'material-icons', 'notranslate']); |
| 1361 | + })); |
| 1362 | +}); |
| 1363 | + |
1240 | 1364 | @Component({template: `<mat-icon>{{iconName}}</mat-icon>`})
|
1241 | 1365 | class IconWithLigature {
|
1242 | 1366 | iconName = '';
|
|
0 commit comments