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: https://mixpanel.com/help/reference/javascript . 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://willcodefor.beer/react-testing-with-mocha-chai-sinon-and-gulp/ and here http://www.hammerlab.org/2015/02/14/testing-react-web-apps-with-mocha/ .
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?
You can create global object to emulate it
mixpanel = {
track : fucntion(){}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.