From 96f2f86b8787e84c46028839f73d8df58df38cf9 Mon Sep 17 00:00:00 2001 From: David Grilli Date: Sun, 3 Nov 2013 21:23:50 +0000 Subject: [PATCH] Adds tests for SterlingValidator --- public/javascripts/FormHelper.js | 4 +-- public/javascripts/SterlingValidator.js | 10 +++----- test/runner.html | 4 ++- test/spec/SterlingValidatorSpec.js | 34 +++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 test/spec/SterlingValidatorSpec.js diff --git a/public/javascripts/FormHelper.js b/public/javascripts/FormHelper.js index 1b6e46b..d02f428 100644 --- a/public/javascripts/FormHelper.js +++ b/public/javascripts/FormHelper.js @@ -10,9 +10,9 @@ define(["jquery", "underscore", "SterlingValidator", "FormatHelper", "PenniesCal * @param {string} input Input text form the user */ FormHelper.prototype.processInput = function(input) { - var validator = new SterlingValidator(input); + var validator = new SterlingValidator(); - if (validator.validate()) { + if (validator.validate(input)) { var formatHelper = new FormatHelper(); var numericInput = formatHelper.convertToValidFloat(input); var calculator = new PenniesCalculator(numericInput); diff --git a/public/javascripts/SterlingValidator.js b/public/javascripts/SterlingValidator.js index ff77709..d133e9c 100644 --- a/public/javascripts/SterlingValidator.js +++ b/public/javascripts/SterlingValidator.js @@ -3,17 +3,15 @@ define([], function() { /** * Used to validate an input that represents an Sterling value * @constructor - * @param {string} input Input text from the user */ - function SterlingValidator(input) { - this.input = input; + function SterlingValidator() { this.pattern = /^\xA3?\d+(\.\d*)?p?$|^\d+p?$/; } - // returns true if this.input matches this.pattern - SterlingValidator.prototype.validate = function() { - return this.pattern.test(this.input); + // returns true if input matches this.pattern + SterlingValidator.prototype.validate = function(input) { + return this.pattern.test(input); }; diff --git a/test/runner.html b/test/runner.html index 7c1cf28..130eaac 100644 --- a/test/runner.html +++ b/test/runner.html @@ -34,7 +34,9 @@ require([ "domReady!", "FormatHelper", - "spec/FormatHelperSpec" + "SterlingValidator", + "spec/FormatHelperSpec", + "spec/SterlingValidatorSpec" ], function(document) { jasmine.getEnv().addReporter( new jasmine.HtmlReporter() diff --git a/test/spec/SterlingValidatorSpec.js b/test/spec/SterlingValidatorSpec.js new file mode 100644 index 0000000..11d54cd --- /dev/null +++ b/test/spec/SterlingValidatorSpec.js @@ -0,0 +1,34 @@ +define(["underscore", "SterlingValidator"], function(_, SterlingValidator) { + describe("SterlingValidator", function() { + var sterlingValidator; + + beforeEach(function() { + sterlingValidator = new SterlingValidator(); + }); + + + it("should validate valid inputs as valid", function() { + var validInputs = [ + "4", + "85", + "197p", + "2p", + "1.87", + "£1.23", + "£2", + "£10", + "£1.87p", + "£1p", + "£1.p", + "£1.", + "001.41p", + "4.235p", + "£1.257422457p" + ]; + + _.each(validInputs, function(validInput, index) { + expect(sterlingValidator.validate(validInput)).toBe(true); + }); + }); + }); +});