簡體   English   中英

無法測試Redux可觀察到的史詩

[英]Unable to test redux-observable epic

我打算為以下史詩寫單元測試

// Actions
const actionCreator = actionCreatorFactory('PARENT_DIRECTORY');
export const fetchPage = actionCreator.async<Page, ParentPage>('FETCH_PAGE');

export const fetchParentDirectoryEpic: Epic = action$ =>
  action$.pipe(
    filter(fetchPage.started.match),
    mergeMap((action) => {
      return getDirectoryPage(action.payload).pipe(
        map(response => fetchPage.done({ params: action.payload, result: response.response })),
        catchError(error => of(fetchPage.failed({ params: action.payload, error: error })))
      );
    })
  );

我嘲笑了getDirectoryPage,如下所示:

import { AjaxResponse, AjaxError } from 'rxjs/ajax';
import { Observable, of } from 'rxjs';

export function getDirectoryPage(page: any): Observable<AjaxResponse> {
  switch (page.index) {
    case 0:
      return Observable.create({'data': [], page: 0, pages: 1});
    default:
      return Observable.create(observer => {
        return new AjaxError('Something bad happened!', null, null);
      });
  }
}

以下是我的單元測試的樣子-

describe('fetchParentDirectoryEpic Epic', () => {
    it('dispatches the correct actions when it is successful', async (done) => {
        const expectedOutputAction = outputAction;

        fetchParentDirectoryEpic(inputAction, initialState, null)
            .subscribe(actualOutputAction => {
                expect(actualOutputAction).toBe(expectedOutputAction)
                done()
            }
        );
    });
});

問題在於,對fetchParentDirectoryEpic(inputAction, initialState, null)的調用會導致產生一個沒有Subscribe方法的Observable。 據我了解,該方法可用於ActionObservable但無法使用有效負載創建其實例。

問題與我如何創建ExpectedOutputAction有關。 它應該是一個Action而不是ActionObservable

在按照以下方式設置了ExpectedOutputAction之后,測試工作得很好-

expectedOutputAction = {
  type: fetchPage.done.type, 
  result: {'data': [], page: 0, pages: 1}, 
  params: inputAction.payload
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM