2016-02-01 71 views
7

Sto provando a testare la risposta del codice nativo con la guida this. Il react nativo viene sovrascritto da reactjs per abilitare il rendering e il testing superficiale usando jestjs.Test di unità tocco gli eventi in risposta nativa

Anche se sono in grado di eseguire test di componenti resi superficiali (controllando la presenza e i bambini), non riesco a testare gli eventi di tocco.

handleTouch() { 
this.setState({ showDescription: !this.state.showDescription }); 
} 


render() { 
const description = this.state.showDescription ? (<Text style={styles.description}>{this.props.entry.description}</Text>) : null; 
return (
    <TouchableNativeFeedback onPress={() => this.handleTouch()}> 
    <View style={styles.rowContainer}> 
     <View style={styles.row}> 
     </View> 
     {description} 
    </View> 
    </TouchableNativeFeedback> 
) 
} 

Sto cercando di verificare se il tocco di TouchableNativeFeedback, description tag è reso. Il reactjs TestUtils fornisce Simulate ma non ha funzionato.

Questa è la mia messa a punto specifica:

beforeEach(function() { 
    profileView = TestUtils.renderIntoDocument(<ProfileEntryView entry={entry}/>); 
    var touchableNativeFeedback = TestUtils.findRenderedComponentWithType(profileView, TouchableNativeFeedback); 
    TestUtils.Simulate.onTouchEnd(touchableNativeFeedback); 
}); 

Come faccio a testare le interazioni UI utilizzando reactjs TestUtils per reagire-nativa?

risposta

4

Poiché simulazione ReactTestUtils non ha evento onTouchEnd o onPress. Quindi è necessario aggiungere a oggetto fittizio mocks/react-native.js:

const NativeComponent = (type) => { 
    return React.createClass({ 
     render() { 
      var properties = {className: type}; 
      if (typeof this.props.onPress !== 'undefined') { 
       properties.onClick = this.props.onPress; 
      } 
      Object.assign(properties, this.props); 
      return (
       <div {...properties}>{this.props.children}</div> 
      ); 
     } 
}; 
... 
ReactNative.TouchableNativeFeedback = NativeComponent("TouchableNativeFeedback"); 

e aggiornare il codice di prova per:

var touchableNativeFeedback = TestUtils.findRenderedDOMComponentWithClass(profileView, 'TouchableNativeFeedback'); 
TestUtils.Simulate.click(touchableNativeFeedback); 

Cerchi di più il codice from this page.

Problemi correlati