1

I am trying to use a function I have defined in a JS file within my Jasmine test, but it is coming back as undefined when I run the test.

At the top of my spec I am requiring the coffee file I am using, would that then give me access to the contents of the JS? Could I then not use the function within my test?

employer_category.coffee

$(document).ready ->
  toggle_glyphicon_plus_minus

toggle_glyphicon_plus_minus = () ->
  $('#govt_category').click ->
      $('#govt_category_span').toggleClass('glyphicon-plus glyphicon-minus')
    $('#federal_category').click ->
      $('#federal_category_span').toggleClass('glyphicon-plus glyphicon-minus')
    $('#other_category').click ->
      $('#other_category_span').toggleClass('glyphicon-plus glyphicon-minus')

employe_category_spec.coffee

 #= require employer_category

    describe 'accordion functionality', ->
      beforeEach ->
        employer_category = "<h5 id='govt_category'></h5>" + "<span id='govt_category_span' class='glyphicon glyphicon-plus'>"
        $(employer_category).appendTo('body')
        $('#govt_category').trigger('click')

      it 'should invoke a click event on govt_category', ->
        toggle_glyphicon_plus_minus
        expect($('#govt_category_span')).toHaveClass('glyphicon-minus')
mu is too short
  • 426,620
  • 70
  • 833
  • 800
adamscott
  • 823
  • 1
  • 10
  • 31
  • Also, `toggle_glyphicon_plus_minus` is just a function reference, `toggle_glyphicon_plus_minus()` is a function call. The function-calling parentheses are needed when calling a function without arguments. – mu is too short Apr 20 '16 at 17:41
  • So If I define this as a global variable, I would be able to include it in Jasmine? Right now the function is working in the coffee file. I just need to make use of the function in Jasmine. Not sure if that duplicate answer covers what I was trying to get at. – adamscott Apr 20 '16 at 17:47
  • The `toggle_glyphicon_plus_minus` function is not in scope in your Jasmine tests so apparently you need to explicitly import it. Have you searched other "[coffeescript] [jasmine]" questions? – mu is too short Apr 20 '16 at 18:22

0 Answers0