在這里我有一個我正在測驗的減速器,在我的測驗覆寫范圍內它顯示 Uncovered Line :這部分'return action.payload;' 這是在'changeNode'函式內,關于如何測驗是否有任何建議?(我的開關中有很多案例,其中很少有其他案例,如果我解決了這個問題,我可以輕松解決其他問題)
export function graphArticle(
state = initialGraph,
action: graphArticle
) {
switch (action.type) {
case ActionType.EDIT_NODE:
const changeN = (n: any) => {
if (n.id == action.payload.id) {
return action.payload;
} else {
return n;
}
};
return {
...state,
graph: {
...state.graph,
cameras: updateGraphC(state.graph, action.payload),
model: {
...state.graph.model,
nodes: state.graph.model?.nodes.map(changeN),
},
currentNode: changeN(state.currentNode),
},
};
}
測驗:
it("EDIT_NODE ", () => {
expect(
reducers.graphArticle(undefined, {
type: ActionType.EDIT_NODE,
payload: {
analyser_id: "yureg",
id: 6,
},
})
).toEqual({
floorplan: "",
graph: {
cameras: [],
currentNode: "",
model: {
nodes: undefined,
},
},
});
});
uj5u.com熱心網友回復:
好吧,您的測驗實際上并沒有測驗 reducer 的“編輯”功能,因為您正在針對沒有節點的初始狀態進行測驗。
您需要提供一個初始狀態,當前是undefined您傳遞給的reducers.graphArticle,帶有幾個節點,然后傳遞一個動作作為另一個引數來編輯其中一個,以便測驗通過if和else。
就像是
it("EDIT_NODE ", () => {
const stateToEdit: InitialGraph = {
floorplan: "",
currentNode: "",
graph: {
cameras: [],
model: {
nodes: [{
id: 1,
analyser_id: "first"
}, {
id: 6,
analyser_id: 'original'
}, ],
},
},
};
const resultingStateAfterEdit: InitialGraph = {
floorplan: "",
currentNode: "",
graph: {
cameras: [],
currentNode: "",
model: {
nodes: [{
id: 1,
analyser_id: "first"
}, {
id: 6,
analyser_id: 'yureg'
}, ],
},
},
};
expect(
reducers.graphArticle(stateToEdit, {
type: ActionType.EDIT_NODE,
payload: {
analyser_id: "yureg",
id: 6,
},
})
).toEqual(resultingStateAfterEdit);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/485214.html
標籤:javascript 反应 单元测试 测试 还原
上一篇:格式化json資料或生成動態結構
