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的更多相关文章

  1. 实例讲解react+react-router+redux

    前言 总括: 本文采用react+redux+react-router+less+es6+webpack,以实现一个简易备忘录(todolist)为例尽可能全面的讲述使用react全家桶实现一个完整应 ...

  2. 【前端】react and redux教程学习实践,浅显易懂的实践学习方法。

    前言 前几天,我在博文[前端]一步一步使用webpack+react+scss脚手架重构项目 中搭建了一个react开发环境.然而在实际的开发过程中,或者是在对源码的理解中,感受到react中用的最多 ...

  3. 【前端,干货】react and redux教程学习实践(二)。

    前言 这篇博文接 [前端]react and redux教程学习实践,浅显易懂的实践学习方法. ,上一篇简略的做了一个redux的初级demo,今天深入的学习了一些新的.有用的,可以在生产项目中使用的 ...

  4. [React] 14 - Redux: Redux Saga

    Ref: Build Real App with React #14: Redux Saga Ref: 聊一聊 redux 异步流之 redux-saga  [入门] Ref: 从redux-thun ...

  5. react脚手架改造(react/react-router/redux/eslint/karam/immutable/es6/webpack/Redux DevTools)

    公司突然组织需要重新搭建一个基于node的论坛系统,前端采用react,上网找了一些脚手架,或多或少不能满足自己的需求,最终在基于YeoMan的react脚手架generator-react-webp ...

  6. 结合React使用Redux

    前面的两篇文章我们认识了 Redux 的相关知识以及解决了如何使用异步的action,基础知识已经介绍完毕,接下来,我们就可以在React中使用Redux了. 由于Redux只是一个状态管理工具,不针 ...

  7. React 和 Redux理解

    学习React有一段时间了,但对于Redux却不是那么理解.网上看了一些文章,现在把对Redux的理解总结如下 从需求出发,看看使用React需要什么 1. React有props和state pro ...

  8. 基于 React.js + Redux + Bootstrap 的 Ruby China 示例 (转)

    一直学 REACT + METEOR 但路由部分有点问题,参考一下:基于 React.js + Redux + Bootstrap 的 Ruby China 示例 http://react-china ...

  9. 基于react+react-router+redux+socket.io+koa开发一个聊天室

    最近练手开发了一个项目,是一个聊天室应用.项目虽不大,但是使用到了react, react-router, redux, socket.io,后端开发使用了koa,算是一个比较综合性的案例,很多概念和 ...

随机推荐

  1. javascript 比较对象(hashcode)

    javascript 对象的比较是比较坑爹的一件事,因为javascript对象比较的是引用地址!当两个内容完全一样的对象比较: var object1={ name:"1234 " ...

  2. oracle 空置排在最后显示

    nulls last select * from emp order by comm select * from emp order by comm desc select * from emp or ...

  3. sql查询语句心得

    1)where 有多个用in ,一个用= 2)自身链接 select A.Sno from S A,S B where A.Sname='a' AND A.City=B.City)) 3)外链接(同时 ...

  4. Qt5-控件-QRadioButton-单选按钮-用于从多个选项中选取一个-单选神器

    #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QRadioButton> ...

  5. sqoop 1.4.4-cdh5.1.2快速入门

    一.快速入门 (一)下载安装 1.下载并解压 wget http://archive.cloudera.com/cdh5/cdh/5/sqoop-1.4.4-cdh5.1.2.tar.gz tar - ...

  6. 转:十条不错的编程观点。(出处:酷 壳 – CoolShell.cn)

    在Stack Overflow上有这样的一个贴子<What’s your most controversial programming opinion?>,翻译成中文就是“你认为最有争议的 ...

  7. wordpress后台打开缓慢的临时解决方法

    解决方法是添加下面的主题在目前的代码在functions.php: //禁用Open Sans class Disable_Google_Fonts { public function __const ...

  8. java获取当前时间

    /////////////////获取时间方法一////////////////////////////// java.util.Date uDate=new java.util.Date(); Sy ...

  9. IE6\ IE7、IE8\9\10和Firefox的hack方式

    #test{color:red;color:red !important;/ Firefox.IE7支持 */_color:red; / IE6支持 */*color:red; / IE6.IE7支持 ...

  10. 关于 "Context" 模式(基于COM思想IUnknown思想)

    有同事很喜欢用Context模式,觉得是自己"首创", 我有些自己的想法, 或者大家可以发表下自己的观点.   什么是Context模式? 23种设计模式中没有这个模式, 是同事自 ...