2015-08-14 21 views
11

Sto cercando di capire come testare i componenti React Native (non React JS). Anche guardando il codice di avviamento di React Native, è difficile vedere come testarlo.Come testare i componenti React Native

var AwesomeProject = React.createClass({ 
    render: function() { 
    return (
     <View style={styles.container}> 
     <Text style={styles.welcome}> 
      Welcome to React Native! 
     </Text> 
     <Text style={styles.instructions}> 
      To get started, edit index.ios.js 
     </Text> 
     <Text style={styles.instructions}> 
      Press Cmd+R to reload,{'\n'} 
      Cmd+D or shake for dev menu 
     </Text> 
     </View> 
    ); 
    } 
}); 

var styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    justifyContent: 'center', 
    alignItems: 'center', 
    backgroundColor: '#F5FCFF', 
    }, 
    welcome: { 
    fontSize: 20, 
    textAlign: 'center', 
    margin: 10, 
    }, 
    instructions: { 
    textAlign: 'center', 
    color: '#333333', 
    marginBottom: 5, 
    }, 
}); 

Sono stato in grado di utilizzare al Babel transpile la sintassi JSX così come l'uso Derisione per deridere la Reagire metodi biblioteca createClass e StyleSheet.create, ma alla fine della giornata, io non riesco a creare qualsiasi test significativo

+0

Penso che vogliamo qualcosa di simile alla tecnica di test Reacts Shallow Rendering che viene utilizzata per il web React. Ho postato questa domanda qui: http://stackoverflow.com/questions/32804218/in-react-native-how-can-i-test-my-components-with-shallow-rendering – ooolala

+0

Vedere questa risposta: http: //stackoverflow.com/questions/29730415/how-to-use-jest-with-react-native#comment50964358_29730415 –

risposta

-1

Date un'occhiata al progetto di esempio UIExplorer di ReactNative, che è configurato con diversi XCTest ibridi & richiedono ('NativeModules'). TestModule suite di test.

https://github.com/facebook/react-native/tree/master/Examples/UIExplorer/UIExplorerIntegrationTests/js

+0

Questi test non sembrano essere scritti in Javascript. Penso che stiamo cercando i test unitari JS. – ooolala

+0

Ho cambiato l'URL per portarti direttamente ai file ES6. – pjcabrera

+4

che collega 404 ora –

3

Per eseguire i test con Jest è necessario sostituire ReactNative con Reagire con la cartella __mocks__, utilizzare TestUtils con poca renderer e magari react-shallow-renderer-helpers per ricerca virtuale albero.

Ho fatto repository di esempio con test di unità here e articolo sulla mia strada attraverso di essa here