Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active May 2, 2016 22:36
Show Gist options
  • Save henrik/5875909 to your computer and use it in GitHub Desktop.
Save henrik/5875909 to your computer and use it in GitHub Desktop.
Stuff extracted from writing Konacha/Mocha/Chai/Sinon unit tests.
describe "Using a fake server for Ajax", ->
server = null
beforeEach -> server = sinon.fakeServer.create()
afterEach -> server.restore()
context "when Ajax succeeds", ->
beforeEach -> respondWithJSON("/url/123", 200, '{"city": "Palm Springs"}')
it "does stuff", ->
server.respond()
context "when Ajax fails", ->
beforeEach -> respondWithJSON("/url/123", 404)
it "does stuff", ->
server.respond()
respondWithJSON = (url, status, body) ->
server.respondWith(
"GET",
url,
[status, { "Content-Type": "application/json" }, (body || "")]
)
assertThereWereNoAjaxRequests = ->
server.requests.should.be.empty()
expectToAlert = (cb) ->
fakeAlert = sinon.stub(window, "alert")
cb()
assert fakeAlert.calledOnce, "Expected alert."
# jQuery ":focus" didn't work for some reason.
assertFocused = ($element) ->
$element[0].should.eq(document.activeElement)
# Debug.
window.showHtml = ->
console.log "-----"
console.log $(document.body).html()
console.log "-----"
# FIXME: Not sure if this is a good pattern. Would be more unit-y to decouple tests from the ready event.
# Wrap jQuery.ready so we can trigger it in
# our mocha tests, which replace the body after
# the original ready event.
window.domReadyFunctions = []
window.fakeDomReady = ->
fn() for fn in domReadyFunctions
$.fn.oldReady = $.fn.ready
$.fn.ready = (fn) ->
domReadyFunctions.push(fn) if fn
$.fn.oldReady.apply(this, arguments)
# Similar to RSpec's "let".
# TODO: Make it more similar.
describe "Foo", ->
$thing = undefined
beforeEach ->
$thing = $("#thing")
it "works", ->
$thing.should.be("#thing")
@johanlunds
Copy link

Add a helper looking something like this:

# let is reserved keyword
@given = (name, callback) ->
  beforeEach ->
    @[name] = callback.apply(@)
describe "blah", ->
  given 'thing', -> 42

  it 'works', ->
    expect(@thing).toBe 42

It's not lazily evaluated though, that's a bit more difficult. Quick Google search: jasmine/jasmine#370

@johanlunds
Copy link

This was pretty fun actually! :) I think this works as a lazily evaluated solution:

# let is reserved keyword
@given = (name, callback) ->
  beforeEach ->
    Object.defineProperty @, name, get: =>
      @["_" + name] ?= callback.apply(@)
describe "blah", ->
  beforeEach ->
    @v = 0

  given 'thing', -> @v += 1

  it 'evaluates givens', ->
    expect(@thing).toBe 1
    expect(@v).toBe 1

  it 'only evaluates givens once', ->
    expect(@thing).toBe 1
    expect(@thing).toBe 1
    expect(@v).toBe 1

  it 'evaluates givens lazily', ->
    expect(@v).toBe 0

@henrik
Copy link
Author

henrik commented Jul 7, 2013

@johanlunds GitHub didn't notify me about these comments so I only saw them now. Clever solutions, thank you! Much better than my abandoned experiments. We de-JSed that part of the code for now, but I'll experiment with this next time I write a JS unit test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment