0

So I have this JS prototype (class) that I'm trying to build jasmine tests for and cannot seem to figure out how to get these tests working.

here's the important parts of the class:

class Calendar extends BasicView
  initialize: (options) ->
    this.$el = options.el
    {@sidebar} = options
    this.$('.select-day').click this.display_date

    this

  display_date: (e) =>
    console.log 'display_date called' # <~~ this is printing
    ... do stuff ...

and the tests I'm writing:

describe "Calendar", ->
  calendar = null

  beforeEach ->
    loadFixtures "calendar/calendar.html"

  describe "#initialize", ->
    beforeEach ->
      calendar = new Calendar().initialize
        el: $('.event-calendar')
        # just mocking dependency class
        sidebar: jasmine.createSpyObj(CurrentlyViewing, ["$"])

    it "listens for click event on .select-day", ->
      spyOn(calendar, 'display_date')
      calendar.$('.select-day:eq(1)').trigger 'click'
      expect(calendar.display_date).toHaveBeenCalled()

When I run the test I get Expected spy display_date to have been called. despite the fact that the actual method IS getting called. I know it's just that what I'm spying on isn't the instance of Calendar that I initialized, but I can't figure out how or why.

I'd appreciate any help anyone can give me.

Sparkmasterflex
  • 1,837
  • 1
  • 20
  • 33

1 Answers1

0

So I figured this out 20+ minutes after posting the question... as I almost always do.

The issue was that I was setting calendar variable to new Calendar().initialize(...) and then spying on it (I guess). Here's what actually works:

describe "Calendar", ->
  calendar = null

  beforeEach ->
    loadFixtures "calendar/calendar.html"
    calendar = new Calendar()
    spyOn calendar, 'display_date'

  describe "#initialize", ->
    beforeEach ->
      calendar.initialize
        el: $('.event-calendar')
        sidebar: jasmine.createSpyObj(CurrentlyViewing, ["$"])

    it "listens for click event on .select-day", ->
      calendar.$('.select-day:eq(1)').trigger 'click'
      expect(calendar.display_date).toHaveBeenCalled()
Sparkmasterflex
  • 1,837
  • 1
  • 20
  • 33