单元测试redux删除减速器失败:Angular

时间:2018-06-01 19:30:02

标签: angular unit-testing redux state

我正在尝试测试删除婴儿,但我的测试失败并出现以下错误。我不明白为什么一切似乎对我好。当我将其记录到控制台时,我找到它所使用的对象的索引的方式。当我在应用程序中尝试时,我的实际删除也有效。 那可能有什么不对?

删除测试:

  

错误:预期1等于0.

返回状态的函数:

static getInitialUsersState(): UsersState {
            return {
                isBaby: undefined, authType: 'unAuthenticated',
                // mock data 
                babies: [
                    {
                        _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",
                    }],
                sitters: []
            };
        }

单元测试:

describe('register reducer', () => {
    let initialState;
    beforeEach(() => {
        initialState = UsersService.getInitialUsersState();
        deepFreeze(initialState);
    })
it('Should delete a sitter object in the sitters array', () => {
    let afterState = UsersService.getInitialUsersState();
    let deletedBaby;
    deletedBaby = {
        _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 index = afterState.babies.indexOf(deletedBaby);
    if (index !== -1) { afterState.babies.splice(index, 1) }
    let newState = usersReducer(initialState, {
        type: types.UsersActions.DELETE_BABY,
        payload: deletedBaby
    });
    expect(newState.babies.length).toEqual(0)
});

我的用户减速机:

  case UsersActions.DELETED_SITTER:
            return tassign(state, { sitters: state.sitters.filter(sitter => sitter !== action.payload) });

0 个答案:

没有答案