如何比较两个Redux存储状态?

时间:2019-02-19 15:49:45

标签: javascript redux react-redux jestjs immutable.js

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

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

2 个答案:

答案 0 :(得分:1)

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

oldStore.equals(newStore); //Returns boolean

答案 1 :(得分:0)

我最终使用了深层对象差异库,这是我的示例-我正在测试除两次出现的电子邮件外没有其他更改:

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' },
            });
        });