1
1
import { inject , async , fakeAsync , tick , TestBed } from '@angular/core/testing' ;
2
2
import { SafeResourceUrl , DomSanitizer , SafeHtml } from '@angular/platform-browser' ;
3
3
import { HttpClientTestingModule , HttpTestingController } from '@angular/common/http/testing' ;
4
- import { Component } from '@angular/core' ;
4
+ import { Component , ErrorHandler } from '@angular/core' ;
5
5
import { MatIconModule , MAT_ICON_LOCATION } from './index' ;
6
6
import { MatIconRegistry , getMatIconNoHttpProviderError } from './icon-registry' ;
7
7
import { FAKE_SVGS } from './fake-svgs' ;
@@ -40,11 +40,13 @@ function verifyPathChildElement(element: Element, attributeValue: string): void
40
40
41
41
describe ( 'MatIcon' , ( ) => {
42
42
let fakePath : string ;
43
+ let errorHandler : jasmine . SpyObj < ErrorHandler > ;
43
44
44
45
beforeEach ( async ( ( ) => {
45
46
// The $ prefix tells Karma not to try to process the
46
47
// request so that we don't get warnings in our logs.
47
48
fakePath = '/$fake-path' ;
49
+ errorHandler = jasmine . createSpyObj ( 'errorHandler' , [ 'handleError' ] ) ;
48
50
49
51
TestBed . configureTestingModule ( {
50
52
imports : [ HttpClientTestingModule , MatIconModule ] ,
@@ -59,10 +61,16 @@ describe('MatIcon', () => {
59
61
SvgIconWithUserContent ,
60
62
IconWithLigatureAndSvgBinding ,
61
63
] ,
62
- providers : [ {
63
- provide : MAT_ICON_LOCATION ,
64
- useValue : { getPathname : ( ) => fakePath }
65
- } ]
64
+ providers : [
65
+ {
66
+ provide : MAT_ICON_LOCATION ,
67
+ useValue : { getPathname : ( ) => fakePath }
68
+ } ,
69
+ {
70
+ provide : ErrorHandler ,
71
+ useValue : errorHandler ,
72
+ } ,
73
+ ]
66
74
} ) ;
67
75
68
76
TestBed . compileComponents ( ) ;
@@ -80,15 +88,15 @@ describe('MatIcon', () => {
80
88
} ) ) ;
81
89
82
90
it ( 'should include notranslate class by default' , ( ) => {
83
- let fixture = TestBed . createComponent ( IconWithColor ) ;
91
+ const fixture = TestBed . createComponent ( IconWithColor ) ;
84
92
85
93
const matIconElement = fixture . debugElement . nativeElement . querySelector ( 'mat-icon' ) ;
86
94
expect ( matIconElement . classList . contains ( 'notranslate' ) )
87
95
. toBeTruthy ( 'Expected the mat-icon element to include the notranslate class' ) ;
88
96
} ) ;
89
97
90
98
it ( 'should apply class based on color attribute' , ( ) => {
91
- let fixture = TestBed . createComponent ( IconWithColor ) ;
99
+ const fixture = TestBed . createComponent ( IconWithColor ) ;
92
100
93
101
const testComponent = fixture . componentInstance ;
94
102
const matIconElement = fixture . debugElement . nativeElement . querySelector ( 'mat-icon' ) ;
@@ -100,7 +108,7 @@ describe('MatIcon', () => {
100
108
} ) ;
101
109
102
110
it ( 'should apply a class if there is no color' , ( ) => {
103
- let fixture = TestBed . createComponent ( IconWithColor ) ;
111
+ const fixture = TestBed . createComponent ( IconWithColor ) ;
104
112
105
113
const testComponent = fixture . componentInstance ;
106
114
const matIconElement = fixture . debugElement . nativeElement . querySelector ( 'mat-icon' ) ;
@@ -140,7 +148,7 @@ describe('MatIcon', () => {
140
148
141
149
describe ( 'Ligature icons' , ( ) => {
142
150
it ( 'should add material-icons class by default' , ( ) => {
143
- let fixture = TestBed . createComponent ( IconWithLigature ) ;
151
+ const fixture = TestBed . createComponent ( IconWithLigature ) ;
144
152
145
153
const testComponent = fixture . componentInstance ;
146
154
const matIconElement = fixture . debugElement . nativeElement . querySelector ( 'mat-icon' ) ;
@@ -153,7 +161,7 @@ describe('MatIcon', () => {
153
161
it ( 'should use alternate icon font if set' , ( ) => {
154
162
iconRegistry . setDefaultFontSetClass ( 'myfont' ) ;
155
163
156
- let fixture = TestBed . createComponent ( IconWithLigature ) ;
164
+ const fixture = TestBed . createComponent ( IconWithLigature ) ;
157
165
158
166
const testComponent = fixture . componentInstance ;
159
167
const matIconElement = fixture . debugElement . nativeElement . querySelector ( 'mat-icon' ) ;
@@ -165,7 +173,7 @@ describe('MatIcon', () => {
165
173
166
174
it ( 'should not clear the text of a ligature icon if the svgIcon is bound to something falsy' ,
167
175
( ) => {
168
- let fixture = TestBed . createComponent ( IconWithLigatureAndSvgBinding ) ;
176
+ const fixture = TestBed . createComponent ( IconWithLigatureAndSvgBinding ) ;
169
177
170
178
const testComponent = fixture . componentInstance ;
171
179
const matIconElement = fixture . debugElement . nativeElement . querySelector ( 'mat-icon' ) ;
@@ -182,7 +190,7 @@ describe('MatIcon', () => {
182
190
iconRegistry . addSvgIcon ( 'fluffy' , trustUrl ( 'cat.svg' ) ) ;
183
191
iconRegistry . addSvgIcon ( 'fido' , trustUrl ( 'dog.svg' ) ) ;
184
192
185
- let fixture = TestBed . createComponent ( IconFromSvgName ) ;
193
+ const fixture = TestBed . createComponent ( IconFromSvgName ) ;
186
194
let svgElement : SVGElement ;
187
195
const testComponent = fixture . componentInstance ;
188
196
const iconElement = fixture . debugElement . nativeElement . querySelector ( 'mat-icon' ) ;
@@ -219,7 +227,7 @@ describe('MatIcon', () => {
219
227
iconRegistry . addSvgIcon ( 'fluffy' , trustUrl ( 'cat.svg' ) , { viewBox : '0 0 27 27' } ) ;
220
228
iconRegistry . addSvgIcon ( 'fido' , trustUrl ( 'dog.svg' ) , { viewBox : '0 0 43 43' } ) ;
221
229
222
- let fixture = TestBed . createComponent ( IconFromSvgName ) ;
230
+ const fixture = TestBed . createComponent ( IconFromSvgName ) ;
223
231
let svgElement : SVGElement ;
224
232
const testComponent = fixture . componentInstance ;
225
233
const iconElement = fixture . debugElement . nativeElement . querySelector ( 'mat-icon' ) ;
@@ -242,7 +250,7 @@ describe('MatIcon', () => {
242
250
iconRegistry . addSvgIcon ( 'fluffy' , 'farm-set-1.svg' ) ;
243
251
244
252
expect ( ( ) => {
245
- let fixture = TestBed . createComponent ( IconFromSvgName ) ;
253
+ const fixture = TestBed . createComponent ( IconFromSvgName ) ;
246
254
fixture . componentInstance . iconName = 'fluffy' ;
247
255
fixture . detectChanges ( ) ;
248
256
} ) . toThrowError ( / u n s a f e v a l u e u s e d i n a r e s o u r c e U R L c o n t e x t / ) ;
@@ -252,12 +260,30 @@ describe('MatIcon', () => {
252
260
iconRegistry . addSvgIconSetInNamespace ( 'farm' , 'farm-set-1.svg' ) ;
253
261
254
262
expect ( ( ) => {
255
- let fixture = TestBed . createComponent ( IconFromSvgName ) ;
263
+ const fixture = TestBed . createComponent ( IconFromSvgName ) ;
256
264
fixture . componentInstance . iconName = 'farm:pig' ;
257
265
fixture . detectChanges ( ) ;
258
266
} ) . toThrowError ( / u n s a f e v a l u e u s e d i n a r e s o u r c e U R L c o n t e x t / ) ;
259
267
} ) ;
260
268
269
+
270
+ it ( 'should delegate http error logging to the ErrorHandler' , ( ) => {
271
+ iconRegistry . addSvgIconSetInNamespace ( 'farm' , trustUrl ( 'farm-set-1.svg' ) ) ;
272
+
273
+ const fixture = TestBed . createComponent ( IconFromSvgName ) ;
274
+ const testComponent = fixture . componentInstance ;
275
+
276
+ testComponent . iconName = 'farm:pig' ;
277
+ fixture . detectChanges ( ) ;
278
+ http . expectOne ( 'farm-set-1.svg' ) . error ( new ErrorEvent ( 'Network error' ) ) ;
279
+ fixture . detectChanges ( ) ;
280
+
281
+ expect ( errorHandler . handleError ) . toHaveBeenCalledTimes ( 1 ) ;
282
+ expect ( errorHandler . handleError . calls . argsFor ( 0 ) [ 0 ] . message ) . toEqual (
283
+ 'Loading icon set URL: farm-set-1.svg failed: Http failure response ' +
284
+ 'for farm-set-1.svg: 0 ' ) ;
285
+ } ) ;
286
+
261
287
it ( 'should extract icon from SVG icon set' , ( ) => {
262
288
iconRegistry . addSvgIconSetInNamespace ( 'farm' , trustUrl ( 'farm-set-1.svg' ) ) ;
263
289
@@ -491,7 +517,7 @@ describe('MatIcon', () => {
491
517
it ( 'should remove the SVG element from the DOM when the binding is cleared' , ( ) => {
492
518
iconRegistry . addSvgIconSet ( trustUrl ( 'arrow-set.svg' ) ) ;
493
519
494
- let fixture = TestBed . createComponent ( IconFromSvgName ) ;
520
+ const fixture = TestBed . createComponent ( IconFromSvgName ) ;
495
521
496
522
const testComponent = fixture . componentInstance ;
497
523
const icon = fixture . debugElement . nativeElement . querySelector ( 'mat-icon' ) ;
@@ -534,7 +560,7 @@ describe('MatIcon', () => {
534
560
iconRegistry . addSvgIconLiteral ( 'fluffy' , trustHtml ( FAKE_SVGS . cat ) ) ;
535
561
iconRegistry . addSvgIconLiteral ( 'fido' , trustHtml ( FAKE_SVGS . dog ) ) ;
536
562
537
- let fixture = TestBed . createComponent ( IconFromSvgName ) ;
563
+ const fixture = TestBed . createComponent ( IconFromSvgName ) ;
538
564
let svgElement : SVGElement ;
539
565
const testComponent = fixture . componentInstance ;
540
566
const iconElement = fixture . debugElement . nativeElement . querySelector ( 'mat-icon' ) ;
@@ -561,7 +587,7 @@ describe('MatIcon', () => {
561
587
iconRegistry . addSvgIconLiteral ( 'fluffy' , trustHtml ( FAKE_SVGS . cat ) , { viewBox : '0 0 43 43' } ) ;
562
588
iconRegistry . addSvgIconLiteral ( 'fido' , trustHtml ( FAKE_SVGS . dog ) , { viewBox : '0 0 27 27' } ) ;
563
589
564
- let fixture = TestBed . createComponent ( IconFromSvgName ) ;
590
+ const fixture = TestBed . createComponent ( IconFromSvgName ) ;
565
591
let svgElement : SVGElement ;
566
592
const testComponent = fixture . componentInstance ;
567
593
const iconElement = fixture . debugElement . nativeElement . querySelector ( 'mat-icon' ) ;
@@ -916,7 +942,7 @@ describe('MatIcon without HttpClientModule', () => {
916
942
expect ( ( ) => {
917
943
iconRegistry . addSvgIcon ( 'fido' , sanitizer . bypassSecurityTrustResourceUrl ( 'dog.svg' ) ) ;
918
944
919
- let fixture = TestBed . createComponent ( IconFromSvgName ) ;
945
+ const fixture = TestBed . createComponent ( IconFromSvgName ) ;
920
946
921
947
fixture . componentInstance . iconName = 'fido' ;
922
948
fixture . detectChanges ( ) ;
0 commit comments