-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
progress-bar.spec.ts
269 lines (201 loc) · 10.6 KB
/
progress-bar.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
import {TestBed, ComponentFixture} from '@angular/core/testing';
import {Component, DebugElement, Type} from '@angular/core';
import {By} from '@angular/platform-browser';
import {dispatchFakeEvent} from '@angular/cdk/testing/private';
import {MatProgressBarModule, MAT_PROGRESS_BAR_LOCATION} from './index';
import {MatProgressBar} from './progress-bar';
describe('MatProgressBar', () => {
let fakePath: string;
function createComponent<T>(componentType: Type<T>,
imports?: Type<{}>[]): ComponentFixture<T> {
fakePath = '/fake-path';
TestBed.configureTestingModule({
imports: imports || [MatProgressBarModule],
declarations: [componentType],
providers: [{
provide: MAT_PROGRESS_BAR_LOCATION,
useValue: {getPathname: () => fakePath}
}]
}).compileComponents();
return TestBed.createComponent<T>(componentType);
}
describe('with animation', () => {
describe('basic progress-bar', () => {
it('should apply a mode of "determinate" if no mode is provided.', () => {
const fixture = createComponent(BasicProgressBar);
fixture.detectChanges();
const progressElement = fixture.debugElement.query(By.css('mat-progress-bar'))!;
expect(progressElement.componentInstance.mode).toBe('determinate');
});
it('should define default values for value and bufferValue attributes', () => {
const fixture = createComponent(BasicProgressBar);
fixture.detectChanges();
const progressElement = fixture.debugElement.query(By.css('mat-progress-bar'))!;
expect(progressElement.componentInstance.value).toBe(0);
expect(progressElement.componentInstance.bufferValue).toBe(0);
});
it('should clamp value and bufferValue between 0 and 100', () => {
const fixture = createComponent(BasicProgressBar);
fixture.detectChanges();
const progressElement = fixture.debugElement.query(By.css('mat-progress-bar'))!;
const progressComponent = progressElement.componentInstance;
progressComponent.value = 50;
expect(progressComponent.value).toBe(50);
progressComponent.value = 999;
expect(progressComponent.value).toBe(100);
progressComponent.value = -10;
expect(progressComponent.value).toBe(0);
progressComponent.bufferValue = -29;
expect(progressComponent.bufferValue).toBe(0);
progressComponent.bufferValue = 9;
expect(progressComponent.bufferValue).toBe(9);
progressComponent.bufferValue = 1320;
expect(progressComponent.bufferValue).toBe(100);
});
it('should return the transform attribute for bufferValue and mode', () => {
const fixture = createComponent(BasicProgressBar);
fixture.detectChanges();
const progressElement = fixture.debugElement.query(By.css('mat-progress-bar'))!;
const progressComponent = progressElement.componentInstance;
expect(progressComponent._primaryTransform()).toEqual({transform: 'scale3d(0, 1, 1)'});
expect(progressComponent._bufferTransform()).toBe(null);
progressComponent.value = 40;
expect(progressComponent._primaryTransform()).toEqual({transform: 'scale3d(0.4, 1, 1)'});
expect(progressComponent._bufferTransform()).toBe(null);
progressComponent.value = 35;
progressComponent.bufferValue = 55;
expect(progressComponent._primaryTransform()).toEqual({transform: 'scale3d(0.35, 1, 1)'});
expect(progressComponent._bufferTransform()).toBe(null);
progressComponent.mode = 'buffer';
expect(progressComponent._primaryTransform()).toEqual({transform: 'scale3d(0.35, 1, 1)'});
expect(progressComponent._bufferTransform()).toEqual({transform: 'scale3d(0.55, 1, 1)'});
progressComponent.value = 60;
progressComponent.bufferValue = 60;
expect(progressComponent._primaryTransform()).toEqual({transform: 'scale3d(0.6, 1, 1)'});
expect(progressComponent._bufferTransform()).toEqual({transform: 'scale3d(0.6, 1, 1)'});
});
it('should prefix SVG references with the current path', () => {
const fixture = createComponent(BasicProgressBar);
fixture.detectChanges();
const rect = fixture.debugElement.query(By.css('rect'))!.nativeElement;
expect(rect.getAttribute('fill')).toMatch(/^url\(['"]?\/fake-path#.*['"]?\)$/);
});
it('should account for location hash when prefixing the SVG references', () => {
fakePath = '/fake-path#anchor';
const fixture = createComponent(BasicProgressBar);
fixture.detectChanges();
const rect = fixture.debugElement.query(By.css('rect'))!.nativeElement;
expect(rect.getAttribute('fill')).not.toContain('#anchor#');
});
it('should not be able to tab into the underlying SVG element', () => {
const fixture = createComponent(BasicProgressBar);
fixture.detectChanges();
const svg = fixture.debugElement.query(By.css('svg'))!.nativeElement;
expect(svg.getAttribute('focusable')).toBe('false');
});
it('should use latest path when prefixing the SVG references', () => {
let fixture = createComponent(BasicProgressBar);
fixture.detectChanges();
let rect = fixture.debugElement.query(By.css('rect'))!.nativeElement;
expect(rect.getAttribute('fill')).toMatch(/^url\(['"]?\/fake-path#.*['"]?\)$/);
fixture.destroy();
fakePath = '/another-fake-path';
fixture = TestBed.createComponent(BasicProgressBar);
fixture.detectChanges();
rect = fixture.debugElement.query(By.css('rect'))!.nativeElement;
expect(rect.getAttribute('fill')).toMatch(/^url\(['"]?\/another-fake-path#.*['"]?\)$/);
});
it('should remove the `aria-valuenow` attribute in indeterminate mode', () => {
const fixture = createComponent(BasicProgressBar);
fixture.detectChanges();
const progressElement = fixture.debugElement.query(By.css('mat-progress-bar'))!;
const progressComponent = progressElement.componentInstance;
progressComponent.mode = 'determinate';
progressComponent.value = 50;
fixture.detectChanges();
expect(progressElement.nativeElement.getAttribute('aria-valuenow'))
.toBe('50', 'Expected aria-valuenow to be set in determinate mode.');
progressComponent.mode = 'indeterminate';
fixture.detectChanges();
expect(progressElement.nativeElement.hasAttribute('aria-valuenow'))
.toBe(false, 'Expect aria-valuenow to be cleared in indeterminate mode.');
});
it('should remove the `aria-valuenow` attribute in query mode', () => {
const fixture = createComponent(BasicProgressBar);
fixture.detectChanges();
const progressElement = fixture.debugElement.query(By.css('mat-progress-bar'))!;
const progressComponent = progressElement.componentInstance;
progressComponent.mode = 'determinate';
progressComponent.value = 50;
fixture.detectChanges();
expect(progressElement.nativeElement.getAttribute('aria-valuenow'))
.toBe('50', 'Expected aria-valuenow to be set in determinate mode.');
progressComponent.mode = 'query';
fixture.detectChanges();
expect(progressElement.nativeElement.hasAttribute('aria-valuenow'))
.toBe(false, 'Expect aria-valuenow to be cleared in query mode.');
});
});
describe('animation trigger on determinate setting', () => {
let fixture: ComponentFixture<BasicProgressBar>;
let progressComponent: MatProgressBar;
let primaryValueBar: DebugElement;
beforeEach(() => {
fixture = createComponent(BasicProgressBar);
const progressElement = fixture.debugElement.query(By.css('mat-progress-bar'))!;
progressComponent = progressElement.componentInstance;
primaryValueBar = progressElement.query(By.css('.mat-progress-bar-primary'))!;
});
it('should trigger output event on primary value bar animation end', () => {
fixture.detectChanges();
spyOn(progressComponent.animationEnd, 'next');
progressComponent.value = 40;
expect(progressComponent.animationEnd.next).not.toHaveBeenCalled();
// On animation end, output should be emitted.
dispatchFakeEvent(primaryValueBar.nativeElement, 'transitionend');
expect(progressComponent.animationEnd.next).toHaveBeenCalledWith({ value: 40 });
});
});
describe('animation trigger on buffer setting', () => {
let fixture: ComponentFixture<BufferProgressBar>;
let progressComponent: MatProgressBar;
let primaryValueBar: DebugElement;
beforeEach(() => {
fixture = createComponent(BufferProgressBar);
const progressElement = fixture.debugElement.query(By.css('mat-progress-bar'))!;
progressComponent = progressElement.componentInstance;
primaryValueBar = progressElement.query(By.css('.mat-progress-bar-primary'))!;
});
it('should bind on transitionend eventListener on primaryBarValue', () => {
spyOn(primaryValueBar.nativeElement, 'addEventListener');
fixture.detectChanges();
expect(primaryValueBar.nativeElement.addEventListener).toHaveBeenCalled();
expect(primaryValueBar.nativeElement.addEventListener
.calls.mostRecent().args[0]).toBe('transitionend');
});
it('should trigger output event on primary value bar animation end', () => {
fixture.detectChanges();
spyOn(progressComponent.animationEnd, 'next');
progressComponent.value = 40;
expect(progressComponent.animationEnd.next).not.toHaveBeenCalled();
// On animation end, output should be emitted.
dispatchFakeEvent(primaryValueBar.nativeElement, 'transitionend');
expect(progressComponent.animationEnd.next).toHaveBeenCalledWith({ value: 40 });
});
it('should trigger output event with value not bufferValue', () => {
fixture.detectChanges();
spyOn(progressComponent.animationEnd, 'next');
progressComponent.value = 40;
progressComponent.bufferValue = 70;
expect(progressComponent.animationEnd.next).not.toHaveBeenCalled();
// On animation end, output should be emitted.
dispatchFakeEvent(primaryValueBar.nativeElement, 'transitionend');
expect(progressComponent.animationEnd.next).toHaveBeenCalledWith({ value: 40 });
});
});
});
});
@Component({template: '<mat-progress-bar></mat-progress-bar>'})
class BasicProgressBar { }
@Component({template: '<mat-progress-bar mode="buffer"></mat-progress-bar>'})
class BufferProgressBar { }