我刚刚开始在React中探索Jest和Enzyme,并试图访问子组件的道具。测试用例:
describe('<Component_A/>', () => {
let wrapper = shallow(
<BrowserRouter>
<Component_A/>
</BrowserRouter>
);
it('should render Component_A', () => {
expect(wrapper.find(Component_A)).toHaveLength(0);
});
});
Component_A:
const component_A = (props) => {
return (<Component_B
items={items}
/>);
}
我尝试了.find()、. get(0),检查了包装器的道具,孩子,没有Component_B的引用。
任何人都可以解释一下在测试案例中我们如何访问 Component_B 及其道具项吗?
答案 0 :(得分:0)
尝试使用“ mount”而不是“ shallow”。
还将displayName添加到Component_A以便按名称查找它。
Component_A.displayName = "Component_A";