I have this object below, I want to test that when i perform an action to change the state of the group
key in the state that it updates correctly. As you can see there are way more key value pairs in this object than just group
so I would like to spread the initialState
object and write my test changing only the group
value.
Here is the initialState:
export const initialState = {
testing: false,
filters: {
start: moment()
.startOf("month")
.format(),
end: moment()
.endOf("month")
.format(),
city: "",
group: "daily"
},
locations: []
};
My unit test:
test("It performs the setGroup action correctly", () => {
const active = "month";
const action = setGroup(active);
const state = reducer(
{
...initialState,
filters: {
...initialState.filters,
group: "daily"
}
},
action
);
expect(state).toEqual({
...initialState,
filters: {
...initialState.filters,
group: "month"
}
});
});
This test works however I want to know why I have to spread the initialstate
once, and then again within the filters object I have to spread the initialState.filters
again just to manipulate the month.
In my head this should work fine :
const state = reducer(
{
...initialState,
filters: {
group: "daily"
}
},
action
);
My question is why do I need to put the line initialState.filters
to make this test viable. Please can someone help me understand? I have done research but can't find anything.