Skip to content

Instantly share code, notes, and snippets.

@brianyang
Created June 25, 2012 01:07
Show Gist options
  • Save brianyang/2985833 to your computer and use it in GitHub Desktop.
Save brianyang/2985833 to your computer and use it in GitHub Desktop.
Follow along code for a Jasmine introduction presentation
function Ball() {
var self = this;
var full = false;
self.inflate = function() {
full = true;
};
self.isFull = function() {
return full;
};
return self;
}
describe("Ball", function () {
var ball;
beforeEach(function() {
ball = new Ball();
});
it("should start deflated", function() {
expect(ball.isFull()).toEqual(false);
});
describe("#inflate", function () {
beforeEach(function() {
ball.inflate();
});
it("should fill a ball", function() {
expect(ball.isFull()).toEqual(true);
});
it("should not affect an already inflated ball", function() {
ball.inflate();
expect(ball.isFull()).toEqual(true);
});
});
});
function Game() {
var self = this;
self.prepare = function(ball) {
if (ball.isFull()) {
return;
}
ball.inflate();
};
return self;
}
describe("Game", function () {
var game, ball;
beforeEach(function() {
ball = new Ball();
spyOn(ball, 'inflate').andCallThrough();
game = new Game();
});
describe("with a full ball", function() {
beforeEach(function() {
ball.inflate();
ball.inflate.reset();
game.prepare(ball);
});
it("should not inflate before play", function() {
expect(ball.inflate).not.toHaveBeenCalled();
});
});
describe("with a not-full ball", function() {
beforeEach(function() {
game.prepare(ball);
});
it("should inflate before play", function() {
expect(ball.inflate).toHaveBeenCalled();
});
});
});
describe("When using the Jasmine Ajax Mock", function() {
var onSuccess, onComplete, onFailure, request;
beforeEach(function() {
onSuccess = jasmine.createSpy('onSuccess');
onComplete = jasmine.createSpy('onComplete');
onFailure = jasmine.createSpy('onFailure');
jasmine.Ajax.useMock();
jQuery.ajax({
url: "example.com/someApi",
success: onSuccess,
complete: onComplete,
error: onFailure
});
request = mostRecentAjaxRequest();
});
it("prevents the call from leaving your system", function() {
expect(request.url).toEqual("http://example.com/someApi");
});
describe("with a successful response", function() {
beforeEach(function() {
var successResponse = {
status: 200,
responseText: "w00t!"
};
request.response(successResponse);
});
it("should call the success callback", function() {
expect(onSuccess).toHaveBeenCalledWith("w00t!");
});
it("should call the complete callback", function() {
expect(onComplete).toHaveBeenCalled();
});
});
describe("with a successful response", function() {
beforeEach(function() {
var failureResponse = {
status: 500,
responseText: "Doh!"
};
request.response(failureResponse);
});
it("should call the failure callback", function() {
expect(onFailure).toHaveBeenCalledWith("Doh!");
});
it("should call the complete callback", function() {
expect(onComplete).toHaveBeenCalled();
});
})
});
describe("Manually ticking the Jasmine Mock Clock", function() {
var timerCallback;
beforeEach(function() {
timerCallback = jasmine.createSpy('timerCallback');
jasmine.Clock.useMock();
});
it("causes a timeout to be called synchronously", function() {
setTimeout(function() {
timerCallback();
}, 100);
expect(timerCallback).not.toHaveBeenCalled();
jasmine.Clock.tick(101);
expect(timerCallback).toHaveBeenCalled();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment