I have a function that could be considered long running (actually, it's multi-step where each step could be waiting for an external event like a response from a HTTP call).
There's an invoker function to this which should return an observable that returns the updates to the original function. The original function MUST run whether or not the returned observable is subscribed to. Basically, it needs to return a hot observable.
I have tried the following approach but, cannot get it to work:
function longRunningOperation(): Observable<number> {
const updates$ = new Subject<number>();
Promise.resolve().then(() => {
console.log('starting updates...');
updates$.next(1);
updates$.next(2);
updates$.next(3);
updates$.complete();
});
return updates$;
}
If I do a marble test on the above, I see that the actual events generated being empty (though the function does execute).
it('should return and start executing', () => {
const updates$ = longRunningOperation();
const marbles = '(abc|)';
const events = { a: 1, b: 2, c: 3 };
new TestScheduler((actual, expected) =>
expect(actual).toEqual(expected)
).run(({ expectObservable }) => {
expectObservable(updates$).toBe(marbles, events);
console.log('Test Scheduler subscribed');
});
});
What am I doing wrong here?
Link to demo https://stackblitz.com/edit/jasmine-in-angular-upoavr?file=src/app/app.component.spec.ts