-
Notifications
You must be signed in to change notification settings - Fork 1
/
Amount.unit.ts
108 lines (79 loc) · 2.65 KB
/
Amount.unit.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
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
104
105
106
107
108
import { Amount } from './Amount';
import { AmountErrors } from './AmountErrors';
test('Creation', () => {
const validData = {
value: 100,
};
const result = Amount.create(validData);
expect(result.isSuccess).toBe(true);
const amount = result.value;
expect(amount.value).toBe(100);
});
test('round down', () => {
const result = Amount.create({ value: 0.01499 });
expect(result.isSuccess).toBe(true);
const amount = result.value;
expect(amount.value).toBe(0.01);
});
test('round up 0.015', () => {
const result = Amount.create({ value: 0.015 });
expect(result.isSuccess).toBe(true);
const amount = result.value;
expect(amount.value).toBe(0.02);
});
test('round up 10.075', () => {
const result = Amount.create({ value: 10.075 });
expect(result.isSuccess).toBe(true);
const amount = result.value;
expect(amount.value).toBe(10.08);
});
test('round up -10.995', () => {
const result = Amount.create({ value: -10.995 });
expect(result.isSuccess).toBe(true);
const amount = result.value;
expect(amount.value).toBe(-11);
});
test('Creation fails with a value greater than max', () => {
const data = {
value: Amount.MAX_ABS + 1,
};
const result = Amount.create(data);
expect(result.isFailure).toBe(true);
expect(result.error).toBeInstanceOf(AmountErrors.MaxBreached);
});
test('Creation fails with a value less than min', () => {
const data = {
value: -Amount.MAX_ABS - 1,
};
const result = Amount.create(data);
expect(result.isFailure).toBe(true);
expect(result.error).toBeInstanceOf(AmountErrors.MaxBreached);
});
test('36326231234624.984 rounds to 36326231234624.98', () => {
const value = Amount.create({ value: 36326231234624.984 }).value.value;
expect(value.toString()).toBe('36326231234624.98');
});
test('1.93 + 10.21 = 12.14', () => {
const v1 = Amount.create({ value: 1.93 }).value;
const v2 = Amount.create({ value: 10.21 }).value;
const r = v1.add(v2).value.value;
expect(r).toBe(12.14);
});
test('-3.77 - 10.98 = -14.75', () => {
const v1 = Amount.create({ value: -3.77 }).value;
const v2 = Amount.create({ value: 10.98 }).value;
const r = v1.subtract(v2).value.value;
expect(r).toBe(-14.75);
});
test('0 - -10.98 = 10.98', () => {
const v1 = Amount.create({ value: 0 }).value;
const v2 = Amount.create({ value: -10.98 }).value;
const r = v1.subtract(v2).value.value;
expect(r).toBe(10.98);
});
test('82102892877577.4 - 42297692960849.49 = 39805199916727.91', () => {
const v1 = Amount.create({ value: 82102892877577.4 }).value;
const v2 = Amount.create({ value: 42297692960849.49 }).value;
const r = v1.subtract(v2).value.value;
expect(r).toBe(39805199916727.91);
});