Reputation: 53
I need to deep freeze the whole redux state in react redux application using deep-freeze library to check for any mutation on state. How should I use deep-freeze with react redux app to achieve this?
Upvotes: 3
Views: 3157
Reputation: 736
I'm going to assume you mean you're looking to use deep-freeze as a testing tool for changes (as I'm not sure why you would want to freeze the entire state). All the while there a handful of examples out there.
Typically you use deep-freeze with expect and you focus on your reducers because reducers are given a state and they return an expected state.
A typical test usually looks something like this (taken from here):
// a test for the reducer
const testReducer = () => {
const stateBefore = { value: 'foo' };
const action = {
type: 'SET_STATE',
value: 'bar'
};
const stateAfter = { value: 'bar' };
// cannot modify
deepFreeze(stateBefore);
deepFreeze(action);
expect(store(stateBefore, action)).toEqual(stateAfter);
}
In this way, you're verifying that actions taken do not alter your state in any way.
Egghead.io also has a good video on this here.
Of course, the usage doesn't have to be limited to tests; you could introduce deep-freeze as an assertion tool throughout your code as well I supposed if you want. I've not personally got any experience doing that.
Upvotes: 5