I am having some trouble understanding why I am getting an error message:
TypeError: Cannot assign to read only property 'description' of object '#'
I know that the principle is that I don't want to modify state in my reducer. Instead, I want to return a new copy of the state.
Here is my reducer:
action TOGGLE_CHECKBOX:
{
let copyOfItems = [...state.items]; // create a new array of items
copyOfItems.forEach(i => i.description = "newDescription");
// return a new copy of the state
return {
...state,
items: copyOfItems
}
}
And here is my Reducer test:
it ('Test that each item description is set', () => {
const state = {
items: [
{ description: "d1" },
{ description: "d2" }
]
}
deepFreeze(state);
expect(MyReducer(state, { type: TOGGLE_CHECKBOX })).toEqual({
items: [
{ description: "newDescription" },
{ description: "newDescription" }
]
});
});
However, I get the above error message. If I remove the deepFreeze
the test passes. This means that I am somehow modifying the original state in some way but I can't figure out why especially because I created a new array of spreaded items.
Any help would be greatly appreciated.