2015-09-27 105 views
19

對於React,我使用Shallow Rendering技術來對我的React組件進行單元測試。我可以在React Native做類似的事嗎?在React Native中,如何使用淺渲染測試我的組件?

我已經followed the instructions to set up Jest,但找不到關於測試我的組件的任何文檔。我想用React完成與React Native完全一樣的TDD。

+2

好問題。官方文件是非常簡短的說至少.. –

+1

https://medium.com/@jcfrancisco/unit-testing-react-native-components-a-firsthand-guide-cea561df242b#.qeg60edil –

+0

至於我' m知道react-native對iOS或Android環境並沒有太多的假設。你不能只使用Jest並假裝它是一個網絡應用程序?是不是所有的依賴都會被嘲笑? – Parris

回答

9

我認爲enzyme是你正在尋找。

它爲您提供shallow函數,它允許您進行淺層比較(如您所願)。

酶可以與所有流行的測試運動員(如摩卡,Jest,Karma等)一起使用。完整列表可以發現on the library's github page

例子:

import {shallow} from 'enzyme'; 

describe('<MyComponent />',() => { 
    it('should render three <Foo /> components',() => { 
    const wrapper = shallow(<MyComponent />); 
    expect(wrapper.find(Foo)).to.have.length(3); 
    }); 
}); 

對於進一步閱讀,你可以採取酶的Shallow Rendering APIdocs一般看看。