Created
January 3, 2010 21:11
-
-
Save coreyhaines/268132 to your computer and use it in GitHub Desktop.
learning to do javascript unit testing
This file contains hidden or 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
| 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); | |
| }); | |
| }); | |
| }); |
This file contains hidden or 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
| 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); | |
| }); | |
| }); |
This file contains hidden or 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
| var TSP = { | |
| update_sticky_text_handler : function(value, settings){ | |
| return value; | |
| } | |
| }; |
This file contains hidden or 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
| 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"); | |
| }); | |
| }); | |
| }); | |
| }); |
This file contains hidden or 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
| 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"); | |
| }); | |
| }); | |
| }); |
This file contains hidden or 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
| <div class='sticky'> | |
| <div class='editable' id='sticky_23'></div> | |
| </div> |
This file contains hidden or 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
| 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