在进行有关渲染特定数量组件的测试时遇到了一些麻烦。这是调试后我的浅层组件的外观:
<div className="block">
<ul className="list">
<Element item={{...}} />
</ul>
</div>
这是我要测试的方式:
expect(wrapper.find(<Element/>)).toHaveLength(1)
测试失败,长度为0。
// EDIT:-更多代码
因此,我将组件导入到测试文件的顶部,如下所示:
import List from "../components/list";
import Element from "../components/element";
然后在describe函数的顶部:
let outer;
beforeEach(() => {
outer = shallow(<List />);
});
整个测试如下:
it("should render an <Element /> if an array is > than 0", () => {
const Children = outer.props().children({
query: " ",
totalItems: 1,
hasMore: true,
data: [{items}] // it's a shorthand.. for testing purposes theres an array with one object
});
const wrapper = shallow(Children);
console.log(wrapper.debug());
expect(wrapper.find(<Element />)).toHaveLength(1);
});