1
- import { TestBed , async } from '@angular/core/testing' ;
2
- import { Component } from '@angular/core' ;
1
+ import { TestBed , async , inject } from '@angular/core/testing' ;
2
+ import { Component , ViewEncapsulation } from '@angular/core' ;
3
3
import { By } from '@angular/platform-browser' ;
4
+ import { Platform } from '@angular/cdk/platform' ;
5
+ import { _getShadowRoot } from './progress-spinner' ;
4
6
import {
5
7
MatProgressSpinnerModule ,
6
8
MatProgressSpinner ,
7
9
MAT_PROGRESS_SPINNER_DEFAULT_OPTIONS ,
8
10
} from './index' ;
9
11
10
-
11
12
describe ( 'MatProgressSpinner' , ( ) => {
13
+ const supportsShadowDom = typeof document . createElement ( 'div' ) . attachShadow !== 'undefined' ;
12
14
13
15
beforeEach ( async ( ( ) => {
14
16
TestBed . configureTestingModule ( {
@@ -22,6 +24,7 @@ describe('MatProgressSpinner', () => {
22
24
ProgressSpinnerCustomDiameter ,
23
25
SpinnerWithColor ,
24
26
ProgressSpinnerWithStringValues ,
27
+ IndeterminateSpinnerInShadowDom ,
25
28
] ,
26
29
} ) . compileComponents ( ) ;
27
30
} ) ) ;
@@ -154,6 +157,35 @@ describe('MatProgressSpinner', () => {
154
157
. toBe ( '0 0 25.2 25.2' , 'Expected the custom diameter to be applied to the svg viewBox.' ) ;
155
158
} ) ;
156
159
160
+ it ( 'should add a style tag with the indeterminate animation to the document head when using a ' +
161
+ 'non-default diameter' , inject ( [ Platform ] , ( platform : Platform ) => {
162
+ // On Edge and IE we use a fallback animation because the
163
+ // browser doesn't support animating SVG correctly.
164
+ if ( platform . EDGE || platform . TRIDENT ) {
165
+ return ;
166
+ }
167
+
168
+ const fixture = TestBed . createComponent ( ProgressSpinnerCustomDiameter ) ;
169
+ fixture . componentInstance . diameter = 32 ;
170
+ fixture . detectChanges ( ) ;
171
+
172
+ expect ( document . head . querySelectorAll ( 'style[mat-spinner-animation="32"]' ) . length ) . toBe ( 1 ) ;
173
+
174
+ // Change to something different so we get another tag.
175
+ fixture . componentInstance . diameter = 64 ;
176
+ fixture . detectChanges ( ) ;
177
+
178
+ expect ( document . head . querySelectorAll ( 'style[mat-spinner-animation="32"]' ) . length ) . toBe ( 1 ) ;
179
+ expect ( document . head . querySelectorAll ( 'style[mat-spinner-animation="64"]' ) . length ) . toBe ( 1 ) ;
180
+
181
+ // Change back to the initial one.
182
+ fixture . componentInstance . diameter = 32 ;
183
+ fixture . detectChanges ( ) ;
184
+
185
+ expect ( document . head . querySelectorAll ( 'style[mat-spinner-animation="32"]' ) . length ) . toBe ( 1 ) ;
186
+ expect ( document . head . querySelectorAll ( 'style[mat-spinner-animation="64"]' ) . length ) . toBe ( 1 ) ;
187
+ } ) ) ;
188
+
157
189
it ( 'should allow a custom stroke width' , ( ) => {
158
190
const fixture = TestBed . createComponent ( ProgressSpinnerCustomStrokeWidth ) ;
159
191
@@ -321,6 +353,57 @@ describe('MatProgressSpinner', () => {
321
353
expect ( progressElement . nativeElement . hasAttribute ( 'aria-valuenow' ) ) . toBe ( false ) ;
322
354
} ) ;
323
355
356
+ it ( 'should add the indeterminate animation style tag to the Shadow root' , ( ) => {
357
+ // The test is only relevant in browsers that support Shadow DOM.
358
+ if ( ! supportsShadowDom ) {
359
+ return ;
360
+ }
361
+
362
+ const fixture = TestBed . createComponent ( IndeterminateSpinnerInShadowDom ) ;
363
+ fixture . componentInstance . diameter = 27 ;
364
+ fixture . detectChanges ( ) ;
365
+
366
+ const spinner = fixture . debugElement . query ( By . css ( 'mat-progress-spinner' ) ) . nativeElement ;
367
+ const shadowRoot = _getShadowRoot ( spinner , document ) as HTMLElement ;
368
+
369
+ expect ( shadowRoot . querySelector ( 'style[mat-spinner-animation="27"]' ) ) . toBeTruthy ( ) ;
370
+
371
+ fixture . componentInstance . diameter = 15 ;
372
+ fixture . detectChanges ( ) ;
373
+
374
+ expect ( shadowRoot . querySelector ( 'style[mat-spinner-animation="27"]' ) ) . toBeTruthy ( ) ;
375
+ } ) ;
376
+
377
+ it ( 'should not duplicate style tags inside the Shadow root' , ( ) => {
378
+ // The test is only relevant in browsers that support Shadow DOM.
379
+ if ( ! supportsShadowDom ) {
380
+ return ;
381
+ }
382
+
383
+ const fixture = TestBed . createComponent ( IndeterminateSpinnerInShadowDom ) ;
384
+ fixture . componentInstance . diameter = 39 ;
385
+ fixture . detectChanges ( ) ;
386
+
387
+ const spinner = fixture . debugElement . query ( By . css ( 'mat-progress-spinner' ) ) . nativeElement ;
388
+ const shadowRoot = _getShadowRoot ( spinner , document ) as HTMLElement ;
389
+
390
+ expect ( shadowRoot . querySelectorAll ( 'style[mat-spinner-animation="39"]' ) . length ) . toBe ( 1 ) ;
391
+
392
+ // Change to something different so we get another tag.
393
+ fixture . componentInstance . diameter = 61 ;
394
+ fixture . detectChanges ( ) ;
395
+
396
+ expect ( shadowRoot . querySelectorAll ( 'style[mat-spinner-animation="39"]' ) . length ) . toBe ( 1 ) ;
397
+ expect ( shadowRoot . querySelectorAll ( 'style[mat-spinner-animation="61"]' ) . length ) . toBe ( 1 ) ;
398
+
399
+ // Change back to the initial one.
400
+ fixture . componentInstance . diameter = 39 ;
401
+ fixture . detectChanges ( ) ;
402
+
403
+ expect ( shadowRoot . querySelectorAll ( 'style[mat-spinner-animation="39"]' ) . length ) . toBe ( 1 ) ;
404
+ expect ( shadowRoot . querySelectorAll ( 'style[mat-spinner-animation="61"]' ) . length ) . toBe ( 1 ) ;
405
+ } ) ;
406
+
324
407
} ) ;
325
408
326
409
@@ -360,3 +443,14 @@ class ProgressSpinnerWithColor { color: string = 'primary'; }
360
443
`
361
444
} )
362
445
class ProgressSpinnerWithStringValues { }
446
+
447
+
448
+ @Component ( {
449
+ template : `
450
+ <mat-progress-spinner mode="indeterminate" [diameter]="diameter"></mat-progress-spinner>
451
+ ` ,
452
+ encapsulation : ViewEncapsulation . ShadowDom ,
453
+ } )
454
+ class IndeterminateSpinnerInShadowDom {
455
+ diameter : number ;
456
+ }
0 commit comments