Last active
May 2, 2016 22:36
-
-
Save henrik/5875909 to your computer and use it in GitHub Desktop.
Stuff extracted from writing Konacha/Mocha/Chai/Sinon unit tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "-----" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Similar to RSpec's "let". | |
# TODO: Make it more similar. | |
describe "Foo", -> | |
$thing = undefined | |
beforeEach -> | |
$thing = $("#thing") | |
it "works", -> | |
$thing.should.be("#thing") |
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
@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
Add a helper looking something like this:
It's not lazily evaluated though, that's a bit more difficult. Quick Google search: jasmine/jasmine#370