Skip to content

Commit

Permalink
fix(stepper): error being thrown if selected step is accessed too ear…
Browse files Browse the repository at this point in the history
…ly (#11186)

Fixes an error that is thrown if the selected step is accessed before `AfterViewInit`.

Fixes #11158.
  • Loading branch information
crisbeto authored and jelbourn committed May 8, 2018
1 parent 9e9daf8 commit 4638833
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/cdk/stepper/stepper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,12 @@ export class CdkStepper implements AfterViewInit, OnDestroy {

/** The step that is selected. */
@Input()
get selected(): CdkStep { return this._steps.toArray()[this.selectedIndex]; }
get selected(): CdkStep {
// @deletion-target 7.0.0 Change return type to `CdkStep | undefined`.
return this._steps ? this._steps.toArray()[this.selectedIndex] : undefined!;
}
set selected(step: CdkStep) {
this.selectedIndex = this._steps.toArray().indexOf(step);
this.selectedIndex = this._steps ? this._steps.toArray().indexOf(step) : -1;
}

/** Event emitted when the selected step has changed. */
Expand Down
22 changes: 22 additions & 0 deletions src/lib/stepper/stepper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,28 @@ describe('MatStepper', () => {
selectionChangeSubscription.unsubscribe();
animationDoneSubscription.unsubscribe();
}));

it('should not throw when attempting to get the selected step too early', () => {
fixture.destroy();
fixture = TestBed.createComponent(SimpleMatVerticalStepperApp);

const stepperComponent: MatVerticalStepper = fixture.debugElement
.query(By.css('mat-vertical-stepper')).componentInstance;

expect(() => stepperComponent.selected).not.toThrow();
});

it('should not throw when attempting to set the selected step too early', () => {
fixture.destroy();
fixture = TestBed.createComponent(SimpleMatVerticalStepperApp);

const stepperComponent: MatVerticalStepper = fixture.debugElement
.query(By.css('mat-vertical-stepper')).componentInstance;

expect(() => stepperComponent.selected = null!).not.toThrow();
expect(stepperComponent.selectedIndex).toBe(-1);
});

});

describe('icon overrides', () => {
Expand Down

0 comments on commit 4638833

Please sign in to comment.