Created
October 3, 2010 03:05
-
-
Save adomokos/608223 to your computer and use it in GitHub Desktop.
This file contains 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
// TotalCalculator.js | |
function TotalCalculator() { | |
} | |
TotalCalculator.prototype.calculate = function(quantity, price) { | |
var _quantity = parseInt(quantity); | |
if (isNaN(_quantity)) | |
return 'N/A'; | |
var _price = parseInt(price.replace('$', '')); | |
var total = _quantity * _price; | |
return '$' + total.toFixed(2); | |
} | |
// TotalCalculatorSpec.js | |
describe("TotalCalculator", function() { | |
var totalCalculator; | |
beforeEach(function() { | |
totalCalculator = new TotalCalculator(); | |
}); | |
it('calculates total from integer strings', function() { | |
var result = totalCalculator.calculate('2', '$15.00'); | |
expect(result).toEqual('$30.00'); | |
}); | |
it('calculates total from double string', function() { | |
var result = totalCalculator.calculate('2.3', '$15.00'); | |
expect(result).toEqual('$30.00'); | |
}); | |
it('handles non-numeric quantity', function() { | |
var result = totalCalculator.calculate('He', '$15.00'); | |
expect(result).toEqual('N/A'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment