我正在使用useQuery钩子构建一个分页,作为 React 中 Apollo 客户端的一部分,它公开了一个fetchMore在这里看到的函数:https ://www.apollographql.com/docs/react/data/pagination/
一切正常,但我正在尝试编写一个测试用例,即当fetchMore函数由于网络错误而失败时。我的组件中的代码如下所示。
const App = () => {
// Some other component logic
const {loading, data, error, fetchMore} = useQuery(QUERY)
const handleChange = () => {
fetchMore({
variables: {
offset: data.feed.length
},
updateQuery: (prev, { fetchMoreResult }) => {
if (!fetchMoreResult) return prev;
return Object.assign({}, prev, {
feed: [...prev.feed, ...fetchMoreResult.feed]
});
}
}).catch((e) => {
// handle the error
})
}
}
基本上我想测试fetchMore函数函数抛出错误的情况。我不想模拟整个 useQuery,只是 fetchMore 函数。fetchMore在我的测试中模拟函数的最佳方法是什么?