vendredi 27 novembre 2015

Javascript testing stub global variable function

I am writing a few Unit tests for a React application.

In the page header, the 'mixpanel' tracking library is added between <script> tags as documented here: http://ift.tt/1pO14pc. According to their documentation, "The snippet provides a global variable named mixpanel that you can use to send data to the Mixpanel API."

In the React component's code, Mixpanel's track function is called like so:

  showModal: function(evt) {
    evt.preventDefault()
    var modal = this.refs.modal
    modal.showModal()
    mixpanel.track("Login button clicked")
  }

However, in the test, there is an error caused by mixpanel.track not being defined (when showModal is called). I have set up the test environment to use a testdom as explained here: http://ift.tt/1edi0C4 and here http://ift.tt/1zdN5JY.

I believe I need to simulate the track method to at least exist so that an error is not thrown in the test. Is this correct, and what is the best way to do this?

Aucun commentaire:

Enregistrer un commentaire