简体   繁体   English

如何从 Enzyme 中获取 React 儿童

[英]How to get React children from Enzyme

I've implemented a "slot" system in React from this article:Vue Slots in React .我在这篇文章中在 React 中实现了一个“插槽”系统:Vue Slots in React However, I'm running into trouble when trying to test the component due to a "mismatch" between the Enzyme wrapper's children and React's children.但是,由于 Enzyme 包装器的子代和 React 的子代之间“不匹配”,我在尝试测试组件时遇到了麻烦。

This is the function to get a "slot" child from React children .这是 function 从 React children获得一个“插槽”孩子。 The function works as expected within a app component when provided with the children prop, but doesn't work during testing as the "children" isn't the same format as React.children . function 在提供children道具时在应用程序组件中按预期工作,但在测试期间不起作用,因为“孩子”的格式与React.children

const getSlot = (children, slot) => {
  if (!children) return null;

  if (!Array.isArray(children)) {
    return children.type === slot ? children : null;
  }

  // Find the applicable React component representing the target slot
  return children.find((child) => child.type === slot);
};

The TestComponent isn't directly used in the tests, but is intended to show an example of how the "slots" would be implemented in a component. TestComponent不直接在测试中使用,但旨在展示如何在组件中实现“插槽”的示例。

const TestComponent = ({ children }) => {
  const slot = getSlot(children, TestComponentSlot);

  return (
    <div id="parent">
      <div id="permanentContent">Permanent Content</div>
      {slot && <div id="optionalSlot">{slot}</div>}
    </div>
  );
};

const TestComponentSlot = () => null;
TestComponent.Slot = TestComponentSlot;

This is the basics of the tests I am trying to write.这是我正在尝试编写的测试的基础。 Essentially, creating a super basic component tree and then checking if the component's children contained the expected "slot" component.本质上,创建一个超级基本组件树,然后检查组件的子组件是否包含预期的“插槽”组件。 However, the getSlot function always returns null as the input isn't the same as the input provided by React children when used within the app.但是, getSlot function始终返回null ,因为输入与在应用程序中使用时 React children级提供的输入不同。

it("Finds slots in React children", () => {
  const wrapper = mount(
    <div>
      <TestComponent.Slot>Test</TestComponent.Slot>
    </div>
  );

  // Unsure how to properly get the React children to test method.
  //   Below are some example that don't work...

  // None of these approaches returns React children like function expects.
  //   Some return null and other return Enzyme wrappers.
  const children = wrapper.children();
  const { children } = wrapper.instance();
  const children = wrapper.children().instance();

  // TODO: Eventually get something I can put into function
  const slot = getSlot(children, TestComponentSlot);
});

Any help or insights would be greatly appreciated!任何帮助或见解将不胜感激!

The problem here is that when you're using enzyme's children() method it returns ShallowWrapper [ 1 ].这里的问题是,当您使用酶的children()方法时,它会返回ShallowWrapper [ 1 ]。 In order to get the children as a React component you have to get them directly from the props method.为了将子组件作为 React 组件,您必须直接从 props 方法中获取它们。

So, derive the children in this way:因此,以这种方式派生孩子:

const children = wrapper.props().children;

CodeSandbox example .代码沙盒示例

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

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