繁体   English   中英

如何比较两个 redux store 状态?

[英]How to compare two redux store states?

我正在为我的减速器编写测试,并想比较分派前的状态与后状态 - 实际上是从状态后“减去”前状态

describe('UpdateAccountData', () => {
    let store;

    describe('Customer', () => {
        beforeEach(() => {
            store = createStore(reducers, customerData, applyMiddleware(sagaMiddleware));
        });

        it('debug store', () => {
            console.log(store.getState());
        });

        it('dispatch change', () => {
            //need to deep copy old state here
            store.dispatch(updateStoredCustomerDetails({ email: 'blabla@blabla.com' }));
            console.log(store.getState());
            //need to substract old state from new state to check that only email has changed and nothing else
        });
    });

您可以像这样使用equals方法:

oldStore.equals(newStore); //Returns boolean

我最终使用了 deep object diff 库,这是我的示例 - 我正在测试除了电子邮件之外没有其他变化发生两次:

import { updatedDiff, deletedDiff, addedDiff } from 'deep-object-diff';  
               .....

        it('updates customer email', () => {
            const before = store.getState();
            store.dispatch(updateStoredCustomerDetails({ email: 'blabla@blabla.com' }));
            const after = store.getState();
            expect(addedDiff(before, after)).toEqual({});
            expect(deletedDiff(before, after)).toEqual({});
            expect(updatedDiff(before, after)).toEqual({
                login: { loginToken: { email: 'blabla@blabla.com' } },
                customer: { email: 'blabla@blabla.com' },
            });
        }); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM