【发布时间】:2019-04-07 23:19:07
【问题描述】:
由于我没有太多使用 graphQL/Apollo 的经验,因此使用 Jest 进行模拟查询响应让我感到困惑。 我有以下动作创建者:
export const fetchSomething = _id => (dispatch) => {
client.query({
query: gql`
{
something(_id: "${_id}") {
_id
somedata
}
}`,
})
.then(something => dispatch({
type: FETCH_SOMETHING,
payload: something.data,
}));
};
下面的测试不起作用
describe('fetchSomething', () => {
it('should dispatch correct type and data correctly', async () => {
const dispatch = jest.fn();
client.query =
async () => new Promise(resolve => resolve({ something: 'something' }));
await fetchSomething('xxx')(dispatch);
expect(dispatch).toHaveBeenCalledWith({ type: FETCH_SOMETHING, payload: {something: 'something');
});
});
我的 ApolloClient.js 文件
import ApolloClient from 'apollo-boost/lib/index.umd';
const client = new ApolloClient({
uri: 'http://localhost:5000/graphql',
});
export default client;
通常,对于像fetch 这样的方法,解析promise 来模拟响应就足够了。这对我来说现在不适用于 graphql/apollo。在这种情况下如何模拟响应?
提前致谢
【问题讨论】:
-
嗨,您检查过 Apollo 模拟文档吗? apollographql.com/docs/graphql-tools/mocking.html
-
是的,这不是客户端模拟
-
嗨,检查这个可能有用的答案stackoverflow.com/a/53586472/8157186
标签: reactjs testing redux graphql jestjs