首页 > 解决方案 > 有没有一种方法可以浅渲染使用 react-apollo 查询的反应组件

问题描述

这是我要测试的组件。我想为此使用浅渲染。

import React from 'react';
import { Query } from 'react-apollo';
import MY_QUERY from './MyQuery.graphql';
import Component from './Component';

const Container = () => (
    <Query query={MY_QUERY}>
      {({ loading, error, data }) => {
        if (loading) return null;
        if (error) return null;

        return <Component data={data} />;
      }}
    </Query>
  );
};

export default Container;

标签: reactjsjestjsenzymereact-apollo

解决方案


shallow()+renderProp()将完成这项工作:

const wrapper = shallow(<Container />).find(Query);
const result = wrapper.renderProp('children')({ loading: false, data: someMockedData });
expect(result.find(Component).prop('data')).toEqual(someMockedData);

推荐阅读