【问题标题】:writing the test cases for the reducer为 reducer 编写测试用例
【发布时间】:2018-12-20 09:48:55
【问题描述】:

我是react-redux 的新手。在这里,我正在尝试为 reducer 编写一个测试用例。

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

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是一样的。

那么,这样写是对还是错?

【问题讨论】:

  • 你的意思是我不需要做...initialState

标签: reactjs redux react-redux jestjs


【解决方案1】:

你必须调用 QuizData 函数并期待新的状态......你不需要将传播逻辑放在你的 toEqual 中

让 newstate= QuizData({},payload)

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

【讨论】:

  • 好的,谢谢你,但是如果我用我以前的方式可以吗?
  • 这样没问题..但是您通过使用扩展语法将一些逻辑带入测试..这就是我说的原因。
猜你喜欢
  • 2018-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-23
  • 2018-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多