Skip to content

Commit f8c6bb0

Browse files
committed
Unit tests
1 parent 44be2e3 commit f8c6bb0

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

src/lib/stepper/stepper.spec.ts

+79
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ describe('MdHorizontalStepper', () => {
7575
let stepHeaders = fixture.debugElement.queryAll(By.css('.mat-horizontal-stepper-header'));
7676
checkKeyboardEvent(stepperComponent, fixture, stepHeaders);
7777
});
78+
79+
it('should only be able to return to a previous step if it is editable', () => {
80+
checkEditableStep(stepperComponent, fixture);
81+
});
82+
83+
it('should show icon instead of index and active label on completed steps', () => {
84+
let stepperComponentEl = fixture.debugElement
85+
.query(By.css('md-horizontal-stepper')).nativeElement;
86+
checkStepIcon(stepperComponent, stepperComponentEl, fixture);
87+
});
7888
});
7989

8090
describe('linear horizontal stepper', () => {
@@ -105,6 +115,10 @@ describe('MdHorizontalStepper', () => {
105115
.queryAll(By.css('.mat-horizontal-stepper-header'))[1].nativeElement;
106116
checkLinearStepperValidity(stepHeaderEl, stepperComponent, testComponent, fixture);
107117
});
118+
119+
it('should be able to move to next step even when invalid if current step is optional', () => {
120+
checkOptionalStep(stepperComponent, testComponent, fixture);
121+
});
108122
});
109123
});
110124

@@ -173,6 +187,16 @@ describe('MdVerticalStepper', () => {
173187
let stepHeaders = fixture.debugElement.queryAll(By.css('.mat-vertical-stepper-header'));
174188
checkKeyboardEvent(stepperComponent, fixture, stepHeaders);
175189
});
190+
191+
it('should only be able to return to a previous step if it is editable', () => {
192+
checkEditableStep(stepperComponent, fixture);
193+
});
194+
195+
it('should show icon instead of index on completed steps', () => {
196+
let stepperComponentEl = fixture.debugElement
197+
.query(By.css('md-vertical-stepper')).nativeElement;
198+
checkStepIcon(stepperComponent, stepperComponentEl, fixture);
199+
});
176200
});
177201

178202
describe('linear vertical stepper', () => {
@@ -204,6 +228,10 @@ describe('MdVerticalStepper', () => {
204228

205229
checkLinearStepperValidity(stepHeaderEl, stepperComponent, testComponent, fixture);
206230
});
231+
232+
it('should be able to move to next step even when invalid if current step is optional', () => {
233+
checkOptionalStep(stepperComponent, testComponent, fixture);
234+
});
207235
});
208236
});
209237

@@ -412,6 +440,57 @@ function checkLinearStepperValidity(stepHeaderEl: HTMLElement,
412440
expect(stepperComponent.selectedIndex).toBe(1);
413441
}
414442

443+
function checkEditableStep(stepperComponent: MdStepper,
444+
fixture: ComponentFixture<any>) {
445+
stepperComponent.selectedIndex = 1;
446+
stepperComponent._steps.toArray()[0].editable = false;
447+
let previousButtonNativeEl = fixture.debugElement
448+
.queryAll(By.directive(MdStepperPrevious))[1].nativeElement;
449+
previousButtonNativeEl.click();
450+
fixture.detectChanges();
451+
452+
expect(stepperComponent.selectedIndex).toBe(1);
453+
454+
stepperComponent._steps.toArray()[0].editable = true;
455+
previousButtonNativeEl.click();
456+
fixture.detectChanges();
457+
458+
expect(stepperComponent.selectedIndex).toBe(0);
459+
}
460+
461+
function checkOptionalStep(stepperComponent: MdStepper,
462+
testComponent: LinearMdHorizontalStepperApp | LinearMdVerticalStepperApp,
463+
fixture: ComponentFixture<any>) {
464+
expect(testComponent.oneGroup.get('oneCtrl')!.value).toBe('');
465+
expect(testComponent.oneGroup.get('oneCtrl')!.valid).toBe(false);
466+
expect(testComponent.oneGroup.valid).toBe(false);
467+
expect(stepperComponent.selectedIndex).toBe(0);
468+
469+
stepperComponent._steps.toArray()[0].optional = true;
470+
let nextButtonNativeEl = fixture.debugElement
471+
.queryAll(By.directive(MdStepperNext))[0].nativeElement;
472+
nextButtonNativeEl.click();
473+
fixture.detectChanges();
474+
475+
expect(stepperComponent.selectedIndex).toBe(1);
476+
}
477+
478+
function checkStepIcon(stepperComponent: MdStepper,
479+
stepperComponentEl: HTMLElement,
480+
fixture: ComponentFixture<any>) {
481+
expect(stepperComponent._steps.toArray()[0].completed).toBe(false);
482+
expect(stepperComponentEl.querySelectorAll('.mat-stepper-index-interacted').length).toBe(0);
483+
expect(stepperComponentEl.querySelectorAll('.mat-stepper-label-active').length).toBe(1);
484+
let nextButtonNativeEl = fixture.debugElement
485+
.queryAll(By.directive(MdStepperNext))[0].nativeElement;
486+
nextButtonNativeEl.click();
487+
fixture.detectChanges();
488+
489+
expect(stepperComponent._steps.toArray()[0].completed).toBe(true);
490+
expect(stepperComponentEl.querySelectorAll('.mat-stepper-index-interacted').length).toBe(1);
491+
expect(stepperComponentEl.querySelectorAll('.mat-stepper-label-active').length).toBe(2);
492+
}
493+
415494
@Component({
416495
template: `
417496
<md-horizontal-stepper>

0 commit comments

Comments
 (0)