File tree Expand file tree Collapse file tree 2 files changed +27
-2
lines changed Expand file tree Collapse file tree 2 files changed +27
-2
lines changed Original file line number Diff line number Diff line change @@ -189,9 +189,12 @@ export class CdkStepper implements AfterViewInit, OnDestroy {
189189
190190 /** The step that is selected. */
191191 @Input ( )
192- get selected ( ) : CdkStep { return this . _steps . toArray ( ) [ this . selectedIndex ] ; }
192+ get selected ( ) : CdkStep {
193+ // @deletion -target 7.0.0 Change return type to `CdkStep | undefined`.
194+ return this . _steps ? this . _steps . toArray ( ) [ this . selectedIndex ] : undefined ! ;
195+ }
193196 set selected ( step : CdkStep ) {
194- this . selectedIndex = this . _steps . toArray ( ) . indexOf ( step ) ;
197+ this . selectedIndex = this . _steps ? this . _steps . toArray ( ) . indexOf ( step ) : - 1 ;
195198 }
196199
197200 /** Event emitted when the selected step has changed. */
Original file line number Diff line number Diff line change @@ -340,6 +340,28 @@ describe('MatStepper', () => {
340340 selectionChangeSubscription . unsubscribe ( ) ;
341341 animationDoneSubscription . unsubscribe ( ) ;
342342 } ) ) ;
343+
344+ it ( 'should not throw when attempting to get the selected step too early' , ( ) => {
345+ fixture . destroy ( ) ;
346+ fixture = TestBed . createComponent ( SimpleMatVerticalStepperApp ) ;
347+
348+ const stepperComponent : MatVerticalStepper = fixture . debugElement
349+ . query ( By . css ( 'mat-vertical-stepper' ) ) . componentInstance ;
350+
351+ expect ( ( ) => stepperComponent . selected ) . not . toThrow ( ) ;
352+ } ) ;
353+
354+ it ( 'should not throw when attempting to set the selected step too early' , ( ) => {
355+ fixture . destroy ( ) ;
356+ fixture = TestBed . createComponent ( SimpleMatVerticalStepperApp ) ;
357+
358+ const stepperComponent : MatVerticalStepper = fixture . debugElement
359+ . query ( By . css ( 'mat-vertical-stepper' ) ) . componentInstance ;
360+
361+ expect ( ( ) => stepperComponent . selected = null ! ) . not . toThrow ( ) ;
362+ expect ( stepperComponent . selectedIndex ) . toBe ( - 1 ) ;
363+ } ) ;
364+
343365 } ) ;
344366
345367 describe ( 'icon overrides' , ( ) => {
You can’t perform that action at this time.
0 commit comments