错误:始终违反:findAllInRenderedTree(...):实例必须是复合组件 [英] Error: Invariant Violation: findAllInRenderedTree(...): instance must be a composite component
本文介绍了错误:始终违反:findAllInRenderedTree(...):实例必须是复合组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当在JEST中为React文件编写测试用例时,出现此错误.以下是我的示例代码:
While writing the test case in JEST for React file I am getting this error. Following is my sample code:
search_basr_test.js
search_basr_test.js
jest.autoMockOff();
global.React = require('react/addons');
jest.setMock('../stores/browser_store.jsx');
beforeEach(function() {
var search_bar = require('../components/search_bar.jsx');
});
var TestUtils = React.addons.TestUtils;
describe("Test", function() {
it("should render Test", function() {
var test = TestUtils.renderIntoDocument(<search_bar/>);
expect(test).toBeDefined();
});
it("renders a list in a box with proper CSS classes", function() {
var test = TestUtils.renderIntoDocument(<search_bar/>);
let box = TestUtils.findRenderedDOMComponentWithTag(test, "div");
expect(box.className).toEqual("sidebar__collapse");
});
});
search_bar.jsx
search_bar.jsx
return (
<div>
<div
className='sidebar__collapse'
onClick={this.handleClose}
>
<span className='fa fa-angle-left'></span>
</div>
<span
className='search__clear'
onClick={this.clearFocus}
>
{'Cancel'}
</span>
}
有人在那里帮助我吗?
推荐答案
复合组件是包含React组件(不是div,span等)的组件. 方法'findRenderedDOMComponentWithTag'将参数引入一个复合组件.
A composite component is a component which contains React Component (not div, span, ...) The method 'findRenderedDOMComponentWithTag' takes in parameter a composite component.
请尝试根据您的情况(jquery,js等)直接解析该组件,因为它不是复合组件
Try to parse the component directly in your case (jquery, js, ...) because it is not a composite one
这篇关于错误:始终违反:findAllInRenderedTree(...):实例必须是复合组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文