title: How to simulate the back button through enzyme or jest with react-native
body: <p>I am unable to mock the hardware back button by using enzyme/jest with react-native code. And also mentioned that I am new to the Enzyme/Jest with react-native</p>
  
  <p>I followed the following link and tried to implement. Unfortunately, I am unable to do that.</p>
  
  <p><a href="https://stackoverflow.com/questions/44952284/how-to-simulate-android-back-button-in-react-native-test">How to simulate android back button in react-native test</a></p>
  
  <p>Sample.js,</p>
  
  <pre><code>import React, { Component } from "react";
  import {
    View,
    Text,
    BackHandler,
    BackAndroid
  } from "react-native";
  
    componentWillMount() {
      BackHandler.addEventListener('hardwareBackPress', this.handleBackButtonPress);
    }
  
    componentWillUnmount() {
      BackHandler.removeEventListener('hardwareBackPress', this.handleBackButtonPress);
    }
  
    handleBackButtonPress = () =&gt; {
      return true;
    };
  </code></pre>
  
  <p>sample.test.js,</p>
  
  <pre><code>import 'react-native';
  import React from "react";
  import  mockPressBack  from 'react-native';
  
  jest.mock('BackHandler', () =&gt; {
      return {
          addEventListener: mockPressBack
      };
  });
  
  describe('sample verifies', ()=&gt;{
        test('back button press', ()=&gt; {
          const wrapper = shallow(&lt;Sample /&gt;);
      const sampleData = wrapper.instance();
      sampleData.componentWillMount(); 
      });
  });
  </code></pre>
  
  <p>Actual Result:
  TypeError: _reactNative.BackHandler.addEventListener is not a function</p>
  
  <p>Expected Result:
  It runs successfully.</p>
  
  <p>I am struck here. Please help me.</p>
  
  <p>Thanks.</p>