From 259ce7c7989926b3bc217a21080353bf9a599d68 Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Tue, 7 Oct 2014 12:39:16 -0300 Subject: [PATCH] check amount with decimals --- js/directives.js | 29 ++++++++++++--------- test/unit/directives/directivesSpec.js | 36 ++++++++++++-------------- views/send.html | 10 +++---- 3 files changed, 39 insertions(+), 36 deletions(-) diff --git a/js/directives.js b/js/directives.js index f477d5923..ea0ac33bb 100644 --- a/js/directives.js +++ b/js/directives.js @@ -48,32 +48,37 @@ angular.module('copayApp.directives') }; } ]) - .directive('enoughAmount', ['$rootScope', - function($rootScope) { + .directive('validAmount', ['$rootScope', '$locale', + function($rootScope, locale) { var w = $rootScope.wallet; preconditions.checkState(w); preconditions.checkState(w.settings.unitToSatoshi); + var formats = locale.NUMBER_FORMATS; - var feeSat = Number(bitcore.TransactionBuilder.FEE_PER_1000B_SAT); return { require: 'ngModel', link: function(scope, element, attrs, ctrl) { var val = function(value) { - var availableBalanceNum = Number(($rootScope.availableBalance * w.settings.unitToSatoshi).toFixed(0)); var vNum = Number((value * w.settings.unitToSatoshi).toFixed(0)); + if (typeof value == 'undefined') { + ctrl.$pristine = true; + } + if (typeof vNum == "number" && vNum > 0) { - vNum = vNum + feeSat; - if (availableBalanceNum < vNum || isNaN(availableBalanceNum)) { - ctrl.$setValidity('enoughAmount', false); - scope.notEnoughAmount = true; + var decimals = Number(w.settings.unitDecimals); + var sep_index = ('' + value).indexOf(formats.DECIMAL_SEP); + var str_value = ('' + value).substring(sep_index+1); + if (sep_index > 0 && str_value.length > decimals) { + ctrl.$setValidity('validAmount', false); + scope.notValidAmount = true; } else { - ctrl.$setValidity('enoughAmount', true); - scope.notEnoughAmount = null; + ctrl.$setValidity('validAmount', true); + scope.notValidAmount = null; } } else { - ctrl.$setValidity('enoughAmount', false); - scope.notEnoughAmount = null; + ctrl.$setValidity('validAmount', false); + scope.notValidAmount = null; } return value; } diff --git a/test/unit/directives/directivesSpec.js b/test/unit/directives/directivesSpec.js index 0e80db41b..456cb7b56 100644 --- a/test/unit/directives/directivesSpec.js +++ b/test/unit/directives/directivesSpec.js @@ -62,10 +62,9 @@ describe("Unit: Testing Directives", function() { describe('Unit: bits', function() { beforeEach(inject(function($compile, $rootScope) { $scope = $rootScope; - $rootScope.availableBalance = 1000; var element = angular.element( '
' + - '' + + '' + '
' ); $scope.model = { @@ -81,20 +80,19 @@ describe("Unit: Testing Directives", function() { form.amount.$setViewValue(800); expect(form.amount.$invalid).to.equal(false); form.amount.$setViewValue(900); - expect($scope.notEnoughAmount).to.equal(null); + expect($scope.notValidAmount).to.equal(null); + form.amount.$setViewValue(0.44); + expect($scope.notValidAmount).to.equal(null); + }); it('should not validate', function() { form.amount.$setViewValue(0); expect(form.amount.$invalid).to.equal(true); - form.amount.$setViewValue(9999999999); + form.amount.$setViewValue(999999999999); expect(form.amount.$invalid).to.equal(true); - form.amount.$setViewValue(901); - expect(form.amount.$invalid).to.equal(true); - form.amount.$setViewValue(1000); - expect(form.amount.$invalid).to.equal(true); - form.amount.$setViewValue(901); - expect($scope.notEnoughAmount).to.equal(true); + form.amount.$setViewValue(0.333); + expect($scope.notValidAmount).to.equal(true); }); }); @@ -104,12 +102,13 @@ describe("Unit: Testing Directives", function() { var w = new FakeWallet(walletConfig); w.settings.unitToSatoshi = 100000000; w.settings.unitName = 'BTC'; + w.settings.unitDecimals = 8; $rootScope.wallet = w; $rootScope.availableBalance = 0.04; var element = angular.element( '
' + - '' + + '' + '
' ); $scope.model = { @@ -122,25 +121,24 @@ describe("Unit: Testing Directives", function() { it('should validate', function() { form.amount.$setViewValue(0.01); - expect($scope.notEnoughAmount).to.equal(null); + expect($scope.notValidAmount).to.equal(null); expect(form.amount.$invalid).to.equal(false); form.amount.$setViewValue(0.039); - expect($scope.notEnoughAmount).to.equal(null); + expect($scope.notValidAmount).to.equal(null); + expect(form.amount.$invalid).to.equal(false); + form.amount.$setViewValue(100292.039); + expect($scope.notValidAmount).to.equal(null); expect(form.amount.$invalid).to.equal(false); }); it('should not validate', function() { - form.amount.$setViewValue(0.03999); - expect($scope.notEnoughAmount).to.equal(true); + form.amount.$setViewValue(0.039998888888888); + expect($scope.notValidAmount).to.equal(true); expect(form.amount.$invalid).to.equal(true); form.amount.$setViewValue(0); expect(form.amount.$invalid).to.equal(true); form.amount.$setViewValue(0.0); expect(form.amount.$invalid).to.equal(true); - form.amount.$setViewValue(0.05); - expect($scope.notEnoughAmount).to.equal(true); - expect(form.amount.$invalid).to.equal(true); - }); }); diff --git a/views/send.html b/views/send.html index ed0effc74..6ae8532ea 100644 --- a/views/send.html +++ b/views/send.html @@ -61,19 +61,19 @@ ng-show="!sendForm.amount.$invalid && !sendForm.amount.$pristine">valid! not valid + ng-show="(sendForm.amount.$invalid || notValidAmount) && + !sendForm.amount.$pristine">not valid - Insufficient funds
- +