使用jest和jasmine-marbles测试ngrx效果时的问题

问题描述 投票:1回答:1

我正在使用jestjasmine-marbles来测试我的ngrx-effects。到目前为止一切都那么好,但我有一个特殊情况,我需要使用withLatestFrom访问Store里面这样的效果:

@Effect()
createDataSourceSuccess$ = this.actions$
  .ofType<sourceActions.CreateDataSourceSuccess>(
    sourceActions.DataSourceActionTypes.CreateDataSourceSuccess
  )
  .pipe(
    map(action => action.dataSource),
    withLatestFrom(this.store.select(getSourceUploadProgress)),
    switchMap(([source, progress]: [DataSource, UploadProgress]) =>
     of(
        new sourceActions.StartSourceUploadProgress({
          id: source.fileId,
          uploadProgress: progress,
        })
      )
    )
  );

我也确实设置了我的测试:

it('should return StartSourceUploadProgress for CreateDataSourceSuccess', () => {
  const dataSource = dataSources[0];
  const action = new dataSourceActions.CreateDataSourceSuccess(dataSource);
  const outcome = new dataSourceActions.StartSourceUploadProgress({
    id: dataSource.fileId,
    uploadProgress: null,
  });

  store.select = jest.fn(_selector => of(null));

  actions.stream = hot('-a-', { a: action });
  const expected = cold('--b', { b: outcome });

  expect(effects.createDataSourceSuccess$).toBeObservable(expected);
});

我还注意到,我成功地为StoreActions设置了模拟,因为所有其他测试工作正常。这与其他人之间的唯一区别是StorewithLatestFrom不存在于这些效果中。

最后这是我得到的错误输出:

 DataSourceEffects › should return StartSourceUploadProgress for CreateDataSourceSuccess

    TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.

有什么想法吗?

angular ngrx ngrx-effects jasmine-marbles
1个回答
2
投票

这个解决方案适合我

it('should return StartSourceUploadProgress for CreateDataSourceSuccess', () => {
  const dataSource = dataSources[0];
  const action = new dataSourceActions.CreateDataSourceSuccess(dataSource);
  const outcome = new dataSourceActions.StartSourceUploadProgress({
    id: dataSource.fileId,
    uploadProgress: null,
  });

  actions.stream = hot('-a-', { a: action });
  const expected = cold('--b', { b: outcome });

  store.select = jest.fn().mockImplementationOnce(() => of(new SourceUploadProgress()));

  expect(effects.createDataSourceSuccess$).toBeObservable(expected);
});
© www.soinside.com 2019 - 2024. All rights reserved.