Skip to content

Instantly share code, notes, and snippets.

@coreyhaines
Created January 3, 2010 21:11
Show Gist options
  • Select an option

  • Save coreyhaines/268132 to your computer and use it in GitHub Desktop.

Select an option

Save coreyhaines/268132 to your computer and use it in GitHub Desktop.
learning to do javascript unit testing
describe("#old_update_sticky_text", function(){
before(function(){
this.sticky_update_url_for = function(x) { return null; }
this.parent_sticky = function(x) { return null; }
this.update_sticky = function(x) { return null; }
});
it("returns the value passed in", function(){
var value = old_update_sticky_text("foo", null);
expect(value).to(equal, "foo");
});
it("calls #parent_sticky", function(){
expect_called(this, 'parent_sticky', function(){
old_update_sticky_text("foo", null);
});
});
it("calls #sticky_update_url_for", function(){
expect_called(this, 'sticky_update_url_for', function(){
old_update_sticky_text("foo", null);
});
});
it("calls #update_sticky", function(){
expect_called(this, 'update_sticky', function(){
old_update_sticky_text("foo", null);
});
});
});
Screw.Matchers['expect_called'] = function(obj, func_name, block){
var got_called = false;
obj[func_name] = function(x) { got_called = true; }
block();
this.expect_true(got_called);
};
// used like this
it("calls #parent_sticky", function(){
expect_called(this, 'parent_sticky', function(){
old_update_sticky_text("foo", null);
});
});
var TSP = {
update_sticky_text_handler : function(value, settings){
return value;
}
};
require("spec_helper.js");
rails_require('tsp');
Screw.Unit(function(){
describe("TSP", function(){
describe("#update_sticky_text_handler", function(){
it("returns the value passed in", function(){
var value = TSP.update_sticky_text_handler("new text", null);
expect(value).to(equal, "new text");
});
});
});
});
Screw.Unit(function(){
describe("getting information from sticky div", function(){
it("can get the update url", function(){
sticky_div = $("#sticky_23");
sticky_div.attr('data-update-url', "/stickies/25");
expect(sticky_update_url_for(sticky_div)).to(equal, "/stickies/25");
});
});
});
<div class='sticky'>
<div class='editable' id='sticky_23'></div>
</div>
function update_sticky_text(value, settings) {
var stickie = $(this);
console.log("value " + value + " settings " + settings + " this " ));
var url = sticky_update_url_for(stickie);
console.log("url " + url );
// jQuery.post(url, {content : value, method : 'put'});
return value;
}
function sticky_update_url_for(sticky_div){
return sticky_div.attr('data-update-url');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment