jeudi 29 octobre 2015

Unit-testing javascript/jQuery events with Jasmine

I have this code:

$(document).on('click', '#clear-button', clearCalculatedPrice)

clearCalculatedPrice = ->
    $('#price_rule').removeAttr('data-original-title')
    $('#calculated-price').removeAttr('data-original-title')
    $('#calculated-price').empty()

And such test for it

describe 'Call taker', ->
  it 'should clear calculated price', ->
    spyOn($.fn, 'removeAttr')
    spyOn($.fn, 'empty')
    clearCalculatedPrice()
    expect($.fn.removeAttr).toHaveBeenCalled()
    expect($.fn.empty).toHaveBeenCalled()

I need to create another test which will check that function was called when event was triggered, like this

     it 'should clear calculated price when "click" event is triggered', ->
      $('#destination-clear-button').trigger('click')
      spyOn($.fn, 'removeAttr')
      spyOn($.fn, 'empty')
      expect($.fn.removeAttr).toHaveBeenCalled()
      expect($.fn.empty).toHaveBeenCalled()

But this test do not work as I thought. So question is: How should unit-test that tests event handler should be written

Aucun commentaire:

Enregistrer un commentaire