Within my Angular service, I have a method that listens for state changes and returns an observable. However, other methods within the same service handle transformation logic:
ngOnInit() {
this.isLoading = true;
this.myService.stateListener().subscribe((res: any) => {
this.data = this.myService.transformModel(res);
this.isLoading = false;
this.cd.detectChanges();
}, (err) => {
this.errMessage = 'Data could not be loaded';
this.isLoading = false;
});
}
During testing, I mock the state listener to provide specific testable data as follows:
const mockService = jasmine.createSpyObj('myService', ['stateListener']);
mockService.stateListener.and.returnValue(of({
number: 107,
mock: someMock
}));
The issue arises when mocking the service because the 'transformerModel' method is no longer accessible (on the mocked service). As a result, the component's bound data becomes 'undefined.'
Is there a way to solely mock the returned observable (stateListener) without affecting the transformer method in the same service?