-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculations.test.ts
242 lines (193 loc) · 6.52 KB
/
calculations.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { calculateCompoundInterest } from "./src/services/calculations";
type CalculationDataSet = [
result: number,
compoundsPerYear: number,
monthlyAddition?: number,
];
describe.each<CalculationDataSet>([
[4940.625, 1, 100], // Annually
[4992.335, 2, 100], // SemiAnnually
[5018.863, 4, 100], // Quartarly
[5036.806, 12, 100], // Monthly
])(
"Calculate compound interests",
(result, compoundsPerYear, monthlyAddition) => {
describe("calculate compund interests correctly", () => {
test(`calculates compound interest correctly with monthly addition ${monthlyAddition}, principal 1000, 5% interest rate, 4 times annually for 3 years.`, () => {
const principal = 1000;
const annualRate = 0.05;
const years = 3;
const expectedAmount = result;
const calculatedAmount = calculateCompoundInterest(
principal,
annualRate,
compoundsPerYear,
years,
monthlyAddition
);
expect(calculatedAmount).toBe(expectedAmount);
});
test("calculates compound interest correctly with principal 1500, 10% interest rate, 3 times annually for 5 years.", () => {
const principal = 1500;
const annualRate = 0.1;
const compoundsPerYear = 3;
const years = 5;
const expectedAmount = 2453.002;
const calculatedAmount = calculateCompoundInterest(
principal,
annualRate,
compoundsPerYear,
years
);
expect(calculatedAmount).toBe(expectedAmount);
});
});
describe("calculate negative compound interests correctly", () => {
test("calculates compound interest correctly with principal 1500, -0.02% interest rate, \
3 times annually for 5 years.", () => {
const principal = 1500;
const annualRate = -0.02;
const compoundsPerYear = 3;
const years = 5;
const expectedAmount = 1356.802;
const calculatedAmount = calculateCompoundInterest(
principal,
annualRate,
compoundsPerYear,
years
);
expect(calculatedAmount).toBe(expectedAmount);
});
});
describe("calculate with 0 values", () => {
test("calculates compound interest correctly with principal 0", () => {
const principal = 0;
const annualRate = 0.05;
const compoundsPerYear = 4;
const years = 3;
const expectedAmount = 0;
const calculatedAmount = calculateCompoundInterest(
principal,
annualRate,
compoundsPerYear,
years
);
expect(calculatedAmount).toBe(expectedAmount);
});
test("calculates compound interest correctly with 0% interest rate", () => {
const principal = 1000;
const annualRate = 0;
const compoundsPerYear = 4;
const years = 3;
const expectedAmount = 1000;
const calculatedAmount = calculateCompoundInterest(
principal,
annualRate,
compoundsPerYear,
years
);
expect(calculatedAmount).toBe(expectedAmount);
});
test("calculates compound interest correctly with 0 compounds per year", () => {
const principal = 1000;
const annualRate = 0.05;
const compoundsPerYear = 0;
const years = 3;
const expectedAmount = 1000;
const calculatedAmount = calculateCompoundInterest(
principal,
annualRate,
compoundsPerYear,
years
);
expect(calculatedAmount).toBe(expectedAmount);
});
test("calculates compound interest correctly with 0 years", () => {
const principal = 1000;
const annualRate = 0.05;
const compoundsPerYear = 4;
const years = 0;
const expectedAmount = 1000;
const calculatedAmount = calculateCompoundInterest(
principal,
annualRate,
compoundsPerYear,
years
);
expect(calculatedAmount).toBe(expectedAmount);
});
test("calculates compound interest correctly with all parameters zero", () => {
const principal = 0;
const annualRate = 0;
const compoundsPerYear = 0;
const years = 0;
const expectedAmount = 0;
const calculatedAmount = calculateCompoundInterest(
principal,
annualRate,
compoundsPerYear,
years
);
expect(calculatedAmount).toBe(expectedAmount);
});
});
describe("calculate with bizarre values", () => {
test("calculates compound interest correctly with high frequency compounding and many years", () => {
const principal = 1000;
const annualRate = 0.05;
const compoundsPerYear = 365;
const years = 100;
const expectedAmount = 148362.346;
const calculatedAmount = calculateCompoundInterest(
principal,
annualRate,
compoundsPerYear,
years
);
expect(calculatedAmount).toBe(expectedAmount);
});
test("calculates compound interest correctly with negative principal, expecting it grows negatively", () => {
const principal = -1000;
const annualRate = 0.05;
const compoundsPerYear = 4;
const years = 3;
const expectedAmount = -1160.755;
const calculatedAmount = calculateCompoundInterest(
principal,
annualRate,
compoundsPerYear,
years
);
expect(calculatedAmount).toBe(expectedAmount);
});
test("calculates compound interest correctly with extremely high interest rate", () => {
const principal = 1000;
const annualRate = 3;
const compoundsPerYear = 4;
const years = 3;
const expectedAmount = 825005.007;
const calculatedAmount = calculateCompoundInterest(
principal,
annualRate,
compoundsPerYear,
years
);
expect(calculatedAmount).toBe(expectedAmount);
});
test("calculates compound interest with fractional compounding periods", () => {
const principal = 1000;
const annualRate = 0.05;
const compoundsPerYear = 2.5;
const years = 3;
const expectedAmount = 1160.12;
const calculatedAmount = calculateCompoundInterest(
principal,
annualRate,
compoundsPerYear,
years
);
expect(calculatedAmount).toBeCloseTo(expectedAmount, 2);
});
});
}
);