ofType 不是使用 rxjs 弹珠图进行测试的函数

ofType is not a function in testing with rxjs marble diagrams

我正在使用 rxjs 6 和 redux-observable 1 并为 epic 编写测试

const signInEpic = action$ =>
  action$
    .ofType(authActions.signIn)
    .pipe(
      switchMap(mapSignInAction$)
    )

我正在使用 TestScheduler 通过弹珠图进行测试,当 运行 测试它时 return 错误 action$.ofType is not a function

测试:

import { TestScheduler } from 'rxjs/testing'

const scheduler = new TestScheduler((actual, expected) => {
  expect(actual).toEqual(expected)
})

scheduler.run(({ cold, hot, expectObservable }) => {
    const action$ = hot('-a', { a: { type: 'HFJKDHF' } })
    const state$ = null
    const output$ = signInEpic(action$, state$)

    expectObservable(output$).toBe('--b', {
      b: actions.signInSuccess(response)
    })
  })

错误是正确的,因为您创建了 hot() Observable,然后将其传递给 signInEpic 方法。但是 .ofType 在 Observable class 上不存在,这个方法是 redux-observable.

特有的

通过快速查看源代码,您可以自己创建一个模拟 $actions Observable:

https://github.com/redux-observable/redux-observable/blob/master/src/ActionsObservable.js

例如像这样:

import { ActionsObservable } from 'redux-observable';

scheduler.run(({ cold, hot, expectObservable }) => {
  const source = hot('-a', { a: { type: 'HFJKDHF' } });
  const actions$ = ActionsObservable.of(source);

  const state$ = null
  const output$ = signInEpic(action$, state$)

  expectObservable(output$).toBe('--b', {
    b: actions.signInSuccess(response);
  });
});

相反 action$.ofType 需要使用 action$.pipe(ofType(...), ...)

import { ofType } from 'redux-observable'

action$
    .pipe(
      ofType(authActions.signIn),
      switchMap(mapSignInAction$)
    )