title: Mocking component methods with jest
body: <p>Say I have the following component:</p>
  
  <pre><code>export class ExampleComponent extends Component {
  
      exampleMethod1 = () =&gt; {
        console.log('in example 1')
      }
  
      exampleMethod2 = () =&gt; {
        console.log('in example 2')
        this.exampleMethod1()
      }
  
      render() {
        return (
          &lt;TouchableOpacity id='touchable' onPress={exampleMethod2}&gt;&lt;Text&gt;Touchable&lt;/Text&gt;&lt;/TouchableOpacity&gt;
        )
      }
  }
  </code></pre>
  
  <p>This works exactly how you would expect. The button appears, and can be pressed. Both methods fire, and console log their text.</p>
  
  <p>I now try to test this with jest:</p>
  
  <pre><code>describe('example tests', () =&gt; {
    let wrapper
    beforeEach(() =&gt; {
      wrapper = shallow(&lt;ExampleComponent/&gt;)
    })
  
    it('this test fails. Interestingly both messages still print', () =&gt; {
      const instance = wrapper.instance()
      instance.exampleMethod2 = jest.fn()
      wrapper.find('#touchable').simulate('press')
      //wrapper.update() uncommenting this line has no effect.
      expect(instance.exampleMethod2.mock.calls.length).toBe(1)
    })
  
    it('this test passes. Only the first message prints', () =&gt; {
      const instance = wrapper.instnace()
      instance.exampleMethod1 = jest.fn()
      wrapper.find('#touchable').simulate('press')
      expect(instance.exampleMethod1.mock.calls.length).toBe(1)
    })
  })
  </code></pre>
  
  <p>As annotated, the first test fails, and the original message prints, as if I had never mocked out the method. This happens irrespectively of whether <code>wrapper.update()</code> is run or not.</p>
  
  <p>Interestingly, if we replace the onPress with a seemingly identical arrow function like so:</p>
  
  <p><code>onPress={() =&gt; {exampleMethod2()}}</code></p>
  
  <p>The test suddenly passes. This whole thing suggest some weird this binding shenanigans (I think?). Any explanation as to what is going on would be much appreciated!</p>