Skip to content
Browse files

some test coverage

  • Loading branch information...
1 parent b4018a7 commit 44e407994add315492fdedf6aa490a955fd1d982 @stuarthalloway committed May 21, 2009
Showing with 76 additions and 3 deletions.
  1. +6 −0 README
  2. +5 −1 test/javascript/fixtures/numberformatter.html
  3. +65 −2 test/javascript/numberformatter_spec.js
View
6 README
@@ -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
View
6 test/javascript/fixtures/numberformatter.html
@@ -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>
View
67 test/javascript/numberformatter_spec.js
@@ -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.
Something went wrong with that request. Please try again.