在这里我有一个我正在测试的减速器,在我的测试覆盖率中它显示 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,
},
},
});
});
好吧,您的测试实际上并没有测试减速器的“编辑”功能,因为您正在针对没有节点的初始状态进行测试。
您需要提供一个初始状态,当前是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);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句