Skip to content

Instantly share code, notes, and snippets.

@searls
Created November 18, 2011 23:40
Show Gist options
  • Save searls/1378113 to your computer and use it in GitHub Desktop.
Save searls/1378113 to your computer and use it in GitHub Desktop.
#1
doStuff = -> alert("Stuff!")
$('.do-stuff').live 'click', (e) ->
doStuff()
#2
clickeyDo = (selector, callback) ->
$(selector).live 'click', (e) ->
e.preventDefault()
callback(e)
describe "#1 automatic binding, real trigger", ->
Given -> spyOn(window, "doStuff")
Given -> @$button = inject('do-stuff')
When -> @$button.trigger('click')
Then -> expect(doStuff).toHaveBeenCalled()
describe "#2 named function, spying, no trigger", ->
Given -> @$saveStuff = inject('save-stuff')
Given -> spyOn($.fn,"live")
Given -> @callback = jasmine.createSpy()
When -> clickeyDo('.save-stuff', @callback)
Then -> expect($.fn.live).toHaveBeenCalledWith('click', jasmine.any(Function))
Then -> expect($.fn.live.mostRecentCall.object).toBe(@$saveStuff)
describe "~ the anonymous bound method", ->
Given -> @event = jasmine.createSpyObj('event',['preventDefault'])
When -> $.fn.live.mostRecentCall.args[1](@event)
Then -> expect(@event.preventDefault).toHaveBeenCalled()
Then -> expect(@callback).toHaveBeenCalledWith(@event)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment