酶测试使用react-apollo的readQuery函数的React组件

时间:2018-02-14 20:30:18

标签: reactjs enzyme jest create-react-app react-apollo

我在componentDidMount

中有一个React组件

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();
  });
});

我没有在互联网上找到任何关于如何做到这一点的信息。

1 个答案:

答案 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/