试验条件内减速器开关在反应



在这里我有一个减速器,我正在测试,在我的测试覆盖它显示未覆盖的行:这部分'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,
},
},
});
});

嗯,你的测试实际上并没有测试"edit"减速机的功能,因为您正在测试初始状态,它没有节点。

您需要提供一个初始状态,当前您传递给reducers.graphArticleundefined,具有几个节点,然后传递一个动作作为将编辑其中一个的另一个参数,以便测试将通过ifelse

之类的
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);
});

最新更新