简体   繁体   English

如何在 Enzyme 中编写测试以检查连接/智能组件是否存在?

[英]How to write test in Enzyme to check if connected/smart component exists?

I have the following component:我有以下组件:

import React from 'react';
import {
  Header
} from 'semantic-ui-react';
import SummaryTable from '../SummaryTable/SummaryTable';
import SummaryFilters from '../SummaryFilters/SummaryFilters';

function TabSummary() {
  return (
    <>
      <Header as="h2">
        Blah blah
      </Header>
      <SummaryFilters />
      <SummaryTable />
    </>
  );
}

export default TabSummary;

How can I write a test for this component to check if the SummaryFilter component exists.如何为此组件编写测试以检查 SummaryFilter 组件是否存在。 SummaryFilter is a connected/smart component and the following test: SummaryFilter 是一个连接/智能组件和以下测试:

it('should display <SummaryFilters /> when page is first loaded', () => {
  console.log(wrapper.debug());
  expect(wrapper.find('SummaryFilters').exists()).toBeTruthy();
});

throws the following error:引发以下错误:

Invariant Violation: Could not find "store" in the context of "Connect(SummaryFilters)". Either wrap the root component in a <Provider>, or pass a custom React context provider to <Provider> and the corresponding React context consumer to Connect(SummaryFilters) in connect options.

What do I need to add/change to run this test successfully?我需要添加/更改什么才能成功运行此测试?

Appreciate any guidance感谢任何指导

Changing expect(wrapper.find('SummaryFilters').exists()).toBeTruthy();改变expect(wrapper.find('SummaryFilters').exists()).toBeTruthy();

to:至:

expect(wrapper.find('Connect(SummaryFilters)').exists()).toBeTruthy();

resolved the error and made the test work as expected.解决了错误并使测试按预期工作。

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

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