[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,算是一个比较综合性的案例,很多概念和 ...
随机推荐
- [JavaScript]plupload多图片上传图片
var uploader = new plupload.Uploader({ //创建实例的构造方法 runtimes: 'html5,flash,silverlight,html4', ...
- (转)关于c#中的事件
原文链接http://blog.csdn.net/joyhen/article/details/8500211 如有不明白的地方欢迎加QQ群14670545 探讨 最近在看委托,然后看到事件,以前一直 ...
- MVC中不能使用原生态的#include ,可替代的解决方案
<!--#include file="../stuff/foo/box.aspx"--> 1.可以用 <%: Html.Partial("~/Views ...
- securecrt简介
SecureCRT是最常用的终端仿真程序,简单的说就是Windows下登录UNIX或Liunx服务器主机的软件,本文主要介绍SecureCRT的使用方法和技巧 VanDyke CRT 和 VanDyk ...
- Ecshop布局参考图
文章列表页: article_cat.dwt 文章内容页: article.dwt 商品品牌页: brand.dwt 所有分类页: catalog.dwt 商品列表页: category.dwt 商品 ...
- ie6,7下js动态加载图片不显示错误
ie6,7下js动态加载图片不显示错误 先描述一下出现这种匪夷所思bug的背景: 我在页面加载的时候加载一堆小缩略图,<a href="javascript:void(0);" ...
- smarty 自定义函数
自定义函数:<{方法名称}> 在lib/plugins中新建文件,命名方式是固定的:function.方法名称.php 或者 block.方法名称.php 1.<{literal}& ...
- cmd中用PING命令时,出现'Ping'不是内部或外部命令
在cmd中用PING命令时,出现'Ping' 不是内部或外部命令,也不是可运行的程序或批处理文件.先了解一下内容:1.可执行文件.命令文件和批处理文件以.exe或者.com或者.bat为扩展名的文件分 ...
- Statement执行DQL语句(查询操作)
import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import org.junit.T ...
- lucene分词多种方法
目前最新版本的lucene自身提供的StandardAnalyzer已经具备中文分词的功能,但是不一定能够满足大多数应用的需要.另外网友谈的比较多的中文分词器还有:CJKAnalyzerChinese ...