From 8486fe9b69ec9dd9d8828e4c445e589c6a4dac90 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Thu, 18 Dec 2014 07:13:39 -0300 Subject: [PATCH] Adding tests to TxPrpposals --- test/TxProposals.js | 95 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 76 insertions(+), 19 deletions(-) diff --git a/test/TxProposals.js b/test/TxProposals.js index 13a2837e5..f01a084ae 100644 --- a/test/TxProposals.js +++ b/test/TxProposals.js @@ -11,6 +11,7 @@ var networks = bitcore.networks; var TxProposal = copay.TxProposal; var TxProposals = copay.TxProposals; +var moment = moment || require('moment'); var dummyProposal = new TxProposal({ creator: 1, @@ -34,41 +35,98 @@ describe('TxProposals', function() { describe('#fromObj', function() { it('should create an instance from an Object', function() { var txps = TxProposals.fromObj({ - networkName:'livenet', + networkName: 'livenet', walletId: '123a12', txps: [], }); should.exist(txps); txps.network.name.should.equal('livenet'); }); - it('should skip Objects with errors', function() { + it('should skip Objects with errors', function() { var txps = TxProposals.fromObj({ - networkName:'livenet', + networkName: 'livenet', walletId: '123a12', - txps: [ { a: 1 }], + txps: [{ + a: 1 + }], }); should.exist(txps); 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() { it('should return keys', function() { var txps = new TxProposals(); - txps.txps = {a:1, b:2}; - txps.getNtxids().should.deep.equal(['a','b']); + txps.txps = { + a: 1, + b: 2 + }; + txps.getNtxids().should.deep.equal(['a', 'b']); }); }); describe('#deleteOne', function() { it('should delete specified ntxid', function() { var txps = new TxProposals(); - txps.txps = {a:1, b:2}; + txps.txps = { + a: 1, + b: 2 + }; txps.deleteOne('a'); txps.getNtxids().should.deep.equal(['b']); }); it('should fail on non-existent ntxid', function() { var txps = new TxProposals(); - txps.txps = {a:1, b:2}; - (function () { + txps.txps = { + a: 1, + b: 2 + }; + (function() { txps.deleteOne('c'); }).should.throw('Unknown TXP: c'); }); @@ -76,7 +134,7 @@ describe('TxProposals', function() { describe('#toObj', function() { it('should an object', function() { var txps = TxProposals.fromObj({ - networkName:'livenet', + networkName: 'livenet', walletId: '123a12', txps: [], }); @@ -86,28 +144,28 @@ describe('TxProposals', function() { }); it('should export txps', function() { var txps = TxProposals.fromObj({ - networkName:'livenet', + networkName: 'livenet', walletId: '123a12', txps: [], }); txps.txps = { - 'hola' : dummyProposal, - 'chau' : dummyProposal, + 'hola': dummyProposal, + 'chau': dummyProposal, }; var o = txps.toObj(); o.txps.length.should.equal(2); }); it('should filter sent txp', function() { var txps = TxProposals.fromObj({ - networkName:'livenet', + networkName: 'livenet', walletId: '123a12', txps: [], }); var d = JSON.parse(JSON.stringify(dummyProposal)); - d.sent=1; + d.sent = 1; txps.txps = { - 'hola' : dummyProposal, - 'chau' : d, + 'hola': dummyProposal, + 'chau': d, }; var o = txps.toObj(); o.txps.length.should.equal(1); @@ -117,8 +175,7 @@ describe('TxProposals', function() { it('should merge', function() { var txps = new TxProposals(); var d = dummyProposal; - txps.merge(d.toObj(),{}); + txps.merge(d.toObj(), {}); }); }); }); -