public
Last active

Follow along code for a Jasmine introduction presentation

  • Download Gist
Ball.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
function Ball() {
var self = this;
 
var full = false;
 
self.inflate = function() {
full = true;
};
 
self.isFull = function() {
return full;
};
 
return self;
}
BallSpec.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
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);
});
});
});
Game.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13
function Game() {
var self = this;
 
self.prepare = function(ball) {
if (ball.isFull()) {
return;
}
 
ball.inflate();
};
 
return self;
}
GameSpec.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
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();
});
});
});
MockAjaxSpec.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
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();
});
})
});
MockClockSpec.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
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();
});
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.