Redux-observable:史诗的开玩笑测试失败

Osc*_*car 1 unit-testing reactjs jestjs redux-observable

我按照文档中的步骤来测试史诗.

...
store.dispatch({ type: FETCH_USER });

expect(store.getActions()).toEqual([
   { type: FETCH_USER },
   { type: FETCH_USER_FULFILLED, payload }
]);
...
Run Code Online (Sandbox Code Playgroud)

但是我失败了,因为后来接到了第二个动作.

Test failed
    Expected value to equal:
      [{"type": "FETCH_USER"}, {"type": "FETCH_USER_FULFILLED", "payload": [some]}]
    Received:
      [{"type": "FETCH_USER"}]

    Difference:

    - Expected
    + Received

    @@ -1,20 +1,5 @@
     Array [
       Object {"type": "FETCH_USER"},
       Object {"type": "FETCH_USER_FULFILLED", "payload": [some]} ] // this is what should be.
Run Code Online (Sandbox Code Playgroud)

所以我想我应该知道派遣何时完成或类似的事情.我怎么解决这个问题?

我使用了fetch()和Rx.Observable.fromPromise而不是ajax.getJSON()

这是我的史诗.

const fetchUserEpic = (action$) =>
  action$
    .ofType(FETCH_USER)
    .mergeMap(() => {
      return Rx.Observable.fromPromise(api.fetchUser())
        .map((users) => ({
          type: FETCH_USER_FULFILLED,
          payload: { users }
        }))
        .catch((error) => Rx.Observable.of({
          type: FETCH_USER_ERROR,
          payload: { error }
        }))
        .takeUntil(action$.ofType(FETCH_USER_CANCELLED))
    })
Run Code Online (Sandbox Code Playgroud)

jay*_*lps 5

原因是承诺总是在下一个微任务中解决,因此你api.fetchUser()不会同步发射.

您需要模拟它,使用类似的东西Promise.resolve().then(() => expect(store.getActions).toEqual(...)等到下一个微任务,或者您可以尝试直接测试您的史诗而不使用redux.

it('Epics with the appropriate input and output of actions', (done) => {
  const action$ = ActionsObservable.of({ type: 'SOMETHING' });

  somethingEpic(action$, store)
    .toArray() // collects everything in an array until our epic completes
    .subscribe(actions => {
      expect(actions).to.deep.equal([
        { type: 'SOMETHING_FULFILLED' }// whatever actions
      ]);

      done();
    });
});
Run Code Online (Sandbox Code Playgroud)

当我(或其他人)有时间编写时,这将是我们在文档中首选的测试故事.因此,我们不是在测试中使用redux和中间件,而是直接使用我们自己的模拟调用epic函数.更简单,更清洁.

通过这种方法,我们可以利用redux-observable的新依赖注入功能:https://redux-observable.js.org/docs/recipes/InjectingDependenciesIntoEpics.html


import { createEpicMiddleware, combineEpics } from 'redux-observable';
import { ajax } from 'rxjs/observable/dom/ajax';
import rootEpic from './somewhere';

const epicMiddleware = createEpicMiddleware(rootEpic, {
  dependencies: { getJSON: ajax.getJSON }
});
Run Code Online (Sandbox Code Playgroud)
// Notice the third argument is our injected dependencies!
const fetchUserEpic = (action$, store, { getJSON }) =>
  action$.ofType('FETCH_USER')
    .mergeMap(() =>
      getJSON(`/api/users/${payload}`)
        .map(response => ({
          type: 'FETCH_USER_FULFILLED',
          payload: response
        }))
    );
Run Code Online (Sandbox Code Playgroud)
import { ActionsObservable } from 'redux-observable';
import { fetchUserEpic } from './somewhere/fetchUserEpic';

const mockResponse = { name: 'Bilbo Baggins' };
const action$ = ActionsObservable.of({ type: 'FETCH_USERS_REQUESTED' });
const store = null; // not needed for this epic
const dependencies = {
  getJSON: url => Observable.of(mockResponse)
};

// Adapt this example to your test framework and specific use cases
fetchUserEpic(action$, store, dependencies)
  .toArray() // buffers all emitted actions until your Epic naturally completes()
  .subscribe(actions => {
    assertDeepEqual(actions, [{
      type: 'FETCH_USER_FULFILLED',
      payload: mockResponse
    }]);
  });
Run Code Online (Sandbox Code Playgroud)