简体   繁体   English

防止私有组件上的酶浅渲染

[英]Prevent enzyme shallow rendering on private component

How do I prevent shallow rendering on private component with enzyme? 如何防止使用酶对私有组件进行浅层渲染?

Here is a component example: 这是一个组件示例:

// foo.jsx 
import React from 'react';

// Private component
const FooSubtitle = ({subtitle}) => {
  if (!subtitle) return null;

  return <div className="foo__subtitle">{subtitle}</div>;
};

// Public component
const Foo = ({title, subtitle}) => (
  <div className="foo">
    <div className="foo__title">{title}</div>
    <FooSubtitle subtitle={subtitle} />
  </div>
);

export default Foo;

Here is my specification: 这是我的规格:

// foo.spec.js
import React from 'react';
import {shallow} from 'enzyme';
import Foo from './foo.jsx';

describe('Foo', () => {

  it('should render a subtitle', () => {
    const wrapper = shallow(<Foo title="my title" subtitle="my subtitle" />);

    // This test doesn't work, so I cannot test the render of my component
    expect(wrapper.find('.foo__subtitle').length).toBe(1);

    // This one works, but it is not relevant
    expect(wrapper.find('FooSubtitle').length).toBe(1);
  });
});

Any idea? 任何想法? Thanks a lot. 非常感谢。

Shallow rendering is useful to constrain yourself to testing a component as a unit, and to ensure that your tests aren't indirectly asserting on behavior of child components . 浅呈现对于限制自己将组件作为一个单元进行测试很有用, 并确保您的测试不会间接断言子组件的行为

I think you are trying to do what shallow tries to avoid ^^. 我想你正试图做什么浅试图避免^^。

You can unit test the private component directly or use render : 您可以直接对私有组件进行单元测试或使用渲染:

expect(wrapper.find(Foo).render().find('.foo__subtitle')).to.have.length(1);

as explaned here : https://github.com/airbnb/enzyme/blob/master/docs/api/ShallowWrapper/render.md 如下所述: https//github.com/airbnb/enzyme/blob/master/docs/api/ShallowWrapper/render.md

But in both cases you'll need to export your Component and I must admit I had an error testing it with your component. 但在这两种情况下,您都需要导出组件,我必须承认我在组件中测试时遇到错误。 :/ :/

In this case (and in generally) your private component is just a function, use it as a function in the render of your public component and you will be able to test its render with the shallow wrapper. 在这种情况下(通常)你的私有组件只是一个函数,在公共组件的渲染中使用它作为函数,你将能够用浅包装器测试它的渲染。

<div className="foo">
  <div className="foo__title">{title}</div>
  {FooSubtitle({subtitle})}
</div>

Otherwise, I'm not sure it's a good idea to have complex private components... 否则,我不确定拥有复杂的私有组件是个好主意......

You have to export your private component, 您必须导出您的私有组件,

export const FooSubtitle = ...

Now, you are able to test it apart with all its prop variants. 现在,您可以使用其所有道具变体对其进行测试。

Then you can test the presence of FooSubtitle, with particular props, in the render of Foo component as usual and nothing more. 然后你可以像往常一样在Foo组件的渲染中测试FooSubtitle的存在,特别是道具,仅此而已。

If you have private components, and you want to test their implementation, you should: 如果您有私有组件,并且想要测试其实现,则应该:

  1. Have at least enzyme v2.5 至少有酶v2.5
  2. Look at the Enzyme .dive() API : Shallow render a non-DOM child of the current wrapper 查看Enzyme .dive()API :Shallow呈现当前包装器的非DOM子项

Here is a working example: 这是一个工作示例:

// foo.jsx
import React from 'react';

// Private component
const FooSubtitle = ({subtitle}) => {
  if (!subtitle) return null;

  return <div className="foo__subtitle">{subtitle}</div>;
};

// Public component
const Foo = ({title, subtitle}) => (
  <div className="foo">
    <div className="foo__title">{title}</div>
    <FooSubtitle subtitle={subtitle} />
  </div>
);

export default Foo;

// foo.spec.js
import React from 'react';
import {shallow} from 'enzyme';
import Foo from './foo.jsx';

describe('Foo', () => {

  it('should render a subtitle', () => {
    const wrapper = shallow(<Foo title="my title" subtitle="my subtitle" />);

    // This test works, but it is not relevant
    expect(wrapper.find('FooSubtitle').length).toBe(1);

    // This one need the `dive()` API to work
    expect(wrapper.find('FooSubtitle').dive().find('.foo__subtitle').length).toBe(1);
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM