Skip to content

fix(stepper): error being thrown if selected step is accessed too early #11186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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