MLH-Fellowship / react-native-tutorial

6 stars 0 forks source link

Document tools/libraries used in setting up unit testing #35

Open temitopeakinsoto opened 3 years ago

temitopeakinsoto commented 3 years ago

Write a brief documentation of the framework and other tools/libraries used in setting up unit testing for the React-native tutorial app

temitopeakinsoto commented 3 years ago

Facebook uses Jest to test React Native applications. So, the above assertion libraries and matchers were chosen because they are easy-to-use and also conveniently work with Jest - which already comes straight a react native scaffold.

For the React-native tutorial app, Jest, coupled with the following testing libraries/jest matchers were used for writing unit tests:

Steps for testing a component or piece of UI (e.g component)

    // ExampleUI component to be tested
    import React from 'react';
    import {StyleSheet, Text, View} from 'react-native';  

    Function ExampleUI() {
      return (
       <View testID="container" style={styles.container}>
        <Text testID="welcome" style={styles.welcome}>Welcome to React Native!</Text>
        <Text style={styles.instructions}>
          This is an example React Native UI component test.
        </Text>
       </View>
      );
     }

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

  export default Intro; 

    // __tests__/exampleUI-test.js
   import React from 'react';
   import renderer from 'react-test-renderer';
   import ExampleUI from '/path/to/ExampleUI';

   test('takes snaptshot of ExampleUI ', () => {
      const tree = renderer.create(<Intro />).toJSON();
      expect(tree).toMatchSnapshot();
   });
   test('ExampleUI renders correctly', () => {
     const { queryByTestId } = rtl.render(<DetailsView />); 
     expect(queryByTestId('container')).toBeDefined();   
   });
   test('ExampleUI contains welcome message', () => {
     const { queryByTestId } = rtl.render(<DetailsView />); 
     expect(queryByTestId('welcome')).toBeDefined(); 
     expect(queryByTestId('welcome')).not.toBeEmpty();
     expect(queryByTestId('welcome')).toHaveTextContent('Welcome to React Native!');   
   });

Check out the following resources to gain a deeper insight into testing a working React Native app:

temitopeakinsoto commented 3 years ago

CC: @princiya @PragatiVerma18 @StuffByLiang

princiya commented 3 years ago

Thank you @temitopeakinsoto 👍