I am trying to convert a test that uses a promise to now instead use an observable...
previously I would do something like this in a beforeEach
to give me access to the promise's resolve:
...
let apiCall: (params: ApiParams) => Promise<any>;
let apiCallResolve: (response: any) => void;
beforeEach((done) => {
const promise = new Promise((resolve: (response: any) => void, reject: (response: any) => void) => {
apiCallResolve = resolve;
done();
});
myService.setApiCall(() => { return promise; });
...
Then I could have my test do something like:
it('does a super cool thing', () => {
apiCallResolve({ some: 'magical', data: '!!!' });
expect(myService.toHaveDoneSomethingRadical).toEqual(true);
});
This works fine, but as soon as I change this to an observable like:
beforeEach((done) => {
const observable = new Observable(subscriber => {
apiCallResolve = subscriber.next;
done();
});
When my test tries to resolve the data by calling apiCallResolve
, nothing seems to happen.. The real world code is basically doing:
const observable = this.apiCall(params);
observable.subscribe((response: ApiResponse) => {
console.log('hello!!!'); // <-- I never see this
});
Isn't next
the thing that would be somewhat-equivalent to calling a promise's resolve
function? Is there another way I can do this?