-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
icon.spec.ts
450 lines (360 loc) · 16.7 KB
/
icon.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import {inject, async, TestBed} from '@angular/core/testing';
import {SafeResourceUrl, DomSanitizer} from '@angular/platform-browser';
import {HttpModule, XHRBackend} from '@angular/http';
import {MockBackend} from '@angular/http/testing';
import {Component} from '@angular/core';
import {MdIconModule} from './index';
import {MdIconRegistry, getMdIconNoHttpProviderError} from './icon-registry';
import {getFakeSvgHttpResponse} from './fake-svgs';
import {wrappedErrorMessage} from '@angular/cdk/testing';
/** Returns the CSS classes assigned to an element as a sorted array. */
function sortedClassNames(element: Element): string[] {
return element.className.split(' ').sort();
}
/**
* Verifies that an element contains a single <svg> child element, and returns that child.
*/
function verifyAndGetSingleSvgChild(element: SVGElement): SVGElement {
expect(element.childNodes.length).toBe(1);
const svgChild = element.childNodes[0] as SVGElement;
expect(svgChild.tagName.toLowerCase()).toBe('svg');
return svgChild;
}
/**
* Verifies that an element contains a single <path> child element whose "id" attribute has
* the specified value.
*/
function verifyPathChildElement(element: Element, attributeValue: string): void {
expect(element.childNodes.length).toBe(1);
const pathElement = element.childNodes[0] as SVGPathElement;
expect(pathElement.tagName.toLowerCase()).toBe('path');
expect(pathElement.getAttribute('id')).toBe(attributeValue);
}
describe('MdIcon', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpModule, MdIconModule],
declarations: [
IconWithColor,
IconWithLigature,
IconWithCustomFontCss,
IconFromSvgName,
IconWithAriaHiddenFalse,
IconWithBindingAndNgIf,
],
providers: [
MockBackend,
{provide: XHRBackend, useExisting: MockBackend},
]
});
TestBed.compileComponents();
}));
let mdIconRegistry: MdIconRegistry;
let sanitizer: DomSanitizer;
let httpRequestUrls: string[];
let deps = [MdIconRegistry, MockBackend, DomSanitizer];
beforeEach(inject(deps, (mir: MdIconRegistry, mockBackend: MockBackend, ds: DomSanitizer) => {
mdIconRegistry = mir;
sanitizer = ds;
// Keep track of requests so we can verify caching behavior.
// Return responses for the SVGs defined in fake-svgs.ts.
httpRequestUrls = [];
mockBackend.connections.subscribe((connection: any) => {
const url = connection.request.url;
httpRequestUrls.push(url);
connection.mockRespond(getFakeSvgHttpResponse(url));
});
}));
it('should apply class based on color attribute', () => {
let fixture = TestBed.createComponent(IconWithColor);
const testComponent = fixture.componentInstance;
const mdIconElement = fixture.debugElement.nativeElement.querySelector('md-icon');
testComponent.iconName = 'home';
testComponent.iconColor = 'primary';
fixture.detectChanges();
expect(sortedClassNames(mdIconElement)).toEqual(['mat-icon', 'mat-primary', 'material-icons']);
});
it('should mark md-icon as aria-hidden by default', () => {
const fixture = TestBed.createComponent(IconWithLigature);
const iconElement = fixture.debugElement.nativeElement.querySelector('md-icon');
expect(iconElement.getAttribute('aria-hidden'))
.toBe('true', 'Expected the md-icon element has aria-hidden="true" by default');
});
it('should not override a user-provided aria-hidden attribute', () => {
const fixture = TestBed.createComponent(IconWithAriaHiddenFalse);
const iconElement = fixture.debugElement.nativeElement.querySelector('md-icon');
expect(iconElement.getAttribute('aria-hidden'))
.toBe('false', 'Expected the md-icon element has the user-provided aria-hidden value');
});
describe('Ligature icons', () => {
it('should add material-icons class by default', () => {
let fixture = TestBed.createComponent(IconWithLigature);
const testComponent = fixture.componentInstance;
const mdIconElement = fixture.debugElement.nativeElement.querySelector('md-icon');
testComponent.iconName = 'home';
fixture.detectChanges();
expect(sortedClassNames(mdIconElement)).toEqual(['mat-icon', 'material-icons']);
});
it('should use alternate icon font if set', () => {
mdIconRegistry.setDefaultFontSetClass('myfont');
let fixture = TestBed.createComponent(IconWithLigature);
const testComponent = fixture.componentInstance;
const mdIconElement = fixture.debugElement.nativeElement.querySelector('md-icon');
testComponent.iconName = 'home';
fixture.detectChanges();
expect(sortedClassNames(mdIconElement)).toEqual(['mat-icon', 'myfont']);
});
});
describe('Icons from URLs', () => {
it('should register icon URLs by name', () => {
mdIconRegistry.addSvgIcon('fluffy', trust('cat.svg'));
mdIconRegistry.addSvgIcon('fido', trust('dog.svg'));
let fixture = TestBed.createComponent(IconFromSvgName);
const testComponent = fixture.componentInstance;
const mdIconElement = fixture.debugElement.nativeElement.querySelector('md-icon');
let svgElement: SVGElement;
testComponent.iconName = 'fido';
fixture.detectChanges();
svgElement = verifyAndGetSingleSvgChild(mdIconElement);
verifyPathChildElement(svgElement, 'woof');
// Change the icon, and the SVG element should be replaced.
testComponent.iconName = 'fluffy';
fixture.detectChanges();
svgElement = verifyAndGetSingleSvgChild(mdIconElement);
verifyPathChildElement(svgElement, 'meow');
expect(httpRequestUrls).toEqual(['dog.svg', 'cat.svg']);
// Using an icon from a previously loaded URL should not cause another HTTP request.
testComponent.iconName = 'fido';
fixture.detectChanges();
svgElement = verifyAndGetSingleSvgChild(mdIconElement);
verifyPathChildElement(svgElement, 'woof');
expect(httpRequestUrls).toEqual(['dog.svg', 'cat.svg']);
});
it('should throw an error when using an untrusted icon url', () => {
mdIconRegistry.addSvgIcon('fluffy', 'farm-set-1.svg');
expect(() => {
let fixture = TestBed.createComponent(IconFromSvgName);
fixture.componentInstance.iconName = 'fluffy';
fixture.detectChanges();
}).toThrowError(/unsafe value used in a resource URL context/);
});
it('should throw an error when using an untrusted icon set url', () => {
mdIconRegistry.addSvgIconSetInNamespace('farm', 'farm-set-1.svg');
expect(() => {
let fixture = TestBed.createComponent(IconFromSvgName);
fixture.componentInstance.iconName = 'farm:pig';
fixture.detectChanges();
}).toThrowError(/unsafe value used in a resource URL context/);
});
it('should extract icon from SVG icon set', () => {
mdIconRegistry.addSvgIconSetInNamespace('farm', trust('farm-set-1.svg'));
let fixture = TestBed.createComponent(IconFromSvgName);
const testComponent = fixture.componentInstance;
const mdIconElement = fixture.debugElement.nativeElement.querySelector('md-icon');
let svgElement: any;
let svgChild: any;
testComponent.iconName = 'farm:pig';
fixture.detectChanges();
expect(mdIconElement.childNodes.length).toBe(1);
svgElement = verifyAndGetSingleSvgChild(mdIconElement);
expect(svgElement.childNodes.length).toBe(1);
svgChild = svgElement.childNodes[0];
// The first <svg> child should be the <g id="pig"> element.
expect(svgChild.tagName.toLowerCase()).toBe('g');
expect(svgChild.getAttribute('id')).toBe('pig');
verifyPathChildElement(svgChild, 'oink');
// Change the icon, and the SVG element should be replaced.
testComponent.iconName = 'farm:cow';
fixture.detectChanges();
svgElement = verifyAndGetSingleSvgChild(mdIconElement);
svgChild = svgElement.childNodes[0];
// The first <svg> child should be the <g id="cow"> element.
expect(svgChild.tagName.toLowerCase()).toBe('g');
expect(svgChild.getAttribute('id')).toBe('cow');
verifyPathChildElement(svgChild, 'moo');
});
it('should allow multiple icon sets in a namespace', () => {
mdIconRegistry.addSvgIconSetInNamespace('farm', trust('farm-set-1.svg'));
mdIconRegistry.addSvgIconSetInNamespace('farm', trust('farm-set-2.svg'));
mdIconRegistry.addSvgIconSetInNamespace('arrows', trust('arrow-set.svg'));
let fixture = TestBed.createComponent(IconFromSvgName);
const testComponent = fixture.componentInstance;
const mdIconElement = fixture.debugElement.nativeElement.querySelector('md-icon');
let svgElement: any;
let svgChild: any;
testComponent.iconName = 'farm:pig';
fixture.detectChanges();
svgElement = verifyAndGetSingleSvgChild(mdIconElement);
expect(svgElement.childNodes.length).toBe(1);
svgChild = svgElement.childNodes[0];
// The <svg> child should be the <g id="pig"> element.
expect(svgChild.tagName.toLowerCase()).toBe('g');
expect(svgChild.getAttribute('id')).toBe('pig');
expect(svgChild.childNodes.length).toBe(1);
verifyPathChildElement(svgChild, 'oink');
// Both icon sets registered in the 'farm' namespace should have been fetched.
expect(httpRequestUrls.sort()).toEqual(['farm-set-1.svg', 'farm-set-2.svg']);
// Change the icon name to one that appears in both icon sets. The icon from the set that
// was registered last should be used (with id attribute of 'moo moo' instead of 'moo'),
// and no additional HTTP request should be made.
testComponent.iconName = 'farm:cow';
fixture.detectChanges();
svgElement = verifyAndGetSingleSvgChild(mdIconElement);
svgChild = svgElement.childNodes[0];
// The first <svg> child should be the <g id="cow"> element.
expect(svgChild.tagName.toLowerCase()).toBe('g');
expect(svgChild.getAttribute('id')).toBe('cow');
expect(svgChild.childNodes.length).toBe(1);
verifyPathChildElement(svgChild, 'moo moo');
expect(httpRequestUrls.sort()).toEqual(['farm-set-1.svg', 'farm-set-2.svg']);
});
it('should unwrap <symbol> nodes', () => {
mdIconRegistry.addSvgIconSetInNamespace('farm', trust('farm-set-3.svg'));
const fixture = TestBed.createComponent(IconFromSvgName);
const testComponent = fixture.componentInstance;
const mdIconElement = fixture.debugElement.nativeElement.querySelector('md-icon');
testComponent.iconName = 'farm:duck';
fixture.detectChanges();
const svgElement = verifyAndGetSingleSvgChild(mdIconElement);
const firstChild = svgElement.childNodes[0];
expect(svgElement.querySelector('symbol')).toBeFalsy();
expect(svgElement.childNodes.length).toBe(1);
expect(firstChild.nodeName.toLowerCase()).toBe('path');
expect((firstChild as HTMLElement).getAttribute('id')).toBe('quack');
});
it('should not wrap <svg> elements in icon sets in another svg tag', () => {
mdIconRegistry.addSvgIconSet(trust('arrow-set.svg'));
let fixture = TestBed.createComponent(IconFromSvgName);
const testComponent = fixture.componentInstance;
const mdIconElement = fixture.debugElement.nativeElement.querySelector('md-icon');
let svgElement: any;
testComponent.iconName = 'left-arrow';
fixture.detectChanges();
// arrow-set.svg stores its icons as nested <svg> elements, so they should be used
// directly and not wrapped in an outer <svg> tag like the <g> elements in other sets.
svgElement = verifyAndGetSingleSvgChild(mdIconElement);
verifyPathChildElement(svgElement, 'left');
});
it('should return unmodified copies of icons from icon sets', () => {
mdIconRegistry.addSvgIconSet(trust('arrow-set.svg'));
let fixture = TestBed.createComponent(IconFromSvgName);
const testComponent = fixture.componentInstance;
const mdIconElement = fixture.debugElement.nativeElement.querySelector('md-icon');
let svgElement: any;
testComponent.iconName = 'left-arrow';
fixture.detectChanges();
svgElement = verifyAndGetSingleSvgChild(mdIconElement);
verifyPathChildElement(svgElement, 'left');
// Modify the SVG element by setting a viewBox attribute.
svgElement.setAttribute('viewBox', '0 0 100 100');
// Switch to a different icon.
testComponent.iconName = 'right-arrow';
fixture.detectChanges();
svgElement = verifyAndGetSingleSvgChild(mdIconElement);
verifyPathChildElement(svgElement, 'right');
// Switch back to the first icon. The viewBox attribute should not be present.
testComponent.iconName = 'left-arrow';
fixture.detectChanges();
svgElement = verifyAndGetSingleSvgChild(mdIconElement);
verifyPathChildElement(svgElement, 'left');
expect(svgElement.getAttribute('viewBox')).toBeFalsy();
});
it('should not throw when toggling an icon that has a binding in IE11', () => {
mdIconRegistry.addSvgIcon('fluffy', trust('cat.svg'));
const fixture = TestBed.createComponent(IconWithBindingAndNgIf);
fixture.detectChanges();
expect(() => {
fixture.componentInstance.showIcon = false;
fixture.detectChanges();
fixture.componentInstance.showIcon = true;
fixture.detectChanges();
}).not.toThrow();
});
it('should remove the SVG element from the DOM when the binding is cleared', () => {
mdIconRegistry.addSvgIconSet(trust('arrow-set.svg'));
let fixture = TestBed.createComponent(IconFromSvgName);
const testComponent = fixture.componentInstance;
const icon = fixture.debugElement.nativeElement.querySelector('md-icon');
testComponent.iconName = 'left-arrow';
fixture.detectChanges();
expect(icon.querySelector('svg')).toBeTruthy();
testComponent.iconName = undefined;
fixture.detectChanges();
expect(icon.querySelector('svg')).toBeFalsy();
});
});
describe('custom fonts', () => {
it('should apply CSS classes for custom font and icon', () => {
mdIconRegistry.registerFontClassAlias('f1', 'font1');
mdIconRegistry.registerFontClassAlias('f2');
let fixture = TestBed.createComponent(IconWithCustomFontCss);
const testComponent = fixture.componentInstance;
const mdIconElement = fixture.debugElement.nativeElement.querySelector('md-icon');
testComponent.fontSet = 'f1';
testComponent.fontIcon = 'house';
fixture.detectChanges();
expect(sortedClassNames(mdIconElement)).toEqual(['font1', 'house', 'mat-icon']);
testComponent.fontSet = 'f2';
testComponent.fontIcon = 'igloo';
fixture.detectChanges();
expect(sortedClassNames(mdIconElement)).toEqual(['f2', 'igloo', 'mat-icon']);
testComponent.fontSet = 'f3';
testComponent.fontIcon = 'tent';
fixture.detectChanges();
expect(sortedClassNames(mdIconElement)).toEqual(['f3', 'mat-icon', 'tent']);
});
});
/** Marks an svg icon url as explicitly trusted. */
function trust(iconUrl: string): SafeResourceUrl {
return sanitizer.bypassSecurityTrustResourceUrl(iconUrl);
}
});
describe('MdIcon without HttpModule', () => {
let mdIconRegistry: MdIconRegistry;
let sanitizer: DomSanitizer;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MdIconModule],
declarations: [IconFromSvgName],
});
TestBed.compileComponents();
}));
beforeEach(inject([MdIconRegistry, DomSanitizer], (mir: MdIconRegistry, ds: DomSanitizer) => {
mdIconRegistry = mir;
sanitizer = ds;
}));
it('should throw an error when trying to load a remote icon', async() => {
const expectedError = wrappedErrorMessage(getMdIconNoHttpProviderError());
expect(() => {
mdIconRegistry.addSvgIcon('fido', sanitizer.bypassSecurityTrustResourceUrl('dog.svg'));
let fixture = TestBed.createComponent(IconFromSvgName);
fixture.componentInstance.iconName = 'fido';
fixture.detectChanges();
}).toThrowError(expectedError);
});
});
@Component({template: `<md-icon>{{iconName}}</md-icon>`})
class IconWithLigature {
iconName = '';
}
@Component({template: `<md-icon [color]="iconColor">{{iconName}}</md-icon>`})
class IconWithColor {
iconName = '';
iconColor = 'primary';
}
@Component({template: `<md-icon [fontSet]="fontSet" [fontIcon]="fontIcon"></md-icon>`})
class IconWithCustomFontCss {
fontSet = '';
fontIcon = '';
}
@Component({template: `<md-icon [svgIcon]="iconName"></md-icon>`})
class IconFromSvgName {
iconName: string | undefined = '';
}
@Component({template: '<md-icon aria-hidden="false">face</md-icon>'})
class IconWithAriaHiddenFalse { }
@Component({template: `<md-icon [svgIcon]="iconName" *ngIf="showIcon">{{iconName}}</md-icon>`})
class IconWithBindingAndNgIf {
iconName = 'fluffy';
showIcon = true;
}