forked from Creditra/Creditra-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamountValidation.test.ts
More file actions
103 lines (86 loc) · 3.64 KB
/
Copy pathamountValidation.test.ts
File metadata and controls
103 lines (86 loc) · 3.64 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { describe, expect, it, vi } from 'vitest';
import {
getDrawAmountValidation,
getRepayAmountValidation,
REPAY_CONFIRM_THRESHOLD,
requiresRepayConfirmation,
} from './amountValidation';
describe('getDrawAmountValidation', () => {
const creditLine = { limit: 50000, available: 35000 };
it('returns a warning when a draw would leave less than the reserve target', () => {
const result = getDrawAmountValidation('32000', creditLine);
expect(result.isValid).toBe(true);
expect(result.feedback.severity).toBe('warning');
expect(result.feedback.title).toBe('Below recommended reserve');
});
it('returns an error when the amount exceeds available credit', () => {
const result = getDrawAmountValidation('36000', creditLine);
expect(result.isValid).toBe(false);
expect(result.feedback.severity).toBe('danger');
expect(result.feedback.title).toBe('Exceeds available credit');
});
});
describe('getRepayAmountValidation', () => {
it('returns an error when the amount exceeds wallet balance', () => {
const result = getRepayAmountValidation('6000', 8000, 4000);
expect(result.isValid).toBe(false);
expect(result.feedback.severity).toBe('danger');
expect(result.feedback.title).toBe('Exceeds wallet balance');
});
it('returns a warning when repayment leaves too little wallet reserve', () => {
const result = getRepayAmountValidation('3900', 8000, 4000);
expect(result.isValid).toBe(true);
expect(result.feedback.severity).toBe('warning');
expect(result.feedback.title).toBe('Low wallet reserve');
});
});
describe('REPAY_CONFIRM_THRESHOLD', () => {
it('defaults to 5000 when VITE_REPAY_CONFIRM_THRESHOLD is not set', () => {
expect(REPAY_CONFIRM_THRESHOLD).toBe(5000);
});
it('reads a custom value from VITE_REPAY_CONFIRM_THRESHOLD', async () => {
vi.stubEnv('VITE_REPAY_CONFIRM_THRESHOLD', '1000');
vi.resetModules();
const { REPAY_CONFIRM_THRESHOLD: threshold } = await import('./amountValidation');
expect(threshold).toBe(1000);
vi.unstubAllEnvs();
vi.resetModules();
});
it('falls back to 5000 when VITE_REPAY_CONFIRM_THRESHOLD is not a valid number', async () => {
vi.stubEnv('VITE_REPAY_CONFIRM_THRESHOLD', 'not-a-number');
vi.resetModules();
const { REPAY_CONFIRM_THRESHOLD: threshold } = await import('./amountValidation');
expect(threshold).toBe(5000);
vi.unstubAllEnvs();
vi.resetModules();
});
it('accepts 0 to disable the guard', async () => {
vi.stubEnv('VITE_REPAY_CONFIRM_THRESHOLD', '0');
vi.resetModules();
const { REPAY_CONFIRM_THRESHOLD: threshold, requiresRepayConfirmation: requires } =
await import('./amountValidation');
expect(threshold).toBe(0);
// When threshold is 0 the guard is disabled; no amount triggers confirmation.
expect(requires(10000)).toBe(false);
vi.unstubAllEnvs();
vi.resetModules();
});
});
describe('requiresRepayConfirmation', () => {
it('returns false for amounts below the threshold', () => {
expect(requiresRepayConfirmation(4999)).toBe(false);
expect(requiresRepayConfirmation(0)).toBe(false);
expect(requiresRepayConfirmation(1)).toBe(false);
});
it('returns true for amounts at the threshold', () => {
expect(requiresRepayConfirmation(5000)).toBe(true);
});
it('returns true for amounts above the threshold', () => {
expect(requiresRepayConfirmation(5001)).toBe(true);
expect(requiresRepayConfirmation(10000)).toBe(true);
});
it('returns true for non-integer amounts at or above the threshold', () => {
expect(requiresRepayConfirmation(5000.01)).toBe(true);
expect(requiresRepayConfirmation(5250.50)).toBe(true);
});
});