[Redux-Observable && Unit Testing] Use tests to verify updates to the Redux store (rxjs scheduler)
In certain situations, you care more about the final state of the redux store than you do about the particular stream of events coming out of an epic. In this lesson we explore a technique for dispatching actions direction into the store, having the epic execute as they would normally in production, and then assert on the updated store’s state.
To test a reducer, what we need to do is actually dispatch as action with its payload.
store.dispatch(action);
But before that, we need to get our 'store' configuration in the test.
configureStore.js:
import {createStore, applyMiddleware, compose} from 'redux';
import reducer from './reducers';
import { ajax } from 'rxjs/observable/dom/ajax';
import {createEpicMiddleware} from 'redux-observable';
import {rootEpic} from "./epics/index";
export function configureStore(deps = {}) {
const epicMiddleware = createEpicMiddleware(rootEpic, {
dependencies: {
ajax,
...deps
}
});
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
return createStore(
reducer,
composeEnhancers(
applyMiddleware(epicMiddleware)
)
);
}
index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import {Provider} from 'react-redux';
import {configureStore} from "./configureStore"; const store = configureStore(); ReactDOM.render(
<Provider store={store}>
<App />
</Provider>
, document.getElementById('root'));
The configureStore.js exports function which create a store, we can import normally in the test file.
Now for example, we want to dispatch this action:
export function searchBeers(query) {
return {
type: SEARCHED_BEERS,
payload: query
}
}
Epic:
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)
.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);
'decountTime' make the Epic async!
To verifiy the result is correct, we can do
const store = configureStore(deps);
const action = searchBeers('name');
store.dispatch(action);
expect(store.getState().beers.length).toBe();
BUT, actually this test code won't work, because the 'decountTime' in the epic, makes it as async opreation. Reducer expects everything happens sync...
One way can test it by using 'scheduler' from rxjs.
import {Observable} from 'rxjs';
import {VirtualTimeScheduler} from 'rxjs/scheduler/VirtualTimeScheduler';
import {searchBeers} from "../actions/index";
import {configureStore} from "../configureStore";
it('should perform a search (redux)', function () {
const scheduler = new VirtualTimeScheduler();
const deps = {
scheduler,
ajax: {
getJSON: () => Observable.of([{name: 'shane'}])
}
};
const store = configureStore(deps);
const action = searchBeers('shane');
store.dispatch(action);
scheduler.flush();
expect(store.getState().beers.length).toBe();
});
And we need to modifiy the epic:
.debounceTime(, deps.scheduler)
Take away, we can test async oprations by using 'scheduler' from rxjs.
-------------------FUll Code------------
[Redux-Observable && Unit Testing] Use tests to verify updates to the Redux store (rxjs scheduler)的更多相关文章
- Unit Testing with NSubstitute
These are the contents of my training session about unit testing, and also have some introductions a ...
- [Java Basics3] XML, Unit testing
What's the difference between DOM and SAX? DOM creates tree-like representation of the XML document ...
- Unit Testing PowerShell Code with Pester
Summary: Guest blogger, Dave Wyatt, discusses using Pester to analyze small pieces of Windows PowerS ...
- C# Note36: .NET unit testing framework
It’s usually good practice to have automated unit tests while developing your code. Doing so helps y ...
- Unit Testing of Spring MVC Controllers: “Normal” Controllers
Original link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...
- Unit Testing, Integration Testing and Functional Testing
转载自:https://codeutopia.net/blog/2015/04/11/what-are-unit-testing-integration-testing-and-functional- ...
- Javascript单元测试Unit Testing之QUnit
body{ font: 16px/1.5em 微软雅黑,arial,verdana,helvetica,sans-serif; } QUnit是一个基于JQuery的单元测试Uni ...
- [Unit Testing] AngularJS Unit Testing - Karma
Install Karam: npm install -g karma npm install -g karma-cli Init Karam: karma init First test: 1. A ...
- C/C++ unit testing tools (39 found)---reference
http://www.opensourcetesting.org/unit_c.php API Sanity AutoTest Description: An automatic generator ...
随机推荐
- Linux下的压缩与解压缩
1 .gz 1)压缩 root@xiaohuang-virtual-machine:/home/xiaohuang/桌面/hellow/hellow# gzip 2.txt 3.txt root@xi ...
- vi-vim和linux常用快捷键
移动光标 上 k 下 j 左 h 右 l 移动光标到当前行行尾首 ^ 移动光标到当前行行尾 $ 移动到文件的第一行 gg 移动到文件的最后一行 G 移动到第1 ...
- Android向unity发送消息
有些时候需要Android向unity发送消息,有两种方法实现,一.通过unity再带的消息机制,二.通过注册回调的方式. 一.通过UnityPlayer.UnitySendMessage():方法 ...
- ECNUOJ 2616 游黄山
游黄山 Time Limit:1000MS Memory Limit:65536KB Total Submit:165 Accepted:52 Special Judge Description Po ...
- Java Web乱码分析及解决方式(二)——POST请求乱码
引言 GET请求的本质表现是将请求參数放在URL地址栏中.form表单的Method为GET的情况.參数会被浏览器默认编码,所以乱码处理方案是一样的. 对于POST请求乱码.解决起来要比GET简单.我 ...
- 在kettle中实现数据验证和检查
在kettle中实现数据验证和检查 在ETL项目,输入数据通常不能保证一致性.在kettle中有一些步骤能够实现数据验证或检查.验证步骤能够在一些计算的基础上验证行货字段:过滤步骤实现数据过滤:jav ...
- 15:Challenge 11(主席树裸题)
总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 262144kB 描述 给一个长为N的数列,有M次操作,每次操作是以下两种之一: (1)修改数列中的一个数 (2)求 ...
- POSTGRESQL NO TABLE
POSTGRESQL EXTENDING SQL GRIGGER PROCEDURAL
- <QT障碍之路>qt中使用串口类接收数据不完整
问题:当用QT中的serial->readAll()的时候,不会把全部的数据一次性都读取出来,而是阶段性的.原因是因为当串口有信号时候,readyRead()信号就会被抛出,那么一帧完整的数据帧 ...
- Reference Counting GC (Part one)
目录 引用计数法 计数器值的增减 new_obj()和update_ptr()函数 new_obj()生成对象 update_ptr()更新指针ptr,对计数器进行增减 优点 可即可回收垃圾 最大暂停 ...