为减速器编写测试用例

时间:2018-12-20 09:48:55

标签: reactjs redux react-redux jestjs

我是react-redux的新手。在这里,我正在尝试为减速器编写测试用例。

所以我的减速器看起来像,

const initialState = {
    Low: [
        {
            id: 0,
            technologyId: 0,
            technology: '',
            type: '',
            count: '',
            allowded: 6,
            level: 'EASY'
        }
    ],
    Medium: [
        {
            id: 0,
            technologyId: 0,
            technology: '',
            type: '',
            count: '',
            allowded: 7,
            level: 'MEDIUM'
        }
    ],
    High: [
        {
            id: 0,
            technologyId: 0,
            technology: '',
            type: '',
            count: '',
            allowded: 7,
            level: 'TOUGH'
        }
    ]
}
export default function QuizData(state = initialState, action) {
    switch (action.type) {
        case QUIZ_DATA:
            return {
                ...state,
                Low: action.data,
                error: false,
            }
        case ADD_NEW:
            return {
                ...state,
                [action.data.addtype]: action.data.addData,
                error: false,
            }
        case REMOVE_TECH:
            return {
                ...state,
                [action.data.removeType]: action.data.newArr,
                error: false,
            }
        default:
            return state
    }
}

在这种情况下,发生在add_new中,在数组中又添加了一个与type相对应的对象。

所以,我编写的测试用例就像

it("should add the new Row in the given type", () => {
      const addData = [{
            id: 0,
            technologyId: 0,
            technology: '',
            type: 'CODE',
            count: '',
            allowded: 6,
            level: 'EASY'},
            {
            id: 1,
            technologyId: 0,
            technology: '',
            type: 'NON_CODE',
            count: '',
            allowded: 6,
            level: 'EASY'
        }]
      const payload = { addtype: "Low", addData: addData};
      expect(LowLevelDataReducer(undefined, {
        type: ADD_NEW,
        data: payload
      })).toEqual(
        {
          ...initialState,
          [payload.addtype]: payload.addData,
          error: false
        }
      ) 
  })

initialState相同。

那么,这是正确的方法还是错误的方法?

1 个答案:

答案 0 :(得分:0)

您必须调用QuizData函数并期望处于新状态...您无需将扩展逻辑放入toEqual

let newstate = QuizData({},payload)

expect(newstate.expectedProperty.value).toEqual(expectedValue)