mercredi 23 septembre 2015

Jasmine spyOn Marionette event listener

I have a Marionette ItemView defined like this:

@App.module "ZoneApp.Edit", (Edit, App) ->
  class Edit.View extends Marionette.ItemView

    template: "#zone_edit"

    events:
      'click button#removeZoneButton': 'removeZoneClicked'
      'click button#updateZone': 'updateZoneClicked'

    updateZoneClicked: (e)->
       e.preventDefault()
       @updateZone()

    updateZone: ->
       @trigger('zone:edit', @model)

My test looks like this:

describe 'ZoneApp.Edit', ->
  describe 'click updateZone button', ->
    beforeEach ->
      affix('script#zone_edit[type="text/template" form button#updateZone')

    it 'triggers updateZone()', ->
      view = new App.ZoneApp.Edit.View()

      spyOn(view, 'updateZoneClicked')
      view.render()
      view.$el.find('button#updateZone').click()
      expect(view.updateZoneClicked).toHaveBeenCalled()

This test does not pass. It says that updateZoneClicked was never called.

If, however, I change the function that is spied on to updateZone, it works:

describe 'ZoneApp.Edit', ->
  describe 'click updateZone button', ->
    beforeEach ->
      affix('script#zone_edit[type="text/template" form button#updateZone')

    it 'triggers updateZone()', ->
      view = new App.ZoneApp.Edit.View()

      spyOn(view, 'updateZone')
      view.render()
      view.$el.find('button#updateZone').click()
      expect(view.updateZone).toHaveBeenCalled()

My guess is that because the updateZoneClicked is taken care of in the events hash, that my spyOn directive is not taking effect.

Any idea if it's possible to attach a spy to a function like this?

Aucun commentaire:

Enregistrer un commentaire