[React] Advanced Epic RxJS pattern with testing

Epic:

复制代码
import { ofType } from 'redux-observable'
import { of, concat, merge, fromEvent, race, forkJoin } from 'rxjs'

const buildAPI = (apiBase, perPage, searchContent) =>
  `${apiBase}?beer_name=${encodeURIComponent(
    searchContent,
  )}&per_page=${perPage}`

const randomApi = (apiBase) => `${apiBase}/random`// getJSON is passing from the dependeniences
export function fetchBeersEpic(action$, state$, { getJSON }) {
  return action$.pipe(
    ofType(SEARCH),
    // avoid too many request to server
    debounceTime(500),
    // Filter out empty search
    filter(({ payload }) => payload.trim() !== ''),
    // Avoid sending the same request payload to server
    distinctUntilChanged(),
    // Get Config State
    withLatestFrom(state$.pipe(pluck('config'))),
    // Ignore the previous request's response
    switchMap(([{ payload }, config]) => {
      // Network reqest
      // This observable can be cancelled by blockers$
      const ajax$ = getJSON(
        buildAPI(config.apiBase, config.perPage, payload),
      ).pipe(
        // Dispatch fulfilled action
        map((resp) => fetchFulfilled(resp)),
        catchError((err) => {
          // If error, dispatch fail action
          return of(fetchFailed(err.response.message))
        }),
      )

      // Canceller
      // Used to cancel the network request when press "Esc" key
      // Or Cancel button was clicked
      // Or this observable can be cancelled by ajax$
      const blockers$ = merge(
        action$.pipe(ofType(CANCEL)),
        fromEvent(document, 'keyup').pipe(
          filter((e) => e.key === 'Escape' || e.key === 'Esc'),
        ),
      ).pipe(
        // Dispatch reset action
        mapTo(reset()),
      )

      // Dispatch setStatus action
      // and wait ajax$ or blockers$, depends on which is faster
      // Faster one will cancel the slower one
      return concat(of(setStatus('pending')), race(ajax$, blockers$))
    }),
  )
}
复制代码

 

Testing:

复制代码
import { TestScheduler } from 'rxjs/testing'...

it('produces correct actions (success)', function() {
  const testScheduler = new TestScheduler((actual, expected) => {
    expect(actual).toEqual(expected)
  })

  testScheduler.run(({ hot, cold, expectObservable }) => {
    const action$ = hot('a', {
      a: search('ship'),
    })
    const state$ = of({
      config: initialState,
    })
    const dependencies = {
      getJSON: (url) => {
        return cold('---a', {
          a: [{ name: 'Beer 1' }],
        })
      },
    }
    const output$ = fetchBeersEpic(action$, state$, dependencies)
    // a: 500ms
    // -: 501ms,
    // b: 502ms
    expectObservable(output$).toBe('500ms a--b', {
      a: setStatus('pending'),
      b: fetchFulfilled([{ name: 'Beer 1' }]),
    })
  })
})

it('produces correct actions (error)', function() {
  const testScheduler = new TestScheduler((actual, expected) => {
    expect(actual).toEqual(expected)
  })

  testScheduler.run(({ hot, cold, expectObservable }) => {
    const action$ = hot('a', {
      a: search('ship'),
    })
    const state$ = of({
      config: initialState,
    })
    const dependencies = {
      getJSON: (url) => {
        return cold('---#', null, {
          response: {
            message: 'oops!',
          },
        })
      },
    }
    const output$ = fetchBeersEpic(action$, state$, dependencies)

    expectObservable(output$).toBe('500ms a--b', {
      a: setStatus('pending'),
      b: fetchFailed('oops!'),
    })
  })
})

it('produces correct actions (reset)', function() {
  const testScheduler = new TestScheduler((actual, expected) => {
    expect(actual).toEqual(expected)
  })

  testScheduler.run(({ hot, cold, expectObservable }) => {
    const action$ = hot('a 500ms -b', {
      a: search('ship'),
      b: cancel(),
    })
    const state$ = of({
      config: initialState,
    })
    const dependencies = {
      getJSON: (url) => {
        return cold('---a', [{ name: 'Beer 1' }])
      },
    }
    const output$ = fetchBeersEpic(action$, state$, dependencies)
    expectObservable(output$).toBe('500ms a-b', {
      a: setStatus('pending'),
      b: reset(),
    })
  })
})
复制代码

 

Config:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { createStore, combineReducers, applyMiddleware, compose } from 'redux'
import { combineEpics, createEpicMiddleware } from 'redux-observable'
import { fetchBeersEpic, randomFetchEpic } from './epics/fetchBeers'
import { beersReducers } from './reducers/beerReducer'
import { configReducer } from './reducers/configReducer'
import { persistEpic, hydrateEpic } from './epics/persist'
import { ajax } from 'rxjs/ajax'
export function configureStore(dependencies = {}) {
  const rootEpic = combineEpics(
    randomFetchEpic,
    fetchBeersEpic,
    persistEpic,
    hydrateEpic,
  )
 
  // Provide platform dependency
  // this make testing easier
  const epicMiddleware = createEpicMiddleware({
    dependencies: {
      getJSON: ajax.getJSON,
      document: document,
      ...dependencies,
    },
  })
 
  // compose reducers into a single root reducer
  const rootReducer = combineReducers({
    beers: beersReducers,
    config: configReducer,
  })
 
  // Enable redux devtools
  const composeEnhancers =
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
 
  const store = createStore(
    rootReducer,
    composeEnhancers(applyMiddleware(epicMiddleware)),
  )
 
  epicMiddleware.run(rootEpic)
  return store
}

  

posted @   Zhentiw  阅读(401)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2018-04-30 [Python] Create a minimal website in Python using the Flask Microframework
2018-04-30 [GraphQL] Apollo React Mutation Component
2017-04-30 [Angular Unit Testing] Shallow Pipe Testing
2017-04-30 [Angular Unit Testing] Testing Pipe
点击右上角即可分享
微信分享提示