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. maven第三章 maven使用入门

    3.1编写pom groupId 一个项目名称 artifactId 子项目(模块)名称 version 开发中的版本,稳定的版本 name 项目名称,方便信息交流 http://news.cnblo ...

  2. CentOS目录结构详解

    CentOS是文件管理系统,在CentOS中所有的程序都以文件形式存储.初学CentOS的朋友需要了解各个挂载点 目录的结构和作用.这样才能更好的去管理系统. CentOS的目录大体上可分为四类:管理 ...

  3. foreach遍历原理(一)

    前言 要使用foreach的遍历的类首先要满足的条件 1. 类要实现公共方法 public IEnumerator GetEnumerator(){},还可以继承IEnumerable接口来实现这个方 ...

  4. css动态样式

    一种 var style=document.createElement("style"); style.type="text/css"; style.appen ...

  5. [转]简述volatile

    volatile int i=10; int j = i; ... int k = i; volatile 告诉编译器i是随时可能发生变化的,每次使用它的时候必须从i的地址中读取,因而编译器生成的可执 ...

  6. Java语言实现简单FTP软件------>FTP软件本地窗口的实现(五)

    1.首先看一下本地窗口的布局效果 2.看一下本地窗口实现的代码框架 2.本地窗口的具体实现代码LocalPanel.java package com.oyp.ftp.panel.local; impo ...

  7. 安装SQL Server 那些事儿

    SQL版本: Microsoft SQL Server2008 R2 RTM - Express with Advanced Services 错误描述: System.Configuration.C ...

  8. jQuery1.9(辅助函数)学习之——.serializeArray();

    .serializeArray();返回一个Array 描述: 将用作提交的表单元素的值编译成拥有name和value对象组成的数组.例如[ { name: a value: 1 }, { name: ...

  9. js获取对象位置的方法

    scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离 scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最 ...

  10. 一个Div在BOdy中上下左右居中

    在body中让一个DIv居中 上下左右 <body> <div style=" width:800px; height:500px; position:absolute; ...