You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
3.5 KiB
138 lines
3.5 KiB
'use strict';
|
|
|
|
var should = require('chai').should();
|
|
var bitcore = require('..');
|
|
var Unit = bitcore.Unit;
|
|
|
|
describe('Unit', function() {
|
|
|
|
it('should create an instance', function() {
|
|
var unit;
|
|
|
|
unit = new Unit(1.2, 'BTC');
|
|
should.exist(unit);
|
|
|
|
unit = Unit(1.2, 'BTC');
|
|
should.exist(unit);
|
|
});
|
|
|
|
it('should have property accesors', function() {
|
|
var unit = new Unit(1.2, 'BTC');
|
|
should.exist(unit.BTC);
|
|
should.exist(unit.mBTC);
|
|
should.exist(unit.bits);
|
|
should.exist(unit.satoshis);
|
|
});
|
|
|
|
it('should allow amount as string', function() {
|
|
var unit;
|
|
|
|
unit = Unit.fromBTC('1.00001');
|
|
unit.BTC.should.equal(1.00001);
|
|
|
|
unit = Unit.fromMilis('1.00001');
|
|
unit.mBTC.should.equal(1.00001);
|
|
|
|
unit = Unit.fromBits('100');
|
|
unit.bits.should.equal(100);
|
|
|
|
unit = Unit.fromSatoshis('8999');
|
|
unit.satoshis.should.equal(8999);
|
|
});
|
|
|
|
it('should have constructor helpers', function() {
|
|
var unit;
|
|
|
|
unit = Unit.fromBTC(1.00001);
|
|
unit.BTC.should.equal(1.00001);
|
|
|
|
unit = Unit.fromMilis(1.00001);
|
|
unit.mBTC.should.equal(1.00001);
|
|
|
|
unit = Unit.fromBits(100);
|
|
unit.bits.should.equal(100);
|
|
|
|
unit = Unit.fromSatoshis(8999);
|
|
unit.satoshis.should.equal(8999);
|
|
});
|
|
|
|
it('should convert to satoshis correctly', function() {
|
|
var unit;
|
|
|
|
unit = Unit.fromBTC(1.3);
|
|
unit.mBTC.should.equal(1300);
|
|
unit.bits.should.equal(1300000);
|
|
unit.satoshis.should.equal(130000000);
|
|
|
|
unit = Unit.fromMilis(1.3);
|
|
unit.BTC.should.equal(0.0013);
|
|
unit.bits.should.equal(1300);
|
|
unit.satoshis.should.equal(130000);
|
|
|
|
unit = Unit.fromBits(1.3);
|
|
unit.BTC.should.equal(0.0000013);
|
|
unit.mBTC.should.equal(0.0013);
|
|
unit.satoshis.should.equal(130);
|
|
|
|
unit = Unit.fromSatoshis(3);
|
|
unit.BTC.should.equal(0.00000003);
|
|
unit.mBTC.should.equal(0.00003);
|
|
unit.bits.should.equal(0.03);
|
|
});
|
|
|
|
it('should take in count floating point problems', function() {
|
|
var unit = Unit.fromBTC(0.00000003);
|
|
unit.mBTC.should.equal(0.00003);
|
|
unit.bits.should.equal(0.03);
|
|
unit.satoshis.should.equal(3);
|
|
});
|
|
|
|
it('should expose unit codes', function() {
|
|
should.exist(Unit.BTC);
|
|
Unit.BTC.should.equal('BTC');
|
|
|
|
should.exist(Unit.mBTC);
|
|
Unit.mBTC.should.equal('mBTC');
|
|
|
|
should.exist(Unit.bits);
|
|
Unit.bits.should.equal('bits');
|
|
|
|
should.exist(Unit.satoshis);
|
|
Unit.satoshis.should.equal('satoshis');
|
|
});
|
|
|
|
it('should expose shorthand conversion methods', function() {
|
|
var unit = new Unit(1.3, 'BTC');
|
|
unit.toBTC().should.equal(unit.BTC);
|
|
unit.toMilis().should.equal(unit.mBTC);
|
|
unit.toBits().should.equal(unit.bits);
|
|
unit.toSatoshis().should.equal(unit.satoshis);
|
|
});
|
|
|
|
it('should expose a general conversion method', function() {
|
|
var unit = new Unit(1.3, 'BTC');
|
|
unit.to(Unit.BTC).should.equal(unit.BTC);
|
|
unit.to(Unit.mBTC).should.equal(unit.mBTC);
|
|
unit.to(Unit.bits).should.equal(unit.bits);
|
|
unit.to(Unit.satoshis).should.equal(unit.satoshis);
|
|
});
|
|
|
|
it('should have a toString method', function() {
|
|
var unit = new Unit(1.3, 'BTC');
|
|
should.exist(unit.toString);
|
|
unit.toString().should.be.a('string');
|
|
});
|
|
|
|
it('should input/output JSON', function() {
|
|
var json = JSON.stringify({amount:1.3, code:'BTC'});
|
|
var unit = Unit.fromJSON(json);
|
|
unit.toJSON().should.deep.equal(json);
|
|
});
|
|
|
|
it('should have an inspect method', function() {
|
|
var unit = new Unit(1.3, 'BTC');
|
|
should.exist(unit.inspect);
|
|
unit.inspect().should.be.a('string');
|
|
});
|
|
|
|
});
|
|
|