lundi 4 juillet 2016

Jasmine Testing Isues with mockup

Hey folks I have a jasmine coffeescript test like the one below. I'm trying to mock an li.active event and test that in the javascript. Below is the test spec.

  describe 'Interviews', ->
   describe 'TTI.questionAdd', ->
    it 'should close modal when choose a question is empty', ->
     spyOn(TTI.Position, 'questionCancel')

      $('.btn-question-add').on('click', TTI.Position.questionAdd)

       htmlMock = '<form action="javascript:void();>"'
       htmlMock += '<ul id="#question-create-methods">'
       htmlMock += '<li class="active"><a href="#">Choose a Question</a></li>'
       htmlMock += '</ul>'
       htmlMock += '<input class="btn-question-add" type="submit">'
       htmlMock += '</form>'

      $(htmlMock).appendTo('body')

      console.log(document.getElementsByTagName('body')[0].innerHTML)

      $(".btn-question-add").trigger('click')
      ...

And here is my real coffeescript file snippet

TTI.Position.questionAdd = (e) ->
 e.preventDefault()
 t = $(this)
 id = t.attr 'id'
 tab = $('.nav-tab.active').data('type')
 type = 'vod'
 duration = parseInt($('.js-duration-mins').val()) * 60 + parseInt($('.js-duration-secs').val())
 switch $('#question-create-methods').find('li.active a').html()
  when 'Choose a Question'
   body = $('#library_questions').find('a.active').html()
  when 'Write a Question'
   body = $('#question_write_body').val()
  when 'Custom Question Bank'
   question_bank_id = $('#question-custom').find('a.active').data('id')
   body = $('#question-custom').find('a.active').html()
   ...

But when I print out $('#question-create-methods').find('li.active a').html() to the console from the actual javascript, it's always undefined!! This is making me nuts as it's preventing me to test this properly by mocking the 'li.active a' element. What am I doing wrong? It could be that the javascript is getting executed before the dom is loaded, so how do I make sure that is not the case. Please help! Thanks!

Aucun commentaire:

Enregistrer un commentaire