This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CurrencyUtils.test.ts
76 lines (59 loc) · 2.64 KB
/
CurrencyUtils.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) 2023. Heusala Group Oy <info@hg.fi>. All rights reserved.
import { CurrencyUtils } from "./CurrencyUtils";
import { createCurrencyRates, CurrencyRates } from "./types/CurrencyRates";
import { Currency } from "./types/Currency";
describe('CurrencyUtils', () => {
const validRates: CurrencyRates = createCurrencyRates(1.2, 0.85);
describe('stringifySum', () => {
it('converts a number to a string with two decimal places', () => {
expect(CurrencyUtils.stringifySum(1234.5678)).toEqual('1234.57');
});
});
describe('getSum', () => {
it('calculates the sum of price and amount considering a discount', () => {
expect(CurrencyUtils.getSum(20, 5, 0.10)).toEqual(90);
});
});
describe('getSumWithVat', () => {
it('calculates the total sum including VAT and considering a discount', () => {
expect(CurrencyUtils.getSumWithVat(20, 5, 0.1, 0.10)).toEqual(99);
});
});
describe('getSumWithDiscount', () => {
it('calculates the sum considering a discount', () => {
expect(CurrencyUtils.getSumWithDiscount(200, 0.10)).toEqual(180);
});
});
describe('getVatlessSum', () => {
it('calculates the sum without VAT and considering a discount', () => {
expect(CurrencyUtils.getVatlessSum(110, 0.1, 0.10)).toEqual(90);
});
});
describe('roundByAccuracy', () => {
it('rounds a number by a specified accuracy', () => {
expect(CurrencyUtils.roundByAccuracy(1234.5678, 2)).toEqual(1234.57);
});
});
describe('convertCurrencyAmount', () => {
it('converts a currency amount from one currency to another', () => {
expect(CurrencyUtils.convertCurrencyAmount(validRates, 100, Currency.EUR, Currency.USD, 2)).toEqual(120);
});
it('throws an error when to-currency is invalid', () => {
expect(() => CurrencyUtils.convertCurrencyAmount(validRates, 100, Currency.EUR,
// @ts-ignore
'INVALID',
2)).toThrow('CurrencyService: To: No exchange rate found: INVALID');
});
it('throws an error when from-currency is invalid', () => {
expect(() => CurrencyUtils.convertCurrencyAmount(validRates, 100,
// @ts-ignore
'INVALID',
Currency.USD, 2)).toThrow('CurrencyService: From: No exchange rate found: INVALID');
});
});
describe('getCents', () => {
it('converts a decimal number to cents', () => {
expect(CurrencyUtils.getCents(1234.5678)).toEqual(123457);
});
});
});