[React Testing] The Redux Store - Multiple Actions

When using Redux, we can test that our application state changes are working by testing that dispatching actions to the store creates our expected output. In this lesson we will run a few realistic actions back to back (as if the user is using the app) and then test that the state tree looks as we expect it to. These types of tests that ensure all of your redux logic is working as expected give you a lot of value for not too much effort (they test your entire app's state in one big swoop). You may also find it useful to add more granular/individual tests for your reducers and/or actions, which we will cover in other lessons in this course.

 

复制代码
import expect from 'expect';
import {store} from './store';

describe('store', () => {

    it('should initialize', () => {
        const actual = store.getState();
        const expected = {
            quotes: [],
            theme: {
                color: '#5DC4C6'
            }
        };
        expect(actual).toEqual(expected);
    });

    it('should work with a series of actions', () => {

        const actions = [
            {
                type: 'ADD_QUOTE_BY_ID',
                payload: {
                    text: 'The best way to cheer yourself up is to try to cheer somebody else up.',
                    author: 'Mark Twain',
                    id: 1,
                    likeCount: 24
                }
            },
            {
                type: 'ADD_QUOTE_BY_ID',
                payload: {
                    text: 'Whatever you are, be a good one.',
                    author: 'Abraham Lincoln',
                    id: 2,
                    likeCount: 0
                }
            },
            {
                type: 'REMOVE_QUOTE_BY_ID',
                payload: {id: 1}
            },
            {
                type: 'LIKE_QUOTE_BY_ID',
                payload: {id: 2}
            },
            {
                type: 'LIKE_QUOTE_BY_ID',
                payload: {id: 2}
            },
            {
                type: 'UNLIKE_QUOTE_BY_ID',
                payload: {id: 2}
            },
            {
                type: 'UPDATE_THEME_COLOR',
                payload: {color: '#777777'}
            }
        ];

        actions.forEach(action => store.dispatch(action));

        const actual = store.getState();
        const expected = {
            quotes: [
                {
                    text: 'Whatever you are, be a good one.',
                    author: 'Abraham Lincoln',
                    id: 2,
                    likeCount: 1
                }
            ],
            theme: {
                color: '#777777'
            }
        };

        expect(actual).toEqual(expected);
    });
});
复制代码

 

posted @   Zhentiw  阅读(235)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示