Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MikeMKH/9587432 to your computer and use it in GitHub Desktop.
Save MikeMKH/9587432 to your computer and use it in GitHub Desktop.
Characterization tests using Mocha and expect.js to understand how Underscore.js' tap function works
var _ = require("underscore"),
send = require("allong.es").allong.es.send,
expect = require("expect.js");
// Characterization tests using Mocha and expect.js to understand how
// underscore.js' tap function works
// See http://comp-phil.blogspot.com/2014/03/bird-watching-with-javascript-kestrel.html
describe("Verify that we understand how tap works", function(){
describe("Call tap with a value", function(){
it("Give a string and a function which does nothing the string will be the same", function(){
expect(_.tap("Mr. Jack", function(){})).to.be.equal("Mr. Jack");
}),
it("Given a value and a function which increments a value the value will increment on each call", function(){
var counter = 0,
increment = function(){counter++;},
peek = function(){return _.tap("Look here", increment);};
expect(counter).to.be.equal(0);
expect(peek()).to.be.equal("Look here");
expect(counter).to.be.equal(1);
peek();
expect(counter).to.be.equal(2);
});
}),
describe("Call tap with an array", function(){
var array = [1, 2, 3, 4, 5];
beforeEach(function(done){
array = [1, 2, 3, 4, 5]; // to reset before each test case
done();
});
it("Given an array and a function which does nothing array will be the same", function(){
expect(_.tap(array, function(){})).to.be.eql(array);
}),
it("Given an array applying pop against it in a tap will remove the last value it", function(){
expect(_.tap(array, send("pop"))).to.be.eql([1, 2, 3, 4]);
}),
it("Given an array applying push against it will add the value to the array", function(){
expect(_.tap(array, send("push", 6))).to.be.eql([1, 2, 3, 4, 5, 6]);
});
});
});
@MikeMKH
Copy link
Author

MikeMKH commented Mar 16, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment