2

How to unit test Material stepper in this situation:

export class FooComponent implements OnInit {
    
    @ViewChild('stepper') stepper: MatStepper;
}
    bar() {
       this.stepper.selectedIndex = 1;
    }
}

This will work, but when testing component with Jasmine, I would get:

TypeError: Cannot set property 'selectedIndex' of undefined

I should note that in test MatStepperModule is in imports.

Roy
  • 7,811
  • 4
  • 24
  • 47
NenadP
  • 585
  • 7
  • 24

1 Answers1

2

I wrote my test in Jest but I'm sure you can do something very similar in Jasmine, if not the same.

it('bar should set stepper selectedIndex to 1', () => {
   
    component.stepper = { selectedIndex: 0 } as MatStepper;
    component.bar();
    fixture.detectChanges();

    expect(component.stepper.selectedIndex).toEqual(1);
});
Dharman
  • 30,962
  • 25
  • 85
  • 135
Megan
  • 21
  • 2