zoukankan      html  css  js  c++  java
  • [Redux-Observable && Unit Testing] Mocking an ajax request when testing epics

    Often in unit tests we are focussing on the logic involved in crafting a network request, & how we respond to the result. The external service is unlikely to be under our control, so we need a way to ‘mock’ the Ajax request in a way that allows us to focus on the logic. In this lesson we’ll see how we can pass in dependencies into epics to make testing things Ajax requests easier.

    In a real world React app, for one epic, we might have some dependecies. For example, ajax call. To make it easy for testing, we can make those deps as injectable deps.

    When creating root epic:

    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 }
    });

    Using it in Epic:

    // Notice the third argument is our injected dependencies!
    const fetchUserEpic = (action$, store, { getJSON }) =>
      action$.ofType('FETCH_USER')
        .mergeMap(({ payload }) =>
          getJSON(`/api/users/${payload}`)
            .map(response => ({
              type: 'FETCH_USER_FULFILLED',
              payload: response
            }))
        );

    ---------------Test example ---------------------

    index.js, root setup

    import {createStore, applyMiddleware, compose} from 'redux';
    import {Provider} from 'react-redux';
    import reducer from './reducers';
    import { ajax } from 'rxjs/observable/dom/ajax';
    
    import {createEpicMiddleware} from 'redux-observable';
    import {rootEpic} from "./epics/index";
    
    const epicMiddleware = createEpicMiddleware(rootEpic, {
      dependencies: {
        ajax
      }
    });
    
    const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
    
    const store = createStore(
      reducer,
      composeEnhancers(
        applyMiddleware(epicMiddleware)
      )
    );

    Epic function:

    import {Observable} from 'rxjs';
    import {combineEpics} from 'redux-observable';
    import {CANCEL_SEARCH, receiveBeers, searchBeersError, searchBeersLoading, SEARCHED_BEERS} from "../actions/index";
    
    const beers  = `https://api.punkapi.com/v2/beers`;
    const search = (term) => `${beers}?beer_name=${encodeURIComponent(term)}`;
    
    export function searchBeersEpic(action$, store, deps) {
      return action$.ofType(SEARCHED_BEERS)
        .debounceTime(500, deps.scheduler)
        .filter(action => action.payload !== '')
        .switchMap(({payload}) => {
    
          // loading state in UI
          const loading = Observable.of(searchBeersLoading(true));
    
          // external API call
          const request = deps.ajax.getJSON(search(payload))
            .takeUntil(action$.ofType(CANCEL_SEARCH))
            .map(receiveBeers)
            .catch(err => {
              return Observable.of(searchBeersError(err));
            });
    
          return Observable.concat(
            loading,
            request,
          );
        })
    }
    
    export const rootEpic = combineEpics(searchBeersEpic);

    Test code:

    import {Observable} from 'rxjs';
    import {ActionsObservable} from 'redux-observable';
    import {searchBeersEpic} from "./index";
    import {RECEIVED_BEERS, searchBeers, SEARCHED_BEERS_LOADING} from "../actions/index";
    
    it.only('should perform a search', function () {
      const action$ = ActionsObservable.of(searchBeers('shane'));
    
      const deps = {
        ajax: {
          getJSON: () => Observable.of([{name: 'shane'}])
        }
      };
    
      const output$ = searchBeersEpic(action$, null, deps);
    
      output$.toArray().subscribe(actions => {
        expect(actions.length).toBe(2);
    
        expect(actions[0].type).toBe(SEARCHED_BEERS_LOADING);
        expect(actions[1].type).toBe(RECEIVED_BEERS);
      });
    });

    Refs: Link

  • 相关阅读:
    SQL Server 存储过程语法及实例
    22个开源的PHP框架
    WAYOS路由WEB认证写入工具,有保存密码、提示日期时间星期及提醒的功能
    WAYOS使用定时开关来重启的,悲哀了吧
    WAYOS BCM扩展多WAN口继续研究,已实现扩展至N个WAN口,并成功在线了
    WAYOS免拉黑服务器版已开发成功,可在远程同时控制多台WAYOS,全新处理内核
    WAYOS 免拉黑工具全面测试成功,确定在ISP、PC甚至在BCM都获得成功,支持官方版本,真正的未动WAYOS的破解
    海蜘蛛V8想转WAYOS的用户有福了,用户数据转换工具出来了
    安网SECNET的机器解密——wayos的ODM产品,帮大家解除相关的疑惑
    WAYOS BCM版扩展WAN口研究
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7683341.html
Copyright © 2011-2022 走看看