Skip to content

Commit

Permalink
some test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarthalloway committed May 21, 2009
1 parent b4018a7 commit 44e4079
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Sample project for the 'Refactoring JavaScript' talk:

http://blog.thinkrelevance.com/2008/6/16/refactoring-javascript

Stuart Halloway
stu@thinkrelevance.com
6 changes: 5 additions & 1 deletion test/javascript/fixtures/numberformatter.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
</head>

<body>
<!-- Put any HTML fixture elements here. -->
<div id="value"></div>
<form>
<input id="input" type="text"></input>
</form>
</body>

</html>
67 changes: 65 additions & 2 deletions test/javascript/numberformatter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,72 @@ require("../../public/javascripts/jquery-1.3.2.js");
require("../../public/javascripts/jquery.numberformatter-1.1.2.js");

Screw.Unit(function(){
describe("formatNumber", function(){
describe("format", function(){
it("defaults to us #,###.00", function(){
expect(formatNumber(1999).to(equal, "boom"));
$("#value").text(1999);
$("#value").format();
expect($("#value").text()).to(equal, "1,999.00");
});

it("supports percents", function(){
$("#value").text(".25");
$("#value").format({format: "##%"});
expect($("#value").text()).to(equal, "25%");
});

it("works with input elements", function(){
$("#input").val(99);
$("#input").format();
expect($("#input").val()).to(equal, "99.00");
});

it("ignores non-format characters at start and end", function(){
$("#value").text("42");
$("#value").format({format: "BOO ## YAA"});
expect($("#value").text()).to(equal, "BOO 42 YAA");
});

it("handles negative prefix, then non-format characters then number, then non-format", function(){
$("#value").text("-500,000.77");
$("#value").format({format: "-$#.#"});
expect($("#value").text()).to(equal, "-$500000.8");
});

it("does nothing if it finds non-format characters in the middle", function(){
$("#value").text("767");
$("#value").format({format: "## AND ##"});
expect($("#value").text()).to(equal, "767");
});

it("default to not show decimal for whole numbers", function(){
$("#value").text("15");
$("#value").format({format: "#.##"});
expect($("#value").text()).to(equal, "15");
});

it("shows decimal for whole numbers if forced", function(){
$("#value").text("15");
$("#value").format({format: "#.##", decimalSeparatorAlwaysShown: true});
expect($("#value").text()).to(equal, "15.");
});

it("handles negative numbers", function(){
$("#value").text("-700");
$("#value").format();
expect($("#value").text()).to(equal, "-700.00");
});

it("rounds positive numbers up", function(){
$("#value").text("11.125");
$("#value").format();
expect($("#value").text()).to(equal, "11.13");
});

it("rounds negative numbers down", function(){
$("#value").text("-11.125");
$("#value").format();
expect($("#value").text()).to(equal, "-11.13");
});

});
});

0 comments on commit 44e4079

Please sign in to comment.