fix some karma tests problems

This commit is contained in:
Manuel Araoz 2014-08-13 18:07:57 -04:00
commit 0067ab4da6
2 changed files with 98 additions and 142 deletions

View file

@ -1,103 +1,96 @@
'use strict'; 'use strict';
angular.module('copayApp.directives') angular.module('copayApp.directives')
.directive('validAddress', ['$rootScope', 'notification', .directive('validAddress', function($rootScope, notification) {
function($rootScope, notification) { var bitcore = require('bitcore');
var Address = bitcore.Address;
var bignum = bitcore.Bignum;
var bitcore = require('bitcore'); return {
var Address = bitcore.Address; require: 'ngModel',
var bignum = bitcore.Bignum; link: function(scope, elem, attrs, ctrl) {
var validator = function(value) {
var uri;
return { if (/^https?:\/\//.test(value)) {
require: 'ngModel', uri = {
link: function(scope, elem, attrs, ctrl) { merchant: value
var validator = function(value) { };
var uri; } else {
uri = copay.HDPath.parseBitcoinURI(value);
}
if (/^https?:\/\//.test(value)) { // Regular Address
uri = { merchant: value }; if (!uri || !uri.merchant) {
} else { var a = new Address(value);
uri = copay.HDPath.parseBitcoinURI(value); ctrl.$setValidity('validAddress', a.isValid() && a.network().name === config.networkName);
return value;
}
notification.info('Fetching Payment',
'Retrieving Payment Request from ' + uri.merchant);
// Payment Protocol URI (BIP-72)
scope.wallet.fetchPaymentTx(uri.merchant, function(err, merchantData) {
var balance = $rootScope.availableBalance;
var available = +(balance * config.unitToSatoshi).toFixed(0);
if (merchantData && available < +merchantData.total) {
err = new Error('No unspent outputs available.');
} }
// Regular Address if (err) {
if (!uri || !uri.merchant) { scope.sendForm.address.$isValid = false;
var a = new Address(value); notification.error('Error', err.message || 'Bad payment server.');
ctrl.$setValidity('validAddress', a.isValid() && a.network().name === config.networkName); return;
return value;
} }
notification.info('Fetching Payment', merchantData.unitTotal = (+merchantData.total / config.unitToSatoshi) + '';
'Retrieving Payment Request from ' + uri.merchant); merchantData.expiration = new Date(
merchantData.pr.pd.expires * 1000).toISOString();
// Payment Protocol URI (BIP-72) $rootScope.merchant = merchantData;
scope.wallet.fetchPaymentTx(uri.merchant, function(err, merchantData) {
var balance = $rootScope.availableBalance;
var available = +(balance * config.unitToSatoshi).toFixed(0);
if (merchantData && available < +merchantData.total) { scope.sendForm.address.$isValid = true;
err = new Error('No unspent outputs available.');
}
if (err) { scope.sendForm.amount.$setViewValue(merchantData.unitTotal);
scope.sendForm.address.$isValid = false; scope.sendForm.amount.$render();
notification.error('Error', err.message || 'Bad payment server.'); scope.sendForm.amount.$isValid = true;
return;
}
merchantData.unitTotal = (+merchantData.total / config.unitToSatoshi) + ''; // If the address changes to a non-payment-protocol one,
merchantData.expiration = new Date( // delete the `merchant` property from the scope.
merchantData.pr.pd.expires * 1000).toISOString(); var unregister = scope.$watch('address', function() {
var val = scope.sendForm.address.$viewValue || '';
$rootScope.merchant = merchantData; var uri = copay.HDPath.parseBitcoinURI(val);
if (!uri || !uri.merchant) {
scope.sendForm.address.$isValid = true; delete $rootScope.merchant;
scope.sendForm.amount.$setViewValue('');
scope.sendForm.amount.$setViewValue(merchantData.unitTotal); scope.sendForm.amount.$render();
scope.sendForm.amount.$render(); unregister();
scope.sendForm.amount.$isValid = true; if ($rootScope.$$phase !== '$apply' && $rootScope.$$phase !== '$digest') {
$rootScope.$apply();
// If the address changes to a non-payment-protocol one,
// delete the `merchant` property from the scope.
var unregister = scope.$watch('address', function() {
var val = scope.sendForm.address.$viewValue || '';
var uri = copay.HDPath.parseBitcoinURI(val);
if (!uri || !uri.merchant) {
delete $rootScope.merchant;
scope.sendForm.amount.$setViewValue('');
scope.sendForm.amount.$render();
unregister();
if ($rootScope.$$phase !== '$apply'
&& $rootScope.$$phase !== '$digest') {
$rootScope.$apply();
}
} }
});
if ($rootScope.$$phase !== '$apply'
&& $rootScope.$$phase !== '$digest') {
$rootScope.$apply();
} }
notification.info('Payment Request',
'Server is requesting '
+ merchantData.unitTotal + ' '
+ config.unitName + '.'
+ ' Message: '
+ merchantData.pr.pd.memo);
}); });
ctrl.$setValidity('validAddress', true); if ($rootScope.$$phase !== '$apply' && $rootScope.$$phase !== '$digest') {
$rootScope.$apply();
}
return 'Merchant: ' + uri.merchant; notification.info('Payment Request',
}; 'Server is requesting ' + merchantData.unitTotal + ' ' + config.unitName + '.' + ' Message: ' + merchantData.pr.pd.memo);
});
ctrl.$parsers.unshift(validator); ctrl.$setValidity('validAddress', true);
ctrl.$formatters.unshift(validator);
} return 'Merchant: ' + uri.merchant;
}; };
}
]) ctrl.$parsers.unshift(validator);
ctrl.$formatters.unshift(validator);
}
};
})
.directive('enoughAmount', ['$rootScope', .directive('enoughAmount', ['$rootScope',
function($rootScope) { function($rootScope) {
var bitcore = require('bitcore'); var bitcore = require('bitcore');
@ -278,32 +271,34 @@ angular.module('copayApp.directives')
restrict: 'A', restrict: 'A',
link: function(scope, element, attrs) { link: function(scope, element, attrs) {
element.bind('click', function() { element.bind('click', function() {
window.open('bitcoin:'+attrs.address, '_blank'); window.open('bitcoin:' + attrs.address, '_blank');
}); });
} }
} }
}) })
// From https://gist.github.com/asafge/7430497 // From https://gist.github.com/asafge/7430497
.directive('ngReallyClick', [function() { .directive('ngReallyClick', [
return {
restrict: 'A', function() {
link: function(scope, element, attrs) { return {
element.bind('click', function() { restrict: 'A',
var message = attrs.ngReallyMessage; link: function(scope, element, attrs) {
if (message && confirm(message)) { element.bind('click', function() {
scope.$apply(attrs.ngReallyClick); var message = attrs.ngReallyMessage;
} if (message && confirm(message)) {
}); scope.$apply(attrs.ngReallyClick);
} }
});
} }
} }
]) }
.directive('match', function () { ])
.directive('match', function() {
return { return {
require: 'ngModel', require: 'ngModel',
restrict: 'A', restrict: 'A',
scope: { scope: {
match: '=' match: '='
}, },
link: function(scope, elem, attrs, ctrl) { link: function(scope, elem, attrs, ctrl) {
scope.$watch(function() { scope.$watch(function() {
@ -324,16 +319,18 @@ angular.module('copayApp.directives')
return { return {
restric: 'A', restric: 'A',
scope: { clipCopy: '=clipCopy' }, scope: {
clipCopy: '=clipCopy'
},
link: function(scope, elm) { link: function(scope, elm) {
var client = new ZeroClipboard(elm); var client = new ZeroClipboard(elm);
client.on( 'ready', function(event) { client.on('ready', function(event) {
client.on( 'copy', function(event) { client.on('copy', function(event) {
event.clipboardData.setData('text/plain', scope.clipCopy); event.clipboardData.setData('text/plain', scope.clipCopy);
}); });
client.on( 'aftercopy', function(event) { client.on('aftercopy', function(event) {
elm.removeClass('btn-copy').addClass('btn-copied').html('Copied!'); elm.removeClass('btn-copy').addClass('btn-copied').html('Copied!');
setTimeout(function() { setTimeout(function() {
elm.addClass('btn-copy').removeClass('btn-copied').html(''); elm.addClass('btn-copy').removeClass('btn-copied').html('');
@ -341,8 +338,8 @@ angular.module('copayApp.directives')
}); });
}); });
client.on( 'error', function(event) { client.on('error', function(event) {
console.log( 'ZeroClipboard error of type "' + event.name + '": ' + event.message ); console.log('ZeroClipboard error of type "' + event.name + '": ' + event.message);
ZeroClipboard.destroy(); ZeroClipboard.destroy();
}); });
} }

View file

@ -221,47 +221,6 @@ describe("Unit: Controllers", function() {
sinon.assert.callCount(scope.loadTxs, 1); sinon.assert.callCount(scope.loadTxs, 1);
}); });
it('#start the example server', function(done) {
startServer(function(err, s) {
if (err) return done(err);
server = s;
server.uri = 'https://localhost:8080/-';
done();
});
});
it('#create a payment protocol transaction proposal', function() {
var uri = 'bitcoin:1JqniWpWNA6Yvdivg3y9izLidETnurxRQm?amount=0.00001000&r=https://localhost:8080/-/request';
sendForm.address.$setViewValue(uri);
sendForm.amount.$setViewValue(1000);
scope.wallet.totalCopayers = scope.wallet.requiredCopayers = 3;
var spy = sinon.spy(scope.wallet, 'createTx');
var spy2 = sinon.spy(scope.wallet, 'sendTx');
scope.submitForm(sendForm);
sinon.assert.callCount(spy, 1);
sinon.assert.callCount(spy2, 0);
});
it('#create and send a payment protocol transaction proposal', function() {
var uri = 'bitcoin:1JqniWpWNA6Yvdivg3y9izLidETnurxRQm?amount=0.00001000&r=https://localhost:8080/-/request';
sendForm.address.$setViewValue(uri);
sendForm.amount.$setViewValue(1000);
scope.wallet.totalCopayers = scope.wallet.requiredCopayers = 1;
var spy = sinon.spy(scope.wallet, 'createTx');
var spy2 = sinon.spy(scope.wallet, 'sendTx');
scope.submitForm(sendForm);
sinon.assert.callCount(spy, 1);
sinon.assert.callCount(spy2, 1);
});
it('#stop the example server', function(done) {
server.close(function() {
done();
});
});
}); });
describe("Unit: Version Controller", function() { describe("Unit: Version Controller", function() {