Adding tests to TxPrpposals

This commit is contained in:
Matias Pando 2014-12-18 07:13:39 -03:00
commit 8486fe9b69

View file

@ -11,6 +11,7 @@ var networks = bitcore.networks;
var TxProposal = copay.TxProposal; var TxProposal = copay.TxProposal;
var TxProposals = copay.TxProposals; var TxProposals = copay.TxProposals;
var moment = moment || require('moment');
var dummyProposal = new TxProposal({ var dummyProposal = new TxProposal({
creator: 1, creator: 1,
@ -34,41 +35,98 @@ describe('TxProposals', function() {
describe('#fromObj', function() { describe('#fromObj', function() {
it('should create an instance from an Object', function() { it('should create an instance from an Object', function() {
var txps = TxProposals.fromObj({ var txps = TxProposals.fromObj({
networkName:'livenet', networkName: 'livenet',
walletId: '123a12', walletId: '123a12',
txps: [], txps: [],
}); });
should.exist(txps); should.exist(txps);
txps.network.name.should.equal('livenet'); txps.network.name.should.equal('livenet');
}); });
it('should skip Objects with errors', function() { it('should skip Objects with errors', function() {
var txps = TxProposals.fromObj({ var txps = TxProposals.fromObj({
networkName:'livenet', networkName: 'livenet',
walletId: '123a12', walletId: '123a12',
txps: [ { a: 1 }], txps: [{
a: 1
}],
}); });
should.exist(txps); should.exist(txps);
Object.keys(txps.txps).length.should.equal(0); Object.keys(txps.txps).length.should.equal(0);
}); });
}); });
describe('#length', function() {
it('should return length', function() {
var txps = new TxProposals();
txps.txps = {
a: 1,
b: 2
};
txps.length().should.equal(2);
});
});
describe('#getNtxidsSince', function() {
it('should throw illegal argument', function() {
var txps = new TxProposals();
txps.txps = {
a: 1,
b: 2
};
(function() {
txps.getNtxidsSince()
}).should.throw('Illegal Argument');
});
it('should return keys since a date', function() {
var today = moment().toDate();
var today_plus_1 = moment().add(1, 'day').toDate();
var today_plus_2 = moment().add(2, 'day').toDate();
var today_plus_3 = moment().add(3, 'day').toDate();
var txps = new TxProposals();
txps.txps = [{
id: 1,
createdTs: today
}, {
id: 2,
createdTs: today_plus_1
}, {
id: 3,
createdTs: today_plus_2
}];
txps.getNtxidsSince(today).length.should.be.equal(3);
txps.getNtxidsSince(today_plus_1).length.should.be.equal(2);
txps.getNtxidsSince(today_plus_2).length.should.be.equal(1);
txps.getNtxidsSince(today_plus_3).length.should.be.equal(0);
});
});
describe('#getNtxids', function() { describe('#getNtxids', function() {
it('should return keys', function() { it('should return keys', function() {
var txps = new TxProposals(); var txps = new TxProposals();
txps.txps = {a:1, b:2}; txps.txps = {
txps.getNtxids().should.deep.equal(['a','b']); a: 1,
b: 2
};
txps.getNtxids().should.deep.equal(['a', 'b']);
}); });
}); });
describe('#deleteOne', function() { describe('#deleteOne', function() {
it('should delete specified ntxid', function() { it('should delete specified ntxid', function() {
var txps = new TxProposals(); var txps = new TxProposals();
txps.txps = {a:1, b:2}; txps.txps = {
a: 1,
b: 2
};
txps.deleteOne('a'); txps.deleteOne('a');
txps.getNtxids().should.deep.equal(['b']); txps.getNtxids().should.deep.equal(['b']);
}); });
it('should fail on non-existent ntxid', function() { it('should fail on non-existent ntxid', function() {
var txps = new TxProposals(); var txps = new TxProposals();
txps.txps = {a:1, b:2}; txps.txps = {
(function () { a: 1,
b: 2
};
(function() {
txps.deleteOne('c'); txps.deleteOne('c');
}).should.throw('Unknown TXP: c'); }).should.throw('Unknown TXP: c');
}); });
@ -76,7 +134,7 @@ describe('TxProposals', function() {
describe('#toObj', function() { describe('#toObj', function() {
it('should an object', function() { it('should an object', function() {
var txps = TxProposals.fromObj({ var txps = TxProposals.fromObj({
networkName:'livenet', networkName: 'livenet',
walletId: '123a12', walletId: '123a12',
txps: [], txps: [],
}); });
@ -86,28 +144,28 @@ describe('TxProposals', function() {
}); });
it('should export txps', function() { it('should export txps', function() {
var txps = TxProposals.fromObj({ var txps = TxProposals.fromObj({
networkName:'livenet', networkName: 'livenet',
walletId: '123a12', walletId: '123a12',
txps: [], txps: [],
}); });
txps.txps = { txps.txps = {
'hola' : dummyProposal, 'hola': dummyProposal,
'chau' : dummyProposal, 'chau': dummyProposal,
}; };
var o = txps.toObj(); var o = txps.toObj();
o.txps.length.should.equal(2); o.txps.length.should.equal(2);
}); });
it('should filter sent txp', function() { it('should filter sent txp', function() {
var txps = TxProposals.fromObj({ var txps = TxProposals.fromObj({
networkName:'livenet', networkName: 'livenet',
walletId: '123a12', walletId: '123a12',
txps: [], txps: [],
}); });
var d = JSON.parse(JSON.stringify(dummyProposal)); var d = JSON.parse(JSON.stringify(dummyProposal));
d.sent=1; d.sent = 1;
txps.txps = { txps.txps = {
'hola' : dummyProposal, 'hola': dummyProposal,
'chau' : d, 'chau': d,
}; };
var o = txps.toObj(); var o = txps.toObj();
o.txps.length.should.equal(1); o.txps.length.should.equal(1);
@ -117,8 +175,7 @@ describe('TxProposals', function() {
it('should merge', function() { it('should merge', function() {
var txps = new TxProposals(); var txps = new TxProposals();
var d = dummyProposal; var d = dummyProposal;
txps.merge(d.toObj(),{}); txps.merge(d.toObj(), {});
}); });
}); });
}); });