[React Testing] Redux Reducers
Sometimes we want to test our Redux reducers to make sure they work as expected. In this lesson we will walk through setting up some Redux reducer tests for common situations and edge cases.
quoteReducer.js
import {ADD_QUOTE_BY_ID, REMOVE_QUOTE_BY_ID, LIKE_QUOTE_BY_ID, UNLIKE_QUOTE_BY_ID} from './ActionTypes';
export default function quoteReducer(state = [], action) {
function changeLikeCountById(change) {
const newQuotes = state
.map(quote => {
if(quote.id === action.payload.id) {
switch (change) {
case 'increment':
quote.likeCount++;
return quote;
case 'decrement':
if(quote.likeCount > 0) {
quote.likeCount--;
}
return quote;
default:
return quote;
}
}
return quote;
});
return newQuotes;
}
switch(action.type) {
case ADD_QUOTE_BY_ID:
return state
.concat(action.payload);
case REMOVE_QUOTE_BY_ID:
return state
.filter(quote => quote.id !== action.payload.id);
case LIKE_QUOTE_BY_ID:
return changeLikeCountById('increment');
case UNLIKE_QUOTE_BY_ID:
return changeLikeCountById('decrement');
default:
return state;
}
}
quoteReducer.spec.js:
import expect from 'expect';
import {addQuoteById, removeQuoteById, likeQuoteById, unlikeQuoteById} from './quoteActionCreator';
import quoteReducer from './quoteReducer'; describe( 'should render quote component correctly', ()=> { const initQuoteState = ()=> {
return [
{
text: 'Lorem ipsum',
author: 'Jane Doe',
id: 1,
likeCount: 7
},
{
text: 'Ullamco laboris nisi ut aliquip',
author: 'John Smith',
id: 2,
likeCount: 0
}
];
}; it( 'should add quote by id', ()=> { const action = addQuoteById({
text: 'This is a new quote',
author: 'Someone awesome',
id: 3,
likeCount: 0
}); const actual = quoteReducer(initQuoteState(), action); const expected = [
{
text: 'Lorem ipsum',
author: 'Jane Doe',
id: 1,
likeCount: 7
},
{
text: 'Ullamco laboris nisi ut aliquip',
author: 'John Smith',
id: 2,
likeCount: 0
},
{
text: 'This is a new quote',
author: 'Someone awesome',
id: 3,
likeCount: 0
}
]; expect( actual )
.toEqual( expected );
} );
} )
;
quoteActionCreator.js
import {ADD_QUOTE_BY_ID, REMOVE_QUOTE_BY_ID, LIKE_QUOTE_BY_ID, UNLIKE_QUOTE_BY_ID} from './ActionTypes';
export function addQuoteById(payload) {
return {
type: ADD_QUOTE_BY_ID,
payload: payload
};
}
export function removeQuoteById(payload) {
return {
type: REMOVE_QUOTE_BY_ID,
payload: payload
};
}
export function likeQuoteById(payload) {
return {
type: LIKE_QUOTE_BY_ID,
payload: payload
};
}
export function unlikeQuoteById(payload) {
return {
type: UNLIKE_QUOTE_BY_ID,
payload: payload
};
}
[React Testing] Redux Reducers的更多相关文章
- 实例讲解react+react-router+redux
前言 总括: 本文采用react+redux+react-router+less+es6+webpack,以实现一个简易备忘录(todolist)为例尽可能全面的讲述使用react全家桶实现一个完整应 ...
- 【前端】react and redux教程学习实践,浅显易懂的实践学习方法。
前言 前几天,我在博文[前端]一步一步使用webpack+react+scss脚手架重构项目 中搭建了一个react开发环境.然而在实际的开发过程中,或者是在对源码的理解中,感受到react中用的最多 ...
- 【前端,干货】react and redux教程学习实践(二)。
前言 这篇博文接 [前端]react and redux教程学习实践,浅显易懂的实践学习方法. ,上一篇简略的做了一个redux的初级demo,今天深入的学习了一些新的.有用的,可以在生产项目中使用的 ...
- [React] 14 - Redux: Redux Saga
Ref: Build Real App with React #14: Redux Saga Ref: 聊一聊 redux 异步流之 redux-saga [入门] Ref: 从redux-thun ...
- react脚手架改造(react/react-router/redux/eslint/karam/immutable/es6/webpack/Redux DevTools)
公司突然组织需要重新搭建一个基于node的论坛系统,前端采用react,上网找了一些脚手架,或多或少不能满足自己的需求,最终在基于YeoMan的react脚手架generator-react-webp ...
- 结合React使用Redux
前面的两篇文章我们认识了 Redux 的相关知识以及解决了如何使用异步的action,基础知识已经介绍完毕,接下来,我们就可以在React中使用Redux了. 由于Redux只是一个状态管理工具,不针 ...
- React 和 Redux理解
学习React有一段时间了,但对于Redux却不是那么理解.网上看了一些文章,现在把对Redux的理解总结如下 从需求出发,看看使用React需要什么 1. React有props和state pro ...
- 基于 React.js + Redux + Bootstrap 的 Ruby China 示例 (转)
一直学 REACT + METEOR 但路由部分有点问题,参考一下:基于 React.js + Redux + Bootstrap 的 Ruby China 示例 http://react-china ...
- 基于react+react-router+redux+socket.io+koa开发一个聊天室
最近练手开发了一个项目,是一个聊天室应用.项目虽不大,但是使用到了react, react-router, redux, socket.io,后端开发使用了koa,算是一个比较综合性的案例,很多概念和 ...
随机推荐
- tabpagerindictor:mergeReleaseResources FAILED Error:Execution failed for task ':tabpagerindictor:mergeReleaseResources'. > D:\android\adt-bundle-windows-x86_64-20140702\android-open-project-demo-mast
解决:将项目移动到路径少的目录再运行即可 异常日志: D:\android\adt-bundle-windows-x86_64-20140702\android-open-project-demo-m ...
- egrep和grep有什么区别
grep默认不支持正则表达式,egrep默认支持正则表达式,egrep 等于 grep -E 命令.
- 武汉科技大学ACM :1002: A+B for Input-Output Practice (II)
Problem Description Your task is to Calculate a + b. Input Input contains an integer N in the first ...
- TComboBox组件的重要属性
TComboBox组件的重要属性 CharCase--------此属性用于设置编辑框内文字的大小写DropDownCount---此属性用于设置当用户下拉组合框时不需要加滚动条就能显示的项的个数Dr ...
- Android Framework------之ActivityManagerService与Activity之间的通信
研究Android系统的童鞋,想必都已经了解一个Activity的启动过程了.而且在网上,关于Activity的启动的文章非常多,很容易就能找到的.这篇文章的重点放在ActivityManagerSe ...
- Microsoft OLE DB Provider for SQL Server 错误 '80040e21'
我的是因为数据库满了,正在向服务商申请增加数据库容量 原文地址:Microsoft OLE DB Provider for SQL Server 错误 '800作者:欧阳IT记事本 昨天打开网站还正常 ...
- IOS面试攻略
IOS面试攻略(1.0) 2013-10-13 20:58:09| 分类: IOS面试 | 标签:ios知识点总汇 ios面试 |举报|字号 订阅 来自:伊甸网 @ 看到这个关键字,我 ...
- 03 - 替换vtkDataObject中的GetProducerPort()方法 VTK 6.0 迁移
VTK6 引入了许多不兼容的变.其中之一是删除vtkDataObject中所有有关管道的方法.其中的一个方法就是GetProducerPort(). 一般,先前使用这个方法如下例子: vtkPolyD ...
- CentOS下安装postgresql
一.说明 postgresql版本:9.4.1 安装包: postgresql94-server-9.4.1-1PGDG.rhel6.x86_64.rpm postgresql94-libs-9.4. ...
- Android 之形状Drawable
形状Drawable资源允许使用 <shape>标记指定基本形状的尺寸.背景.轮廓线,从而定义这些基本形状. 每个形状都包含一个类型(通过shape属性指定).定义该形状尺寸的属性,以及指 ...