1
- import { TestBed , async , ComponentFixture } from '@angular/core/testing' ;
2
- import { Component } from '@angular/core' ;
1
+ import { TestBed , ComponentFixture } from '@angular/core/testing' ;
2
+ import { Component , Type } from '@angular/core' ;
3
3
import { By } from '@angular/platform-browser' ;
4
- import { Location } from '@angular/common' ;
5
- import { MatProgressBarModule } from './index' ;
4
+ import { MatProgressBarModule , MAT_PROGRESS_BAR_LOCATION } from './index' ;
6
5
7
6
8
7
describe ( 'MatProgressBar' , ( ) => {
9
8
let fakePath = '/fake-path' ;
10
9
11
- beforeEach ( async ( ( ) = > {
10
+ function createComponent < T > ( componentType : Type < T > ) : ComponentFixture < T > {
12
11
TestBed . configureTestingModule ( {
13
12
imports : [ MatProgressBarModule ] ,
14
- declarations : [
15
- BasicProgressBar ,
16
- BufferProgressBar ,
17
- ] ,
13
+ declarations : [ componentType ] ,
18
14
providers : [ {
19
- provide : Location ,
20
- useValue : { path : ( ) => fakePath }
15
+ provide : MAT_PROGRESS_BAR_LOCATION ,
16
+ useValue : { pathname : fakePath }
21
17
} ]
22
- } ) ;
23
-
24
- TestBed . compileComponents ( ) ;
25
- } ) ) ;
18
+ } ) . compileComponents ( ) ;
26
19
20
+ return TestBed . createComponent < T > ( componentType ) ;
21
+ }
27
22
28
23
describe ( 'basic progress-bar' , ( ) => {
29
- let fixture : ComponentFixture < BasicProgressBar > ;
30
-
31
- beforeEach ( ( ) => {
32
- fixture = TestBed . createComponent ( BasicProgressBar ) ;
24
+ it ( 'should apply a mode of "determinate" if no mode is provided.' , ( ) => {
25
+ const fixture = createComponent ( BasicProgressBar ) ;
33
26
fixture . detectChanges ( ) ;
34
- } ) ;
35
27
36
- it ( 'should apply a mode of "determinate" if no mode is provided.' , ( ) => {
37
- let progressElement = fixture . debugElement . query ( By . css ( 'mat-progress-bar' ) ) ;
28
+ const progressElement = fixture . debugElement . query ( By . css ( 'mat-progress-bar' ) ) ;
38
29
expect ( progressElement . componentInstance . mode ) . toBe ( 'determinate' ) ;
39
30
} ) ;
40
31
41
32
it ( 'should define default values for value and bufferValue attributes' , ( ) => {
42
- let progressElement = fixture . debugElement . query ( By . css ( 'mat-progress-bar' ) ) ;
33
+ const fixture = createComponent ( BasicProgressBar ) ;
34
+ fixture . detectChanges ( ) ;
35
+
36
+ const progressElement = fixture . debugElement . query ( By . css ( 'mat-progress-bar' ) ) ;
43
37
expect ( progressElement . componentInstance . value ) . toBe ( 0 ) ;
44
38
expect ( progressElement . componentInstance . bufferValue ) . toBe ( 0 ) ;
45
39
} ) ;
46
40
47
41
it ( 'should clamp value and bufferValue between 0 and 100' , ( ) => {
48
- let progressElement = fixture . debugElement . query ( By . css ( 'mat-progress-bar' ) ) ;
49
- let progressComponent = progressElement . componentInstance ;
42
+ const fixture = createComponent ( BasicProgressBar ) ;
43
+ fixture . detectChanges ( ) ;
44
+
45
+ const progressElement = fixture . debugElement . query ( By . css ( 'mat-progress-bar' ) ) ;
46
+ const progressComponent = progressElement . componentInstance ;
50
47
51
48
progressComponent . value = 50 ;
52
49
expect ( progressComponent . value ) . toBe ( 50 ) ;
@@ -68,8 +65,11 @@ describe('MatProgressBar', () => {
68
65
} ) ;
69
66
70
67
it ( 'should return the transform attribute for bufferValue and mode' , ( ) => {
71
- let progressElement = fixture . debugElement . query ( By . css ( 'mat-progress-bar' ) ) ;
72
- let progressComponent = progressElement . componentInstance ;
68
+ const fixture = createComponent ( BasicProgressBar ) ;
69
+ fixture . detectChanges ( ) ;
70
+
71
+ const progressElement = fixture . debugElement . query ( By . css ( 'mat-progress-bar' ) ) ;
72
+ const progressComponent = progressElement . componentInstance ;
73
73
74
74
expect ( progressComponent . _primaryTransform ( ) ) . toEqual ( { transform : 'scaleX(0)' } ) ;
75
75
expect ( progressComponent . _bufferTransform ( ) ) . toBe ( undefined ) ;
@@ -95,26 +95,38 @@ describe('MatProgressBar', () => {
95
95
} ) ;
96
96
97
97
it ( 'should prefix SVG references with the current path' , ( ) => {
98
+ const fixture = createComponent ( BasicProgressBar ) ;
99
+ fixture . detectChanges ( ) ;
100
+
98
101
const rect = fixture . debugElement . query ( By . css ( 'rect' ) ) . nativeElement ;
99
102
expect ( rect . getAttribute ( 'fill' ) ) . toMatch ( / ^ u r l \( [ ' " ] ? \/ f a k e - p a t h # .* [ ' " ] ? \) $ / ) ;
100
103
} ) ;
101
104
105
+ it ( 'should account for location hash when prefixing the SVG references' , ( ) => {
106
+ fakePath = '/fake-path#anchor' ;
107
+
108
+ const fixture = createComponent ( BasicProgressBar ) ;
109
+ fixture . detectChanges ( ) ;
110
+
111
+ const rect = fixture . debugElement . query ( By . css ( 'rect' ) ) . nativeElement ;
112
+ expect ( rect . getAttribute ( 'fill' ) ) . not . toContain ( '#anchor#' ) ;
113
+ } ) ;
114
+
102
115
it ( 'should not be able to tab into the underlying SVG element' , ( ) => {
116
+ const fixture = createComponent ( BasicProgressBar ) ;
117
+ fixture . detectChanges ( ) ;
118
+
103
119
const svg = fixture . debugElement . query ( By . css ( 'svg' ) ) . nativeElement ;
104
120
expect ( svg . getAttribute ( 'focusable' ) ) . toBe ( 'false' ) ;
105
121
} ) ;
106
122
} ) ;
107
123
108
124
describe ( 'buffer progress-bar' , ( ) => {
109
- let fixture : ComponentFixture < BufferProgressBar > ;
110
-
111
- beforeEach ( ( ) => {
112
- fixture = TestBed . createComponent ( BufferProgressBar ) ;
125
+ it ( 'should not modify the mode if a valid mode is provided.' , ( ) => {
126
+ const fixture = createComponent ( BufferProgressBar ) ;
113
127
fixture . detectChanges ( ) ;
114
- } ) ;
115
128
116
- it ( 'should not modify the mode if a valid mode is provided.' , ( ) => {
117
- let progressElement = fixture . debugElement . query ( By . css ( 'mat-progress-bar' ) ) ;
129
+ const progressElement = fixture . debugElement . query ( By . css ( 'mat-progress-bar' ) ) ;
118
130
expect ( progressElement . componentInstance . mode ) . toBe ( 'buffer' ) ;
119
131
} ) ;
120
132
} ) ;
0 commit comments