-
-
Save timplourde/a48ef241782d374fa358 to your computer and use it in GitHub Desktop.
This file contains 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
// Jasmine intro | |
/////////////////////////////////////////////////////////// | |
// example of basic describe/it usage | |
/////////////////////////////////////////////////////////// | |
// calculator.js | |
function isEven(num) { | |
return num % 2 === 0; | |
} | |
// calculator-tests.js | |
describe("isEven", function() { | |
it("returns true for even numbers", function() { | |
expect(isEven(8)).toEqual(true); | |
}); | |
it("returns false for odd numbers", function() { | |
expect(isEven(7)).toEqual(false); | |
}); | |
}); | |
/////////////////////////////////////////////////////////// | |
// example of nested describes and beforeEach | |
/////////////////////////////////////////////////////////// | |
// customer.js | |
function Customer() { | |
this.isHappy = false; | |
} | |
Customer.prototype.giveDiscount = function(amount) { | |
if(amount > 100){ | |
this.isHappy = true; | |
} | |
}; | |
// customer-tests.js | |
describe("Customer", function() { | |
var sut; | |
beforeEach(function(){ | |
sut = new Customer(); | |
}); | |
it("is not happy by default", function() { | |
expect(sut.isHappy).toEqual(false); | |
}); | |
describe("who gets a discount less than 100", function(){ | |
beforeEach(function(){ | |
sut.giveDiscount(1); | |
}); | |
it("is still not happy", function() { | |
expect(sut.isHappy).toEqual(false); | |
}); | |
}); | |
describe("who gets a discount more than 100", function(){ | |
beforeEach(function(){ | |
sut.giveDiscount(101); | |
}); | |
it("is happy", function() { | |
expect(sut.isHappy).toEqual(true); | |
}); | |
}); | |
}); | |
/////////////////////////////////////////////////////////// | |
// example of a custom matcher | |
/////////////////////////////////////////////////////////// | |
// player.js | |
function Player() { | |
this.health = 100; | |
} | |
Player.prototype.takeHit = function(damage) { | |
this.health -= damage; | |
}; | |
// player-matchers.js | |
var playerMatchers = { | |
toBeDead: function(){ | |
return { | |
compare: function(actual) { | |
var result = { | |
pass : actual.health <= 0 | |
}; | |
if(!result.pass){ | |
result.message = "I'm not dead yet!"; | |
} | |
return result; | |
} | |
} | |
} | |
}; | |
// player-tests.js | |
describe("Player", function() { | |
beforeEach(function(){ | |
jasmine.addMatchers(playerMatchers); | |
}); | |
it("is dead when it takes more than 100 damage", function() { | |
var player = new Player(); | |
player.takeHit(101); | |
expect(player).toBeDead(); | |
}); | |
}); | |
/////////////////////////////////////////////////////////// | |
// example of a spy | |
/////////////////////////////////////////////////////////// | |
// integration.js | |
var service = { | |
getData: function(query) { | |
// imagine some insane complexity here | |
} | |
}; | |
var app = { | |
showData: function(param) { | |
return service.getData({ id: param }).toUpperCase(); | |
} | |
} | |
// integration-tests.js | |
describe("when an app shows data", function() { | |
var result, | |
FAKE_DATA = "super fake"; | |
beforeEach(function(){ | |
spyOn(service, "getData").and.returnValue(FAKE_DATA); | |
result = app.showData(100); | |
}); | |
it("passes the correct args to service.getData", function() { | |
expect(service.getData).toHaveBeenCalledWith({id: 100}); | |
}); | |
it("returns the uppercase'd result from service.getData", function() { | |
expect(result).toEqual(FAKE_DATA.toUpperCase()); | |
}); | |
}); | |
/////////////////////////////////////////////////////////// | |
// async examples | |
/////////////////////////////////////////////////////////// | |
// laggy.js | |
function laggy(obj) { | |
setTimeout(function(){ | |
obj.done = true; | |
}, 1000); | |
} | |
// laggy-tests.js | |
describe("laggy", function() { | |
it("sets done to true (uses done, is slow)", function(done) { | |
var obj = {}; | |
laggy(obj); | |
setTimeout(function(){ | |
expect(obj.done).toEqual(true); | |
done(); | |
}, 1001); | |
}); | |
it("sets done to true (uses clock, is not slow)", function() { | |
var obj = {}; | |
jasmine.clock().install(); | |
laggy(obj); | |
jasmine.clock().tick(1001); | |
expect(obj.done).toEqual(true); | |
jasmine.clock().uninstall(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment