Currently, I am in the process of testing a component that involves calling a service. My goal is to effectively stub or mock the service in order to control its return value and manipulate the component variables within the callback/success function of the subscribe method. Specifically, when my component invokes the getBusinessDetails method in welcomeService, I encounter an issue. I aim to simulate a predefined Boolean value for the isReturning variable and then update the relevant component variables accordingly. However, I am seeing an unexpected outcome where undefined is expected to be true.
public getBusinessDetails(): void {
this.welcomeService.getBusiness(this.businessId, 2017)
.subscribe((isReturning) => {
this.isReturningApplicant = isReturning;
this.welcomeMessage = this.isReturningApplicant ? 'Welcome Back' : 'Welcome';
});
}
TestBed.configureTestingModule({
declarations: [WelcomeComponent],
imports: [ReactiveFormsModule],
providers: [{ provide: SessionService, useValue: sessionServiceStub },
{ provide: WelcomeService, useValue: welcomeServiceStub }]
});
describe('Gets details', () => {
beforeEach(() => {
sessionServiceStub = {
getSession: function () { return { businessId: 11111, agentId: 11111 } }
}
welcomeServiceStub = {
getBusiness: function () { return { subscribe: () => Observable.of(true) } }
};
})
it('get a returning customer', () => {
component.ngOnInit();
expect(component.isReturningApplicant).toBe(true);
});
})