IvanInvanov
@IvanInvanov
Новичок

Как написать unit test на функцию reducer?

Я новичок в модульном тестировании. У меня проблема, мне нужно написать тест, который бы тестировал reducer. Я что-то написал сам, но из-за того, что я использую localStorage в initialState, тест у меня не работает. Ошибка звучит примерно так: TypeError: localStorage.getItem is not a function. Я прошу вас помочь мне написать этот тест.

Мой reducer:

import actionTypes from "...";
import en from '...'
import ru from '...'

const translates = {en, ru};

export const getLanguage = () => {
    const currentLocale = localStorage.getItem('language');
    return translates[currentLocale ? currentLocale : 'en'];
};

const initialState = {
    dictionary: getLanguage(),
};

export const localeReducer = (state = initialState, action) => {
    switch (action.type) {
        case actionTypes.SET_LOCALE: return {...state, dictionary: translates[action.payload]};
        default:
            return state;
    }
};


Мой test, который не работает:

import {localeReducer} from '...'
import actionTypes from "...";
import en from '...'
import ru from '...'

describe('testing localeReducer', () => {
    it('testing localeReducer with action {type: actionTypes.SET_LOCALE}', () => {
        console.log("initialState");
        const translates = {en, ru};
        const initialState = {
            dictionary: translates['ru'],
        };
        let expectedInitialState = {
            dictionary: translates['en'],
        };
        const actionCreator = { type: actionTypes.SET_LOCALE, payload: 'en' };
        const actualInitialState = localeReducer(initialState, actionCreator);
        assert.deepEqual(actualInitialState, expectedInitialState);
    });
});
  • Вопрос задан
  • 45 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы