Mat Stepper as view child unit test

Viewed 1129

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.

1 Answers

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);
});
Related