如何模拟数组中对象的更新:单元测试角度



我正在对我的redux reducer进行单元测试,我已经测试GETCREATE,但我不知道如何进行更新。我不知道如何嘲笑它。我还在下面的减速器中添加了UPDATE的代码。

这是我到目前为止在测试中所做的:

describe('register reducer', () => {
let initialState;
beforeEach(() => {
initialState = UsersService.getInitialUsersState();
deepFreeze(initialState);
})
it('should return the initial state', () => {
expect(usersReducer(undefined, [])).toEqual(initialState);
});
it('Should add a new baby object to the babies array', () => {
// create 'mock' of initial state
// add baby by calling reducer function
// check that state is correct. Use deep freeze to check no mutations.
let afterState = UsersService.getInitialUsersState();
let baby = {
firstname: 'Peter',
lastname: 'Petursson',
username: 'test baby 1',
birthDate: new Date(2018, 0, 1),
area: 'Copenhagen',
rating: []
};
afterState.babies.push(baby);
let newState = usersReducer(initialState, {
type: types.UsersActions.CREATED_BABY,
payload: baby
});
expect(newState.babies.length).toEqual(1);
expect(newState).toEqual(afterState);
});
it('Should get the babies array', () => {
let afterState = UsersService.getInitialUsersState();
let babies = [{
_id: "5ad228ffdc32f1a42f5d3259",
firstname: "Oliver",
lastname: "Hultgren",
username: "o@h",
birthDate: new Date(2018, 0, 1),
area: "København Ø",
rating: [],
userType: "baby",
dataClient: "Julia"
},
{
_id: "5ad229d7dc32f1a42f5d325a",
firstname: "Oli",
lastname: "Hult",
username: "o.h@dk",
birthDate: new Date(2018, 0, 1),
area: "København V",
rating: [],
userType: "baby",
dataClient: "Julia",
}, {
_id: "5ad309337cbda3002a7e92e0",
firstname: "Jenny",
lastname: "Lopez",
username: "jl@com",
birthDate: new Date(2018, 0, 1),
area: "København Ø",
rating: [],
userType: "baby",
dataClient: "Julia",
}]
afterState.babies = babies;
let newState = usersReducer(initialState, {
type: types.UsersActions.RECEIVED_BABIES,
payload: babies
});
expect(newState).toEqual(afterState);
})

更新来了

it('Should update a baby object in the babies array', () => {}}

这是我的实际减速器的样子:

case UsersActions.UPDATED_BABY:
let indexBaby = state.babies.findIndex(baby => { return baby._id === action.payload._id });
return tassign(state, { babies: [...state.babies.slice(0, indexBaby), action.payload, ...state.babies.slice(indexBaby + 1)] });

试试这个

it('Should add a new baby object to the babies array', () => {
let afterState = UsersService.getInitialUsersState();
let babies = [{
_id: "5ad228ffdc32f1a42f5d3259",
firstname: "Oliver",
lastname: "Hultgren",
username: "o@h",
birthDate: new Date(2018, 0, 1),
area: "København Ø",
rating: [],
userType: "baby",
dataClient: "Julia"
},
{
_id: "5ad229d7dc32f1a42f5d325a",
firstname: "Oli",
lastname: "Hult",
username: "o.h@dk",
birthDate: new Date(2018, 0, 1),
area: "København V",
rating: [],
userType: "baby",
dataClient: "Julia",
}];
let updatedBaby = {
_id: "5ad229d7dc32f1a42f5d325a",
firstname: "ChangedName", // change the name for example
lastname: "Hult",
username: "o.h@dk",
birthDate: new Date(2018, 0, 1),
area: "København V",
rating: [],
userType: "baby",
dataClient: "Julia",
}
let newState = usersReducer(initialState, {
type: types.UsersActions.UPDATED_BABY,
payload: updatedBaby
});
expect(newState[1].firstname).toEqual('ChangedName'); // to check that update is working and that you have the new firstname

最新更新