所以我导入了一个组件来使用 Jest 进行一些测试。
class MyComponent extends Component {
render() {
return (
<div>
<OtherComponent />
</div>
);
}
}
export { MyComponent };
其中另一个组件定义为:
class OtherComponent extends Component { ... }
export default connect(...)(OtherComponent);
我的测试如下:
import React from 'react';
import { shallow } from 'enzyme';
import { MyComponent } from '../../components/MyComponent';
// ...
事实上,在 MyComponent 内部有其他组件,它使用 connect 连接到 Redux 使得上面的测试文件中的导入失败:
不变违规:_registerComponent(...):目标容器不是 DOM 元素。
at invariant (node_modules/fbjs/lib/invariant.js:44:15)
at Object._renderNewRootComponent (node_modules/react-dom/lib/ReactMount.js:311:76)
at Object._renderSubtreeIntoContainer (node_modules/react-dom/lib/ReactMount.js:401:32)
at Object.render (node_modules/react-dom/lib/ReactMount.js:422:23)
at Object.<anonymous> (my-app-directory/index.js:30:46)
at Object.<anonymous> (my-app-directory/components/OtherComponent.js:x:xx)
at Object.<anonymous> (my-app-directory/components/MyComponent.js:x:xx)
at Object.<anonymous> (my-app-directory/test/components/MyComponent.test.js:x:xx)
at handle (node_modules/worker-farm/lib/child/index.js:41:8)
at process.<anonymous> (node_modules/worker-farm/lib/child/index.js:47:3)
at emitTwo (events.js:106:13)
at process.emit (events.js:191:7)
at process.nextTick (internal/child_process.js:744:12)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
那么如果嵌套组件连接到 Redux,我该如何测试我的组件呢?O_O'
更新:
我从我的主应用程序文件index.js 中从 react-router 导入历史记录,并调用它在我的其他组件方法之一中推送新路由。这似乎是导致问题的原因。所以我想我不应该在我的组件中使用历史记录?如果我选择保持这种方式,我将如何在我的测试中处理它?
import { history } from '../'; // index.js file with ReactDOM injection
class OtherComponent extends Component {
// ...
someMethod() {
callSomeActionCreator();
history.push('/some/route');
}
}