RxJS - Unit testing mergeMap inner Observable

Viewed 977

I have the following code:

  createAsset(asset: Asset): void {
    this._assetsService.uploadFile()
    .pipe(
      mergeMap(_ => this._assetsService.createAsset({
        ...asset,
        extension: 'png'
      })),
      take(1)
    )
    .subscribe(_ => {}, _ => console.error(_));
  }

And I would like to ensure that whenever this function is called, and it calls createAsset with the right parameters.

I did write a UT using Karma and Jasmine:

it('...', async (async () => {
    const assetsService = TestBed.inject(AssetsService);
    const uploadSpy = spyOn(assetsService, 'uploadFile').and.returnValue(of());
    const createSpy = spyOn(assetsService, 'createAsset').and.returnValue(of());

    const extension = 'png';

    const asset = {
      name: 'name',
      extension: null
    };

    component.createAsset(asset);

    await fixture.whenStable();
    expect(uploadSpy).toHaveBeenCalled();
    expect(createSpy).toHaveBeenCalledWith({...asset, extension}); // Error : This is never called
  }));

The inner obversable from the mergeMap createSpy is never called. Is there something I am missing ?

1 Answers
 const uploadSpy = spyOn(assetsService, 'uploadFile').and.returnValue(of());

the issue lies in the mock using a Observable<void> simply replacing it by anything other than void fixed the issue.

Related