I am attempting to trigger a d3 event using Jasmine. In particular, I want to check that my event listener is being called using a Jasmine spy.
For example, if I attach the d3 zoom behavior to an svg element (I am using Backbone.js for my front end):
Code (a):
class MyView extends Backbone.View
initialize: ->
zoom = d3.behavior.zoom().on("zoom", this.zoom_listener)
d3.select(this.el).append("svg").attr("class", "viewport").call(zoom)
zoom_listener: ->
console.log("zoom called")
The following test in Jasmine fails:
Code (b):
it "calls zoom listener on dblclick", ->
zoom_spy = spyOn(MyView.prototype, "zoom_listener").andCallThrough()
view = new MyView()
view.$(".viewport").trigger("dblclick")
waitsFor((-> zoom_spy.callCount == 1), "Call zoom", 1000)
On the other hand, (just as a sanity check) if I binded a 'dblclick' event to my view as shown below, the above test i.e. Code (b) will pass:
Code (c):
class MyView extends Backbone.View
events:
"dblclick" : "zoom_listener"
initialize: ->
zoom = d3.behavior.zoom().on("zoom", this.zoom_listener)
d3.select(this.el).append("svg").attr("class", "viewport")
# .call(zoom) # commented off this line for the sanity check
zoom_listener: ->
console.log("zoom called")
Can anyone give me some insight as to why I can't seem to get the D3 zoom event triggered within the Jasmine test i.e. Code (b) using my original view above i.e. Code (a)?