2016-07-12 43 views
0

我的代码2个变种:TestUtils.renderIntoDocument返回null,如果声明为匿名函数

FIRST(声明为类):

export default class COMPONENT_NAME extends React.Component{ 
    constructor(props){ 
    super(props); 
    this.props = props; 
    } 
    .... 
    render =() => <div className="clName"></div> 
} 

SECOND(声明为匿名函数):

export default (props) => <div className="clName"></div> 

JEST CODE:

jest.dontMock('../js/components/COMPONENT_NAME/index'); 
const COMPONENT_NAME = require('../js/components/COMPONENT_NAME/index.js').default; 
var loaderComponent; 
... 
... 
function renderComponent() { 
    loaderComponent = TestUtils.renderIntoDocument(
    <COMPONENT_NAME /> 
    ); 
} 

为什么测试只适用于第一种情况?

在第二种情况下renderIntoDocument返回null。 我找不到任何有关它的信息。

所以现在的问题是 - 不支持JEST渲染匿名函数?

回答

1

尝试改变

jest.dontMock('../js/components/COMPONENT_NAME/index'); 

jest.unmock('../js/components/COMPONENT_NAME/index'); 
+0

面对完全相同的问题。尝试unmock而不是dontMock,但不起作用。 –

-1

它传递给TestUtils.renderIntoDocument功能时,你应该换你的功能成分div

function renderComponent() { 
    loaderComponent = TestUtils.renderIntoDocument(
     <div> 
      <COMPONENT_NAME /> 
     </div> 
    ); 
} 
+0

已经试过,不帮助: 不变违规:findAllInRenderedTree(...):inst ance必须是复合组件 – artofatih

+0

请从http://pastebin.com上的测试文件中提供完整的代码。 – 1ven

+0

http://pastebin.com/qCZMAYja – artofatih