【发布时间】: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