diff --git a/js/directives.js b/js/directives.js index 82704b104..555b78991 100644 --- a/js/directives.js +++ b/js/directives.js @@ -1,10 +1,12 @@ 'use strict'; +var bitcore = require('bitcore'); +var Address = bitcore.Address; +var bignum = bitcore.Bignum; +var preconditions = require('preconditions').singleton(); + angular.module('copayApp.directives') - .directive('validAddress', ['$rootScope', function($rootScope) { - var bitcore = require('bitcore'); - var Address = bitcore.Address; - var bignum = bitcore.Bignum; + irective('validAddress', ['$rootScope', function($rootScope) { return { require: 'ngModel', @@ -46,8 +48,10 @@ angular.module('copayApp.directives') }]) .directive('enoughAmount', ['$rootScope', function($rootScope) { - var bitcore = require('bitcore'); var w = $rootScope.wallet; + preconditions.checkState(w); + preconditions.checkState(w.settings.unitToSatoshi); + var feeSat = Number(bitcore.TransactionBuilder.FEE_PER_1000B_SAT); return { require: 'ngModel', @@ -271,7 +275,7 @@ angular.module('copayApp.directives') client.on('datarequested', function(client) { client.setText(scope.clipCopy); - } ); + }); client.on('complete', function(client, args) { elm.removeClass('btn-copy').addClass('btn-copied').html('Copied!'); diff --git a/test/mocks/FakeWallet.js b/test/mocks/FakeWallet.js index e4a7bc8d7..57aa5d2d2 100644 --- a/test/mocks/FakeWallet.js +++ b/test/mocks/FakeWallet.js @@ -6,11 +6,10 @@ if (is_browser) { } var Wallet = copay.Wallet; -var FakePrivateKey = function () { -}; +var FakePrivateKey = function() {}; FakePrivateKey.prototype.toObj = function() { - return extendedPublicKeyString = 'privHex'; + return extendedPublicKeyString = 'privHex'; }; var FakeWallet = function() { @@ -37,12 +36,20 @@ var FakeWallet = function() { } }; this.blockchain = { - getSubscriptions: function(){ return []; }, - subscribe: function(){} + getSubscriptions: function() { + return []; + }, + subscribe: function() {} }; this.privateKey = new FakePrivateKey(); - this.settings = {}; + this.settings = { + unitName: 'bits', + unitToSatoshi: 100, + unitDecimals: 2, + alternativeName: 'US Dollar', + alternativeIsoCode: 'USD', + }; }; FakeWallet.prototype.createTx = function(toAddress, amountSatStr, comment, opts, cb) { @@ -99,8 +106,7 @@ FakeWallet.prototype.getBalance = function(cb) { return cb(null, this.balance, this.balanceByAddr, this.safeBalance); }; -FakeWallet.prototype.removeTxWithSpentInputs = function (cb) { -}; +FakeWallet.prototype.removeTxWithSpentInputs = function(cb) {}; FakeWallet.prototype.setEnc = function(enc) { this.enc = enc; @@ -110,8 +116,7 @@ FakeWallet.prototype.toEncryptedObj = function() { return this.enc; }; -FakeWallet.prototype.close = function() { -}; +FakeWallet.prototype.close = function() {}; // TODO a try catch was here module.exports = FakeWallet; diff --git a/test/unit/directives/directivesSpec.js b/test/unit/directives/directivesSpec.js index e0a2b389e..87b8aab91 100644 --- a/test/unit/directives/directivesSpec.js +++ b/test/unit/directives/directivesSpec.js @@ -8,17 +8,23 @@ describe("Unit: Testing Directives", function() { beforeEach(module('copayApp.directives')); - beforeEach(function() { - config.unitToSatoshi = 100; - config.unitName = 'bits'; - }); + var walletConfig = { + requiredCopayers: 3, + totalCopayers: 5, + spendUnconfirmed: 1, + reconnectDelay: 100, + networkName: 'testnet', + alternativeName: 'lol currency', + alternativeIsoCode: 'LOL' + }; - 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'); - }); - }); + + beforeEach(inject(function($rootScope) { + $rootScope.wallet = new FakeWallet(walletConfig); + var w = $rootScope.wallet; + w.settings.unitToSatoshi = 100; + w.settings.unitName = 'bits'; + })); describe('Validate Address', function() { beforeEach(inject(function($compile, $rootScope) { @@ -94,9 +100,12 @@ describe("Unit: Testing Directives", function() { describe('Unit: BTC', function() { beforeEach(inject(function($compile, $rootScope) { - config.unitToSatoshi = 100000000; - config.unitName = 'BTC'; $scope = $rootScope; + var w = new FakeWallet(walletConfig); + w.settings.unitToSatoshi = 100000000; + w.settings.unitName = 'BTC'; + $rootScope.wallet = w; + $rootScope.availableBalance = 0.04; var element = angular.element( '