【发布时间】:2017-03-03 17:23:13
【问题描述】:
我正在使用一些 react-bootstrap 组件构建一个组件,特别是 Modal 及其名称间隔的子组件,例如Modal.Heading(或 Modal.Title、Modal.Body 等)。例如:
...
import { Modal } from 'react-bootstrap/lib';
import OtherComponent from './OtherComponent';
class MyComponent extends React.Component {
...
render() {
return (
<div>
<Modal>
<p>{someContent}</p>
<OtherComponent/>
<Modal.Header>{someOtherContent}</Modal.Header>
</Modal>
</div>
);
}
}
在 Jest 测试套件中使用 Enzyme 我可以找到 Modal 组件的各种子组件,包括 DOM 元素和其他自定义 React 组件。但是我找不到名称间隔的子组件:
const modal = shallow(<MyComponent/>).find('Modal');
it('should find the Modal element', () => {
expect(modal).toHaveLength(1); // passes
});
it('should find a child DOM element', () => {
expect(modal.find('p')).toHaveLength(1); // passes
});
it('should find a regular child component', () => {
expect(modal.find('OtherComponent')).toHaveLength(1); // passes
});
it('should find a name-spaced child component', () => {
expect(modal.find('Modal.Header')).toHaveLength(1); // fails *****
});
我试过了:
-
.find('Modal.Header'),即包含命名空间,如上图 -
.find('Header'),即离开命名空间, -
mount(<MyComponent/>)而不是shallow(<MyComponent/>)与上述两个find选项的组合
那么我如何使用 Enzyme 来查找命名空间的子组件?
【问题讨论】:
标签: javascript namespaces jestjs react-bootstrap enzyme