I am trying to write a test for a component method that uses Observable.forkJoin. I have been all over the web and down some marbles rabbit holes, but in the end I think all I really need to do is mock the Observable forkJoin call and return fake data. Here is my component method
public loadData(): void {
this.someOtherMethod();
this.someProperty = false;
this.someOtherMethod2();
if (this.isNew) {
this.noData = true;
} else if (this.key) {
Observable.forkJoin([
/*00*/ this.service1$.someCall(this.key),
/*01*/ this.service2$.someCall(this.key),
/*02*/ this.service2$.someCall1(this.key),
/*03*/ this.service2$.someCall2(this.key),
/*04*/ this.service2$.someCall3(this.key),
/*05*/ this.service2$.someCall4(this.key),
/*06*/ this.service2$.someCall5(this.key),
])
.takeWhile(() => this.alive)
.subscribe(
response => {
... // join all the data together
},
// error => this.handleError(error)
);
}
this.changeDetector$.markForCheck();
}
Here is my test so far:
it('makes expected calls', async(() => {
const response = [];
const service1Stub: Service1 = fixture.debugElement.injector.get(Service1 );
const service2Stub: Service2 = fixture.debugElement.injector.get(Service2 );
comp.key = key;
spyOn(comp, 'someOtherMethod').and.returnValue(of(response));
spyOn(comp, 'someOtherMethod2').and.returnValue(of(dummyData));
spyOn(service1Stub, 'someCall').and.returnValue(of(dummyData));
spyOn(service2Stub, 'someCall').and.returnValue(of(response));
spyOn(service2Stub, 'someCall1').and.returnValue(of(response));
spyOn(service2Stub, 'someCall2').and.returnValue(of(response));
spyOn(service2Stub, 'someCall3').and.returnValue(of(response));
spyOn(service2Stub, 'someCall4').and.returnValue(of(response));
spyOn(service2Stub, 'someCall5').and.returnValue(of(response));
comp.loadData();
expect(comp.someProperty).toBe(false);
expect(comp.someOtherMethod).toHaveBeenCalled();
expect(comp.someOtherMethod2).toHaveBeenCalled();
expect(service1Stub.someCall).toHaveBeenCalledWith(key);
expect(service2Stub.someCall1).toHaveBeenCalledWith(key);
expect(service2Stub.someCall2).toHaveBeenCalledWith(key);
expect(service1Stub.someCall3).toHaveBeenCalledWith(key);
expect(service1Stub.someCall4).toHaveBeenCalledWith(key);
expect(service1Stub.someCall5).toHaveBeenCalledWith(key);
}));
I get the following error (after I comment out the error catch above):
TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
Marbles seems to be concerned with testing the observable and how it reacts. I just want to see that the calls are being made and do deeper testing into what is happening inside the subscribe where all the data is joined together.
I know there are better ways to handle data but that requires a large overhaul of the application. I cannot change the method, just have to live with how it is now.