Как я могу издеваться над обновлением объекта в массиве: модульное тестирование Angular - PullRequest
0 голосов
/ 29 мая 2018

Я модульный тест с моими редукторными редукторами , я тестировал GET и CREATE , однако пока незнать, как сделать ОБНОВЛЕНИЕ .Я не могу понять, как издеваться над этим.Я также добавил код для ОБНОВЛЕНИЕ в редукторе ниже.

Это то, что я сделал до сих пор в тесте:

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)] });

1 Ответ

0 голосов
/ 31 мая 2018

попробуйте

 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...