Currently, I am implementing a stepper feature with back, step, next steps. On the last step, when the user clicks 'done,' I need to call a service to update user data. While I have successfully tested the backStep()
and nextStep()
methods, I now want to test the updateAdmin()
method which updates a boolean value. Below is the relevant code snippet:
P.S: How can I effectively test a service that updates data and returns a response?
Controller:
backStep(step: number) {
this.currentStep = --step;
this.currentSlide.emit(this.currentStep);
}
nextStep(step: number) {
if (++step <= this.totalSteps.length) {
this.currentStep = step;
this.currentSlide.emit(this.currentStep);
} else {
this.updateAdmin();
}
}
updateAdmin() {
const body = {
is_admin: true
}
this.adminService.updateAdmin(user_id, body).subscribe(
(response) => {
console.log(response);
}
);
}
Unit Tests
it('backStep() should decrement current step', () => {
comp.backStep(2);
expect(comp.currentStep).toEqual(1);
});
it('nextStep() should increment current step', () => {
const start = 1;
comp.totalSteps = Array.from(Array(4), (_, i) => start + i);
comp.nextStep(1);
expect(comp.currentStep).toEqual(2);
});
it('Should call updateAdmin method and test mock service', () => {
const start = 1;
comp.totalSteps = Array.from(Array(4), (_, i) => start + i);
comp.nextStep(4);
//when steps reached to max limit (4) I need to call a mock service which should update a field and return response, here I dont know how to test updateAdmin() method.
});
});
Thank you in advance.