在测试文件中,我需要渲染一个组件,同时模拟它的一些子组件。文件结构看起来像这样。
文件 1
import {A, B} from 'a-module';
export function MyComponent() {
return (
<div>
<A /> // I need to mock
<B /> // these components out
</div>
);
}
档案 2
import {MyComponent} from 'File 1';
/*
* In this file I would like to render MyComponent but
* have components A and B be replaced by mocks
*/
我试过这样做,jest.mock('a-module', () => 'Blah');
但这并没有成功地模拟组件。但是,这在文件 1 中使用默认导入时有效。
在模拟组件A
和在文件 2 中B
渲染时的任何帮助MyComponent
将不胜感激!