[英]How can I mock an update of an object in an array : Unit testing Angular
I am unit test ing my redux reducers , I have tested GET , and CREATE so far however I don't know how to do the UPDATE . 我正在对redux缩减器进行 单元测试 ,已经测试了GET ,并创建了CREATE ,但是到目前为止,我还不知道如何进行UPDATE 。 I can't figure out how to mock it. 我不知道如何模拟它。 I have also added the code for the UPDATE in the reducer below. 我还在下面的reducer中添加了UPDATE的代码。
This is what I have done so far in the test: 到目前为止,这是我在测试中所做的:
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);
})
//update comes here //更新来了
it('Should update a baby object in the babies array', () => {}}
This is how my actual reducer look like: 这是我的实际减速器的样子:
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)] });
try this 尝试这个
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.