我有AccountSelection,它渲染了AccountSelectionModal

我想做mount以便测试用户交互的某些方面:

const wrapper = mount(<AccountSelection {...accountSelectionComponentParams} />);

但是,我想模拟AccountSelectionModal-我不需要它(它也是连接的组件,并且我不想在测试中使用store)。

当我用jest.mock('../AccountSelectionModal', () => 'AccountSelectionModal');模拟它时

我开始收到很多警告:

Warning: <AccountSelectionModal /> is using incorrect casing. Use PascalCase for React components, or lowercase for HTML elements.
    console.error node_modules\fbjs\lib\warning.js:33




Warning: The tag <AccountSelectionModal> is unrecognized in this browser. If you meant to render a React component, start its name with an uppercase letter.


要么

Warning: Unknown event handler property `onHide`. It will be ignored.


要么

React does not recognize the `selectedAccountId` prop on a DOM element. If you intentionally want it to appear in the DOM as
a custom attribute, spell it as lowercase `selectedaccountid` instead. If you accidentally passed it from a parent component, remove it from the DOM element.


所有警告均来自AccountSelectionModal上设置的道具。

如何正确模拟AccountSelectionModal

最佳答案

传递给mock的第二个参数是一个函数,该函数返回想要返回的内容,并且由于要模拟组件,因此该函数应返回有效的react组件(现在返回的是String)。

这就是模拟组件的方式。

jest.mock('../AccountSelectionModal', () => () => 'AccountSelectionModal');


(注意传递给mock的函数现在如何返回函数)

您还可以返回一个String,但是它应该为小写字母(contains短划线)​​,这样它将被视为一个自定义元素,而不是一个react元素。

 jest.mock('../AccountSelectionModal', () => 'account-selection-modal');

07-26 03:22