博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[React Testing] Redux Reducers
阅读量:6114 次
发布时间:2019-06-21

本文共 3614 字,大约阅读时间需要 12 分钟。

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    };}

 

转载地址:http://cgpka.baihongyu.com/

你可能感兴趣的文章
ABP实战--集成Ladp/AD认证
查看>>
存储过程
查看>>
phpcms v9栏目列表调用每一篇文章内容方法
查看>>
python 自定义信号处理器
查看>>
luov之SMTP报错详解
查看>>
软件概要设计做什么,怎么做
查看>>
dwr
查看>>
java的特殊符号
查看>>
word2010中去掉红色波浪线的方法
查看>>
fabric上下文管理器(context mangers)
查看>>
JQuery-EasyUI Datagrid数据行鼠标悬停/离开事件(onMouseOver/onMouseOut)
查看>>
并发和并行的区别
查看>>
[20170410]11G ora_sql_txt是否有效.txt
查看>>
php小知识
查看>>
数据库连接(直接用)
查看>>
Eureka 2.0 开源流产,真的对你影响很大吗?
查看>>
[日推荐]『驾考宝典App』学车驾考必过宝典
查看>>
spring之ioc原理
查看>>
SpringMVC、Tomcat怎样完成一次Http请求的?
查看>>
mybatis中获取sqlSession的源码分析
查看>>