2016-07-12 44 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