1

I am writing tests for my reducers and would like to compare state before dispatch to state after - actually 'substract' before state from after state

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
        });
    });
jeff
  • 1,169
  • 1
  • 19
  • 44
  • 1
    refer to this: https://stackoverflow.com/questions/8572826/generic-deep-diff-between-two-objects you just need to check the difference bewteen the two objects – Silvio Biasiol Feb 19 '19 at 15:55

2 Answers2

1

You can use equals method like this:

oldStore.equals(newStore); //Returns boolean
Daya
  • 121
  • 1
  • 6
0

I ended up using the deep object diff library, here is my example - I am testing that there are no other changes except email in two occurrences:

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' },
            });
        }); 
jeff
  • 1,169
  • 1
  • 19
  • 44