diff --git a/css/main.css b/css/main.css index 5c748b33f..2aeac8172 100644 --- a/css/main.css +++ b/css/main.css @@ -635,3 +635,9 @@ ul.pagination li.current a:hover, ul.pagination li.current a:focus { color:white; } +.input-note { + margin-top: -10px; + display: block; + margin-bottom: 1rem; +} + diff --git a/index.html b/index.html index 0381a52b0..06859b9fc 100644 --- a/index.html +++ b/index.html @@ -696,7 +696,6 @@
diff --git a/js/controllers/send.js b/js/controllers/send.js index 26c50f6aa..9d4e96cb3 100644 --- a/js/controllers/send.js +++ b/js/controllers/send.js @@ -267,8 +267,12 @@ angular.module('copayApp.controllers').controller('SendController', }); }; - $scope.topAmount = function() { - var maxSat = ($rootScope.availableBalance * config.unitToSatoshi).toFixed(0) - bitcore.TransactionBuilder.FEE_PER_1000B_SAT; - $scope.amount = maxSat / config.unitToSatoshi; + $scope.getAvailableAmount = function() { + return ((($rootScope.availableBalance * config.unitToSatoshi).toFixed(0) - bitcore.TransactionBuilder.FEE_PER_1000B_SAT) / config.unitToSatoshi); + }; + + $scope.topAmount = function(form) { + $scope.amount = $scope.getAvailableAmount(); + form.amount.$pristine = false; }; }); diff --git a/js/directives.js b/js/directives.js index 586a1ac31..fac694ddb 100644 --- a/js/directives.js +++ b/js/directives.js @@ -40,12 +40,12 @@ angular.module('copayApp.directives') .directive('enoughAmount', ['$rootScope', function($rootScope) { var bitcore = require('bitcore'); - var feeSat = bitcore.TransactionBuilder.FEE_PER_1000B_SAT; + var feeSat = Number(bitcore.TransactionBuilder.FEE_PER_1000B_SAT); return { require: 'ngModel', link: function(scope, element, attrs, ctrl) { var val = function(value) { - var availableBalanceNum = ($rootScope.availableBalance * config.unitToSatoshi).toFixed(0); + var availableBalanceNum = Number(($rootScope.availableBalance * config.unitToSatoshi).toFixed(0)); var vNum = Number((value * config.unitToSatoshi).toFixed(0)) + feeSat; if (typeof vNum == "number" && vNum > 0) { if (availableBalanceNum < vNum) { diff --git a/test/unit/controllers/controllersSpec.js b/test/unit/controllers/controllersSpec.js index 7dcadc715..a47c013e9 100644 --- a/test/unit/controllers/controllersSpec.js +++ b/test/unit/controllers/controllersSpec.js @@ -282,10 +282,23 @@ describe("Unit: Controllers", function() { }); describe('Send Controller', function() { - var sendCtrl; - beforeEach(inject(function($controller, $rootScope) { + var sendCtrl, form; + beforeEach(inject(function($compile, $rootScope, $controller) { scope = $rootScope.$new(); $rootScope.availableBalance = 123456; + + var element = angular.element( + '
' + + '' + + '
' + ); + scope.model = { + amount: null + }; + $compile(element)(scope); + scope.$digest(); + form = scope.form; + sendCtrl = $controller('SendController', { $scope: scope, $modal: {}, @@ -296,8 +309,15 @@ describe("Unit: Controllers", function() { expect(scope.isMobile).not.to.equal(null); }); it('should autotop balance correctly', function() { - scope.topAmount(); + scope.topAmount(form); + form.amount.$setViewValue(123356); expect(scope.amount).to.equal(123356); + expect(form.amount.$invalid).to.equal(false); + expect(form.amount.$pristine).to.equal(false); + }); + it('should return available amount', function() { + var amount = scope.getAvailableAmount(); + expect(amount).to.equal(123356); }); }); diff --git a/test/unit/directives/directivesSpec.js b/test/unit/directives/directivesSpec.js index 882939143..ff3efc4a8 100644 --- a/test/unit/directives/directivesSpec.js +++ b/test/unit/directives/directivesSpec.js @@ -8,6 +8,10 @@ describe("Unit: Testing Directives", function() { beforeEach(module('copayApp.directives')); + beforeEach(function() { + config.unitToSatoshi = 100; + config.unitName = 'bits'; + }); describe('Check config', function() { it('unit should be set to BITS in config.js', function() { @@ -43,41 +47,89 @@ describe("Unit: Testing Directives", function() { }); describe('Validate Amount', function() { - beforeEach(inject(function($compile, $rootScope) { - $scope = $rootScope; - $rootScope.availableBalance = 1000; - var element = angular.element( - '
' + - '' + - '
' - ); - $scope.model = { - amount: null - }; - $compile(element)($scope); - $scope.$digest(); - form = $scope.form; - })); + describe('Unit: bits', function() { + beforeEach(inject(function($compile, $rootScope) { + $scope = $rootScope; + $rootScope.availableBalance = 1000; + var element = angular.element( + '
' + + '' + + '
' + ); + $scope.model = { + amount: null + }; + $compile(element)($scope); + $scope.$digest(); + form = $scope.form; + })); + it('should validate', function() { + form.amount.$setViewValue(100); + expect(form.amount.$invalid).to.equal(false); + form.amount.$setViewValue(800); + expect(form.amount.$invalid).to.equal(false); + form.amount.$setViewValue(900); + expect($scope.notEnoughAmount).to.equal(null); + }); - - - it('should validate', function() { - form.amount.$setViewValue(100); - expect(form.amount.$invalid).to.equal(false); - form.amount.$setViewValue(900); - expect(form.amount.$invalid).to.equal(false); + it('should not validate', function() { + form.amount.$setViewValue(0); + expect(form.amount.$invalid).to.equal(true); + form.amount.$setViewValue(9999999999); + 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); + }); }); - it('should not validate', function() { - form.amount.$setViewValue(0); - expect(form.amount.$invalid).to.equal(true); - form.amount.$setViewValue(9999999999); - 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); + describe('Unit: BTC', function() { + beforeEach(inject(function($compile, $rootScope) { + config.unitToSatoshi = 100000000; + config.unitName = 'BTC'; + $scope = $rootScope; + $rootScope.availableBalance = 0.04; + var element = angular.element( + '
' + + '' + + '
' + ); + $scope.model = { + amount: null + }; + $compile(element)($scope); + $scope.$digest(); + form = $scope.form; + })); + + it('should validate', function() { + form.amount.$setViewValue(0.01); + expect($scope.notEnoughAmount).to.equal(null); + expect(form.amount.$invalid).to.equal(false); + form.amount.$setViewValue(0.039); + expect($scope.notEnoughAmount).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); + 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); + + }); + }); + }); describe('Contact directive', function() { diff --git a/test/unit/services/servicesSpec.js b/test/unit/services/servicesSpec.js index 388007e06..2c29c78ea 100644 --- a/test/unit/services/servicesSpec.js +++ b/test/unit/services/servicesSpec.js @@ -5,7 +5,13 @@ // var sinon = require('sinon'); +beforeEach(function() { + config.unitToSatoshi = 100; + config.unitName = 'bits'; +}); + describe('Check config', function() { + it('unit should be set to BITS in config.js', function() { expect(config.unitToSatoshi).to.equal(100); expect(config.unitName).to.equal('bits');