2016-04-13 23 views
1

Depolanacak bir bileşen var. Reaksiyon bileşeninin içindeki bir fonksiyonu test etmeliyim. Bu işleve nasıl ulaşabilirim?Bağlanan bileşen içindeki işlevi reaksiyonda nasıl test ederim?

Örn: Eğer test ortamında bunları erişebilmesi için

var newReactComponent = React.createClass({ 
    functionToBeTested: function(){ 
     this.props.actions.fetchAll(); 
    } 
}); 
var mapStateToProps = function (state) { 
     return { 
      cars: state.carReducer.cars 
     } 
    }; 

    var mapDispatchToProps = function (dispatch) { 
     return { 
      actions: bindActionCreators(_.assign({}, carActions, apiActions), dispatch) 
     } 
    }; 

    module.exports = connect(mapStateToProps, mapDispatchToProps)(newReactComponent); 
` 

cevap

0

kişinin size gerekli nesne (ler) dönmek için işlevini bağlamak alay edebilir. Bu yolla, newReactComponent 'e erişebileceksiniz ve daha sonra reaksiyon bileşenini (enzimden sığ kullanarak) monte edebileceksiniz.

Montajdan sonra sınıf yöntemlerine erişebileceksiniz. Böyle

şey: Ben de benzer bir soruyu here

cevap vermiş

const actions = {fetchAll: jest.fn()} 
 
const wrapper = shallow(<newReactComponent actions={actions} />); 
 
wrapper.instance().functionToBeTested(); 
 
expect(actions.fetchAll).toBeCalled();

İlgili konular