简体   繁体   English

如何使用 React 测试库测试 React 组件是否返回 null 或其子级?

[英]How to test if React component is returning null or its children using React Testing Library?

I have a React component that returns its children to be rendered by React if the prop isTrue is truth-y.我有一个 React 组件,如果 prop isTrue是真值,则返回其children级以由 React 呈现。 If its prop isTrue is false-y, then the component returns null , and React doesn't render anything.如果它的 prop isTrue为 false-y,则组件返回null ,并且 React 不会渲染任何内容。

I need to test it as a component, mount it, pass the prop, and test if it's children is getting rendered when the prop isTrue is truth-y, or we are getting null if isTrue is false-y.我需要将它作为一个组件进行测试,安装它,传递道具,并测试当道具isTrue为真值时它的子项是否正在渲染,或者如果isTrue为假值,我们将得到null

Here is my component:这是我的组件:

const RenderIf = ({ isTrue, children }) => {
    if (isTrue) {
        return children;
    }
    return null;
}
export default RenderIf
expect(container).toBeEmptyDOMElement()
import React from 'react'; import { render } from '@testing-library/react'; import '@testing-library/jest-dom/extend-expect'; //... describe('TestHistory', () => { it('should render nothing if there are no history entries', () => { // ARRANGE // ACT const { container } = render( <TestHistory version={1} versionHistory={[]} datasetType={DatasetType.VALIDATION} />, ); // ASSERT expect(container).toBeEmptyDOMElement(); }); });

I'd think in this case it is probably ok to test the whole html.我认为在这种情况下,测试整个 html 可能是可以的。 react-testing-library wraps your content with a single div so you could something like that: react-testing-library 用一个 div 包装你的内容,所以你可以这样:

const { container } = render(<MyComponent ifTrue={false}>Content</MyComponent>);
expect(container.innerHTML).toBe('<div></div>');

If you don't like this approach, you can still render a children with a test-id / text and query it to see if it's present.如果您不喜欢这种方法,您仍然可以使用 test-id / text 渲染一个孩子并查询它以查看它是否存在。

expect(container.innerHTML).toEqual('');

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

相关问题 在组件中使用 React.Children 时,Jest 和 React 测试库返回未定义的错误 - Jest and React Testing Library returning undefined error when using React.Children in a component 使用 test 和 react-testing-library 测试反应组件 - Test a react component using test and react-testing-library 使用 Redux 和 react-testing-library 测试 React 组件 - Test React Component using Redux and react-testing-library 如何使用 onClick 函数 Jest/React 测试库测试 React 组件 - How to test react component with onClick function Jest/React testing library 如何使用反应测试库测试 HOC - How to test HOC using react testing library 如何使用 React 测试库测试支持 Redux 的组件? - How to test a Redux-enabled component with React Testing Library? React 测试库 + Jest:如何测试接收数字然后对其进行格式化的组件 - React testing library + Jest: How to test a component that receives a number then format it 如何在测试文件中呈现条件组件? 反应测试库 - How to render a conditional component in a Test file? React Testing Library React 测试库 - 如何在状态中填充值以测试组件? - React testing library - how to populate values in a state to test the component? 如何测试这个组件与 React 测试库原则保持一致? - How to test this component staying aligned with React Testing Library Principles?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM