Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit fdccadc

Browse files
committed
Add unit tests for helper
1 parent 87b13c9 commit fdccadc

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/helper.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ export const toAmountLabel = (satoshis, settings) => {
134134
* @return {string} The corresponding value label
135135
*/
136136
export const toLabel = (amount, settings) => {
137+
if (!settings) {
138+
throw new Error('Missing args!');
139+
}
137140
const satoshis = toSatoshis(amount, settings.unit);
138141
return toAmountLabel(satoshis, settings);
139142
};

test/unit/helper.spec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,66 @@ describe('Helpers Unit Tests', () => {
450450
});
451451
});
452452

453+
describe('toLabel()', () => {
454+
let settings;
455+
456+
beforeEach(() => {
457+
settings = {
458+
unit: 'btc',
459+
fiat: 'usd',
460+
exchangeRate: { usd: 0.00014503 },
461+
displayFiat: true,
462+
};
463+
});
464+
465+
it('should throw error if amount is undefined', () => {
466+
expect(
467+
helpers.toLabel.bind(null, undefined, settings),
468+
'to throw',
469+
/Missing/
470+
);
471+
});
472+
473+
it('should throw error if amount is null', () => {
474+
expect(helpers.toLabel.bind(null, null, settings), 'to throw', /Missing/);
475+
});
476+
477+
it('should throw error if amount is number', () => {
478+
expect(helpers.toLabel.bind(null, 0.1, settings), 'to throw', /Missing/);
479+
});
480+
481+
it('should throw error if amount is separated with a comma', () => {
482+
expect(
483+
helpers.toLabel.bind(null, '0,1', settings),
484+
'to throw',
485+
/Missing/
486+
);
487+
});
488+
489+
it('should throw error if unit is undefined', () => {
490+
expect(
491+
helpers.toLabel.bind(null, '100', undefined),
492+
'to throw',
493+
/Missing/
494+
);
495+
});
496+
497+
it('should be 0 for empty amount', () => {
498+
const num = helpers.toLabel('', settings);
499+
expect(num, 'to match', /0{1}[,.]0{2}/);
500+
});
501+
502+
it('should work for string input', () => {
503+
const num = helpers.toLabel('0.10', settings);
504+
expect(num, 'to match', /689[,.]51/);
505+
});
506+
it('should format a number value', () => {
507+
settings.displayFiat = false;
508+
const lbl = helpers.toLabel('0.10', settings);
509+
expect(lbl, 'to match', /0[,.]1/);
510+
});
511+
});
512+
453513
describe('toCaps()', () => {
454514
it('should work for undefined', () => {
455515
const caps = helpers.toCaps(undefined);

0 commit comments

Comments
 (0)