forked from Creditra/Creditra-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrency.test.ts
More file actions
75 lines (64 loc) · 2.21 KB
/
Copy pathcurrency.test.ts
File metadata and controls
75 lines (64 loc) · 2.21 KB
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
import { describe, it, expect } from 'vitest';
import {
formatCurrency,
formatCompactCurrency,
roundToCents,
computeMonthlyAccruedInterest,
computeFullPayoffAmount,
formatAmountInputValue,
} from './currency';
describe('formatCurrency', () => {
it('formats a whole-dollar amount in USD by default', () => {
expect(formatCurrency(1234)).toBe('$1,234.00');
});
it('respects an explicit currency code', () => {
const result = formatCurrency(1234, 'EUR', 'en-US');
expect(result).toMatch(/€/);
expect(result).toContain('1,234');
});
it('formats zero as $0.00', () => {
expect(formatCurrency(0)).toBe('$0.00');
});
});
describe('formatCompactCurrency', () => {
it('produces a short representation for thousands', () => {
const result = formatCompactCurrency(1200);
// Locales differ in exactly how compact units are rendered, but the
// string must be shorter than the long form.
expect(result.length).toBeLessThan(formatCurrency(1200).length);
expect(result).toMatch(/K/i);
});
it('produces a short representation for millions', () => {
const result = formatCompactCurrency(3_400_000);
expect(result).toMatch(/M/i);
});
});
describe('roundToCents', () => {
it('rounds to two decimal places', () => {
expect(roundToCents(10.005)).toBe(10.01);
expect(roundToCents(10.004)).toBe(10);
});
});
describe('computeMonthlyAccruedInterest', () => {
it('returns zero for non-positive principal or APR', () => {
expect(computeMonthlyAccruedInterest(0, 12)).toBe(0);
expect(computeMonthlyAccruedInterest(1000, 0)).toBe(0);
});
it('computes monthly interest from principal and APR', () => {
expect(computeMonthlyAccruedInterest(3000, 12)).toBe(30);
});
});
describe('computeFullPayoffAmount', () => {
it('sums principal and accrued monthly interest', () => {
expect(computeFullPayoffAmount(3000, 12)).toBe(3030);
});
it('returns zero when principal is zero', () => {
expect(computeFullPayoffAmount(0, 12)).toBe(0);
});
});
describe('formatAmountInputValue', () => {
it('formats with two fixed decimals', () => {
expect(formatAmountInputValue(3030)).toBe('3030.00');
expect(formatAmountInputValue(10.5)).toBe('10.50');
});
});