mirror of https://github.com/BTCPrivate/copay.git
all test pass!
This commit is contained in:
parent
cb8f719a99
commit
4241bdac68
|
@ -13,6 +13,70 @@ var WalletFactory = require('../js/models/core/WalletFactory');
|
||||||
var Passphrase = require('../js/models/core/Passphrase');
|
var Passphrase = require('../js/models/core/Passphrase');
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A better way to compare two objects in Javascript
|
||||||
|
**/
|
||||||
|
function getKeys(obj) {
|
||||||
|
var keys;
|
||||||
|
if(obj.keys) {
|
||||||
|
keys = obj.keys();
|
||||||
|
} else {
|
||||||
|
keys = [];
|
||||||
|
|
||||||
|
for(var k in obj) {
|
||||||
|
if(Object.prototype.hasOwnProperty.call(obj, k)) {
|
||||||
|
keys.push(k);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return keys;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new object so the keys appear in the provided order.
|
||||||
|
* @param {Object} obj The object to be the base for the new object
|
||||||
|
* @param {Array} keys The order in which properties of the new object should appear
|
||||||
|
**/
|
||||||
|
function reconstructObject(obj, keys) {
|
||||||
|
var result = {};
|
||||||
|
for (var i = 0, l = keys.length; i < l; i++) {
|
||||||
|
if (Object.prototype.hasOwnProperty.call(obj, keys[i])) {
|
||||||
|
result[keys[i]] = obj[keys[i]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function assertObjectEqual(a, b, msg) {
|
||||||
|
msg = msg || '';
|
||||||
|
if( Object.prototype.toString.call( a ) === '[object Array]' && Object.prototype.toString.call( b ) === '[object Array]') {
|
||||||
|
// special case: array of objects
|
||||||
|
if (a.filter(function(e) { return Object.prototype.toString.call( e ) === '[object Object]' }).length > 0 ||
|
||||||
|
b.filter(function(e) { return Object.prototype.toString.call( e ) === '[object Object]' }).length > 0 ){
|
||||||
|
|
||||||
|
if (a.length !== b.length) {
|
||||||
|
JSON.stringify(a).should.equal(JSON.stringify(b), msg);
|
||||||
|
} else {
|
||||||
|
for(var i = 0, l = a.length; i < l; i++) {
|
||||||
|
assertObjectEqual(a[i], b[i], msg + '[elements at index ' + i + ' should be equal]');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// simple array of primitives
|
||||||
|
} else {
|
||||||
|
JSON.stringify(a).should.equal(JSON.stringify(b), msg);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var orderedA = reconstructObject(a, getKeys(a).sort()),
|
||||||
|
orderedB = reconstructObject(b, getKeys(b).sort());
|
||||||
|
|
||||||
|
// compare as strings for diff tolls to show us the difference
|
||||||
|
JSON.stringify(orderedA).should.equal(JSON.stringify(orderedB), msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
describe('WalletFactory model', function() {
|
describe('WalletFactory model', function() {
|
||||||
var config = {
|
var config = {
|
||||||
Network: FakeNetwork,
|
Network: FakeNetwork,
|
||||||
|
@ -83,7 +147,23 @@ describe('WalletFactory model', function() {
|
||||||
w2.id.should.equal(w.id);
|
w2.id.should.equal(w.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('#fromObj #toObj round trip', function() {
|
it('#fromObj #toObj round trip', function() {
|
||||||
|
var wf = new WalletFactory(config, '0.0.5');
|
||||||
|
var o2 = o.replace(/cosigner/g,'copayerIndex');
|
||||||
|
var w = wf.fromObj(JSON.parse(o2));
|
||||||
|
|
||||||
|
should.exist(w);
|
||||||
|
w.id.should.equal("dbfe10c3fae71cea");
|
||||||
|
should.exist(w.publicKeyRing.getCopayerId);
|
||||||
|
should.exist(w.txProposals.toObj());
|
||||||
|
should.exist(w.privateKey.toObj());
|
||||||
|
assertObjectEqual(w.toObj(), JSON.parse(o2));
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
it('#fromObj #toObj round trip, using old cosigner', function() {
|
||||||
var wf = new WalletFactory(config, '0.0.5');
|
var wf = new WalletFactory(config, '0.0.5');
|
||||||
var w = wf.fromObj(JSON.parse(o));
|
var w = wf.fromObj(JSON.parse(o));
|
||||||
|
|
||||||
|
@ -92,8 +172,8 @@ describe('WalletFactory model', function() {
|
||||||
should.exist(w.publicKeyRing.getCopayerId);
|
should.exist(w.publicKeyRing.getCopayerId);
|
||||||
should.exist(w.txProposals.toObj());
|
should.exist(w.txProposals.toObj());
|
||||||
should.exist(w.privateKey.toObj());
|
should.exist(w.privateKey.toObj());
|
||||||
|
var expected = JSON.parse(o.replace(/cosigner/g,'copayerIndex'));
|
||||||
JSON.stringify(w.toObj()).should.equal(o);
|
assertObjectEqual(w.toObj(), expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('support old index schema: #fromObj #toObj round trip', function() {
|
it('support old index schema: #fromObj #toObj round trip', function() {
|
||||||
|
@ -108,6 +188,10 @@ describe('WalletFactory model', function() {
|
||||||
should.exist(w.publicKeyRing.getCopayerId);
|
should.exist(w.publicKeyRing.getCopayerId);
|
||||||
should.exist(w.txProposals.toObj);
|
should.exist(w.txProposals.toObj);
|
||||||
should.exist(w.privateKey.toObj);
|
should.exist(w.privateKey.toObj);
|
||||||
|
|
||||||
|
//
|
||||||
|
var expected = JSON.parse(o2.replace(/cosigner/g,'copayerIndex'));
|
||||||
|
assertObjectEqual(w.toObj(), expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create wallet from encrypted object', function() {
|
it('should create wallet from encrypted object', function() {
|
||||||
|
|
Loading…
Reference in New Issue