我在componentDidMount
:
apolloClient.readQuery({
query: authenticatedUserQuery
});
这是我的apollo客户端代码:
const apolloClient = new ApolloClient({
link: link,
cache: new InMemoryCache({
dataIdFromObject: object => object.id
}),
});
我正在尝试用酶测试该组件,但当然我收到错误,因为apolloClient
没有被嘲笑。
这就是我的测试结果:
import React from 'react';
import {shallow} from "enzyme";
import Comment from "./index";
describe('Comment component', () => {
it('Should render Comment component', () => {
const wrapper = shallow(<Comment/>);
expect(wrapper).toMatchSnapshot();
});
});
我没有在互联网上找到任何关于如何做到这一点的信息。
答案 0 :(得分:0)
我发现了apollos MockedProvider
:
import React from 'react';
import { MockedProvider } from '@apollo/react-testing';
import {mount} from "enzyme";
import Comment from "./index";
describe('Comment component', () => {
it('Should render Comment component', () => {
const wrapper = mount(<MockedProvider><Comment/></MockedProvider>);
expect(wrapper).toMatchSnapshot();
});
});
阅读本文,以了解如何模拟查询https://www.apollographql.com/docs/react/recipes/testing/