Created
November 5, 2011 14:44
-
-
Save jdudek/1341601 to your computer and use it in GitHub Desktop.
Given/when/then in Jasmine
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
test = (suite) -> | |
self = {} | |
steps = [] | |
last = null | |
run = (suite, steps) -> | |
return if steps.length == 0 | |
[step, msg, fn] = steps.shift() | |
if step == describe | |
describe.call suite, msg, -> | |
beforeEach fn | |
run(this, steps) | |
else if step == it | |
it.call suite, msg, fn | |
run(suite, steps) | |
parseArgs = (args, msg) -> | |
if args.length == 2 | |
args | |
else | |
[msg, args[0]] | |
self.given = (args...) -> | |
[msg, fn] = parseArgs(args, "given") | |
steps.push([describe, msg, fn]) | |
last = self.given | |
self | |
self.when = (args...) -> | |
[msg, fn] = parseArgs(args, "when") | |
steps.push([describe, msg, fn]) | |
last = self.when | |
self | |
self.then = (args...) -> | |
[msg, fn] = parseArgs(args, "then") | |
steps.push([it, msg, fn]) | |
last = self.then | |
self | |
self.and = (args...) -> | |
last(args...) | |
self.finish = -> run(suite, steps) | |
self | |
describe "GameScreenController", -> | |
beforeEach -> | |
# setup... | |
# --- regular Jasmine test -------------------------------------------------- | |
describe "when player clicks 'Play'", -> | |
beforeEach -> | |
@gameScreenView.trigger("play:clicked") | |
it "should create game session on the server", -> | |
expect(@serverSide.postGameSession).toHaveBeenCalled() | |
it "should play sound effect", -> | |
expect(@soundEffects.play).toHaveBeenCalledWith("gameSessionStart") | |
it "should display how-to-play", -> | |
expect(@gameScreenView.showHowToPlay).toHaveBeenCalled() | |
it "should display countdown", -> | |
expect(@gameScreenView.showCountdown).toHaveBeenCalled() | |
expect(@gameScreenView.updateCountdown).toHaveBeenCalledWith(5) | |
it "should not create the task on the server yet", -> | |
expect(@serverSide.postTask).not.toHaveBeenCalled() | |
describe "after one second", -> | |
beforeEach -> | |
@clocks.last().tick(101) | |
it "should update countdown", -> | |
expect(@gameScreenView.updateCountdown).toHaveBeenCalledWith(4) | |
describe "after next four seconds", -> | |
beforeEach -> | |
@clocks.last().tick(501) | |
it "should create new task on the server", -> | |
expect(@serverSide.postTask).toHaveBeenCalled() | |
it "should hide countdown and how-to-play", -> | |
expect(@gameScreenView.hideHowToPlay).toHaveBeenCalled() | |
expect(@gameScreenView.hideCountdown).toHaveBeenCalled() | |
it "should display new task", -> | |
expect(@gameScreenView.setWord).toHaveBeenCalledWith("xyz") | |
expect(@gameScreenView.setLevel).toHaveBeenCalledWith(1) | |
expect(@invaderViews[0].invader.translation).toEqual("abc") | |
expect(@invaderViews[1].invader.translation).toEqual("def") | |
# --- given/when/then test with messages ------------------------------------ | |
test(this) | |
.when "player clicks 'Play'", -> | |
@gameScreenView.trigger("play:clicked") | |
.then "should create game session on the server", -> | |
expect(@serverSide.postGameSession).toHaveBeenCalled() | |
.and "should play sound effect", -> | |
expect(@soundEffects.play).toHaveBeenCalledWith("gameSessionStart") | |
.and "should display how-to-play", -> | |
expect(@gameScreenView.showHowToPlay).toHaveBeenCalled() | |
.and "should display countdown", -> | |
expect(@gameScreenView.showCountdown).toHaveBeenCalled() | |
expect(@gameScreenView.updateCountdown).toHaveBeenCalledWith(5) | |
.and "should not create the task on the server yet", -> | |
expect(@serverSide.postTask).not.toHaveBeenCalled() | |
.when "player waits one second", -> | |
@clocks.last().tick(101) | |
.then "should update countdown", -> | |
expect(@gameScreenView.updateCountdown).toHaveBeenCalledWith(4) | |
.when "player waits four more seconds", -> | |
@clocks.last().tick(401) | |
.then "should create new task on the server", -> | |
expect(@serverSide.postTask).toHaveBeenCalled() | |
.and "should hide countdown and how-to-play", -> | |
expect(@gameScreenView.hideHowToPlay).toHaveBeenCalled() | |
expect(@gameScreenView.hideCountdown).toHaveBeenCalled() | |
.and "should display new task", -> | |
expect(@gameScreenView.setWord).toHaveBeenCalledWith("xyz") | |
expect(@gameScreenView.setLevel).toHaveBeenCalledWith(1) | |
expect(@invaderViews[0].invader.translation).toEqual("abc") | |
expect(@invaderViews[1].invader.translation).toEqual("def") | |
.finish() | |
# --- given/when/then test without messages --------------------------------- | |
test(this) | |
.when(-> @gameScreenView.trigger("play:clicked")) | |
.then(-> expect(@serverSide.postGameSession).toHaveBeenCalled()) | |
.and(-> expect(@soundEffects.play).toHaveBeenCalledWith("gameSessionStart")) | |
.and(-> expect(@gameScreenView.showHowToPlay).toHaveBeenCalled()) | |
.and -> | |
expect(@gameScreenView.showCountdown).toHaveBeenCalled() | |
expect(@gameScreenView.updateCountdown).toHaveBeenCalledWith(5) | |
.and "should not create the task on the server yet", -> | |
expect(@serverSide.postTask).not.toHaveBeenCalled() | |
.when("player waits one second", -> @clocks.last().tick(101)) | |
.then(-> expect(@gameScreenView.updateCountdown).toHaveBeenCalledWith(4)) | |
.when("player waits four more seconds", -> @clocks.last().tick(401)) | |
.then(-> expect(@serverSide.postTask).toHaveBeenCalled()) | |
.and -> | |
expect(@gameScreenView.hideHowToPlay).toHaveBeenCalled() | |
expect(@gameScreenView.hideCountdown).toHaveBeenCalled() | |
.and "should display new task", -> | |
expect(@gameScreenView.setWord).toHaveBeenCalledWith("xyz") | |
expect(@gameScreenView.setLevel).toHaveBeenCalledWith(1) | |
expect(@invaderViews[0].invader.translation).toEqual("abc") | |
expect(@invaderViews[1].invader.translation).toEqual("def") | |
.finish() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment