1 | 'use strict';
|
2 |
|
3 | var should = require('chai').should();
|
4 | var expect = require('chai').expect;
|
5 |
|
6 | var bitcore = require('..');
|
7 | var errors = bitcore.errors;
|
8 | var Unit = bitcore.Unit;
|
9 |
|
10 | describe('Unit', function() {
|
11 |
|
12 | it('can be created from a number and unit', function() {
|
13 | expect(function() {
|
14 | return new Unit(1.2, 'BTC');
|
15 | }).to.not.throw();
|
16 | });
|
17 |
|
18 | it('can be created from a number and exchange rate', function() {
|
19 | expect(function() {
|
20 | return new Unit(1.2, 350);
|
21 | }).to.not.throw();
|
22 | });
|
23 |
|
24 | it('no "new" is required for creating an instance', function() {
|
25 | expect(function() {
|
26 | return Unit(1.2, 'BTC');
|
27 | }).to.not.throw();
|
28 |
|
29 | expect(function() {
|
30 | return Unit(1.2, 350);
|
31 | }).to.not.throw();
|
32 | });
|
33 |
|
34 | it('has property accesors "BTC", "mBTC", "uBTC", "bits", and "satoshis"', function() {
|
35 | var unit = new Unit(1.2, 'BTC');
|
36 | unit.BTC.should.equal(1.2);
|
37 | unit.mBTC.should.equal(1200);
|
38 | unit.uBTC.should.equal(1200000);
|
39 | unit.bits.should.equal(1200000);
|
40 | unit.satoshis.should.equal(120000000);
|
41 | });
|
42 |
|
43 | it('a string amount is allowed', function() {
|
44 | var unit;
|
45 |
|
46 | unit = Unit.fromBTC('1.00001');
|
47 | unit.BTC.should.equal(1.00001);
|
48 |
|
49 | unit = Unit.fromMilis('1.00001');
|
50 | unit.mBTC.should.equal(1.00001);
|
51 |
|
52 | unit = Unit.fromMillis('1.00001');
|
53 | unit.mBTC.should.equal(1.00001);
|
54 |
|
55 | unit = Unit.fromBits('100');
|
56 | unit.bits.should.equal(100);
|
57 |
|
58 | unit = Unit.fromSatoshis('8999');
|
59 | unit.satoshis.should.equal(8999);
|
60 |
|
61 | unit = Unit.fromFiat('43', 350);
|
62 | unit.BTC.should.equal(0.12285714);
|
63 | });
|
64 |
|
65 | it('should have constructor helpers', function() {
|
66 | var unit;
|
67 |
|
68 | unit = Unit.fromBTC(1.00001);
|
69 | unit.BTC.should.equal(1.00001);
|
70 |
|
71 | unit = Unit.fromMilis(1.00001);
|
72 | unit.mBTC.should.equal(1.00001);
|
73 |
|
74 | unit = Unit.fromBits(100);
|
75 | unit.bits.should.equal(100);
|
76 |
|
77 | unit = Unit.fromSatoshis(8999);
|
78 | unit.satoshis.should.equal(8999);
|
79 |
|
80 | unit = Unit.fromFiat(43, 350);
|
81 | unit.BTC.should.equal(0.12285714);
|
82 | });
|
83 |
|
84 | it('converts to satoshis correctly', function() {
|
85 |
|
86 | var unit;
|
87 |
|
88 | unit = Unit.fromBTC(1.3);
|
89 | unit.mBTC.should.equal(1300);
|
90 | unit.bits.should.equal(1300000);
|
91 | unit.satoshis.should.equal(130000000);
|
92 |
|
93 | unit = Unit.fromMilis(1.3);
|
94 | unit.BTC.should.equal(0.0013);
|
95 | unit.bits.should.equal(1300);
|
96 | unit.satoshis.should.equal(130000);
|
97 |
|
98 | unit = Unit.fromBits(1.3);
|
99 | unit.BTC.should.equal(0.0000013);
|
100 | unit.mBTC.should.equal(0.0013);
|
101 | unit.satoshis.should.equal(130);
|
102 |
|
103 | unit = Unit.fromSatoshis(3);
|
104 | unit.BTC.should.equal(0.00000003);
|
105 | unit.mBTC.should.equal(0.00003);
|
106 | unit.bits.should.equal(0.03);
|
107 | });
|
108 |
|
109 | it('takes into account floating point problems', function() {
|
110 | var unit = Unit.fromBTC(0.00000003);
|
111 | unit.mBTC.should.equal(0.00003);
|
112 | unit.bits.should.equal(0.03);
|
113 | unit.satoshis.should.equal(3);
|
114 | });
|
115 |
|
116 | it('exposes unit codes', function() {
|
117 | should.exist(Unit.BTC);
|
118 | Unit.BTC.should.equal('BTC');
|
119 |
|
120 | should.exist(Unit.mBTC);
|
121 | Unit.mBTC.should.equal('mBTC');
|
122 |
|
123 | should.exist(Unit.bits);
|
124 | Unit.bits.should.equal('bits');
|
125 |
|
126 | should.exist(Unit.satoshis);
|
127 | Unit.satoshis.should.equal('satoshis');
|
128 | });
|
129 |
|
130 | it('exposes a method that converts to different units', function() {
|
131 | var unit = new Unit(1.3, 'BTC');
|
132 | unit.to(Unit.BTC).should.equal(unit.BTC);
|
133 | unit.to(Unit.mBTC).should.equal(unit.mBTC);
|
134 | unit.to(Unit.bits).should.equal(unit.bits);
|
135 | unit.to(Unit.satoshis).should.equal(unit.satoshis);
|
136 | });
|
137 |
|
138 | it('exposes shorthand conversion methods', function() {
|
139 | var unit = new Unit(1.3, 'BTC');
|
140 | unit.toBTC().should.equal(unit.BTC);
|
141 | unit.toMilis().should.equal(unit.mBTC);
|
142 | unit.toMillis().should.equal(unit.mBTC);
|
143 | unit.toBits().should.equal(unit.bits);
|
144 | unit.toSatoshis().should.equal(unit.satoshis);
|
145 | });
|
146 |
|
147 | it('can convert to fiat', function() {
|
148 | var unit = new Unit(1.3, 350);
|
149 | unit.atRate(350).should.equal(1.3);
|
150 | unit.to(350).should.equal(1.3);
|
151 |
|
152 | unit = Unit.fromBTC(0.0123);
|
153 | unit.atRate(10).should.equal(0.12);
|
154 | });
|
155 |
|
156 | it('toString works as expected', function() {
|
157 | var unit = new Unit(1.3, 'BTC');
|
158 | should.exist(unit.toString);
|
159 | unit.toString().should.be.a('string');
|
160 | });
|
161 |
|
162 | it('can be imported and exported from/to JSON', function() {
|
163 | var json = JSON.stringify({amount:1.3, code:'BTC'});
|
164 | var unit = Unit.fromObject(JSON.parse(json));
|
165 | JSON.stringify(unit).should.deep.equal(json);
|
166 | });
|
167 |
|
168 | it('importing from invalid JSON fails quickly', function() {
|
169 | expect(function() {
|
170 | return Unit.fromJSON('¹');
|
171 | }).to.throw();
|
172 | });
|
173 |
|
174 | it('inspect method displays nicely', function() {
|
175 | var unit = new Unit(1.3, 'BTC');
|
176 | unit.inspect().should.equal('<Unit: 130000000 satoshis>');
|
177 | });
|
178 |
|
179 | it('fails when the unit is not recognized', function() {
|
180 | expect(function() {
|
181 | return new Unit(100, 'USD');
|
182 | }).to.throw(errors.Unit.UnknownCode);
|
183 | expect(function() {
|
184 | return new Unit(100, 'BTC').to('USD');
|
185 | }).to.throw(errors.Unit.UnknownCode);
|
186 | });
|
187 |
|
188 | it('fails when the exchange rate is invalid', function() {
|
189 | expect(function() {
|
190 | return new Unit(100, -123);
|
191 | }).to.throw(errors.Unit.InvalidRate);
|
192 | expect(function() {
|
193 | return new Unit(100, 'BTC').atRate(-123);
|
194 | }).to.throw(errors.Unit.InvalidRate);
|
195 | });
|
196 |
|
197 | });
|