-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompoundInterestCalculator.test.ts
135 lines (113 loc) · 4 KB
/
compoundInterestCalculator.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
const { fireEvent } = require("@testing-library/dom");
import fs from "fs";
import path from "path";
document.body.innerHTML = fs.readFileSync(
path.resolve(__dirname, "./src/index.html"), "utf8");
// This import must be after the inner html has been configured so it can fetch values and inputs.
import "./src/main";
let principal: HTMLInputElement;
let rate: HTMLInputElement;
let compounding: HTMLInputElement;
let time: HTMLInputElement;
let finalAmount: HTMLInputElement;
let variance: HTMLInputElement;
let varianceAboveAmount: HTMLInputElement;
let varianceBelowAmount: HTMLInputElement;
let calculateButt: HTMLInputElement;
beforeEach(() => {
principal = document.getElementById("principal") as HTMLInputElement;
rate = document.getElementById("rate") as HTMLInputElement;
compounding = document.getElementById("compounding") as HTMLInputElement;
time = document.getElementById("time") as HTMLInputElement;
finalAmount = document.getElementById("finalAmount") as HTMLInputElement;
variance = document.getElementById("variance") as HTMLInputElement;
varianceAboveAmount = document.getElementById(
"finalAmountVarianceAbove"
) as HTMLInputElement;
varianceBelowAmount = document.getElementById(
"finalAmountVarianceBelow"
) as HTMLInputElement;
calculateButt = document.getElementById("calculate") as HTMLInputElement;
// Reset input values
principal.value = "";
rate.value = "";
compounding.value = "";
time.value = "";
variance.value = "";
varianceAboveAmount.value = "";
varianceBelowAmount.value = "";
finalAmount.value = "";
});
describe("Compound Interest Calculator", () => {
describe("Displays Final amount when all values provided", () => {
test("Correctly calculates final amount", () => {
principal.value = "50000";
rate.value = "5";
time.value = "3";
compounding.value = "4";
const calculatedAmount: string = "58037.726";
fireEvent.click(calculateButt);
expect(finalAmount.value).toBe(calculatedAmount);
});
});
describe("Displays above variance", () => {
test("Displays Final amount with above variance", () => {
principal.value = "50000";
rate.value = "10";
time.value = "3";
compounding.value = "1";
variance.value = "3";
const calculatedvarianceAboveAmount: string = "72144.85";
fireEvent.click(calculateButt);
expect(varianceAboveAmount.value).toBe(calculatedvarianceAboveAmount);
});
});
describe("Displays below variance", () => {
test("Displays Final amount with below variance", () => {
principal.value = "50000";
rate.value = "10";
time.value = "3";
compounding.value = "1";
variance.value = "3";
const calculatedvarianceBelowAmount: string = "61252.15";
fireEvent.click(calculateButt);
expect(varianceBelowAmount.value).toBe(calculatedvarianceBelowAmount);
});
});
describe("Edge cases", () => {
describe("Use 'E' for a power (for example 1E3=1^3)", () => {
test("Principal", () => {
principal.value = "1E3";
rate.value = "5";
time.value = "3";
compounding.value = "4";
fireEvent.click(calculateButt);
expect(finalAmount.value).toBe("1160.755");
});
test("Rate", () => {
principal.value = "50000";
rate.value = "1E2"; // Represents 100
time.value = "3";
compounding.value = "4";
fireEvent.click(calculateButt);
expect(finalAmount.value).toBe("727595.761");
});
test("Time", () => {
principal.value = "50000";
rate.value = "5";
time.value = "1E1";
compounding.value = "4";
fireEvent.click(calculateButt);
expect(finalAmount.value).toBe("82180.973");
});
test("All inputs in powers", () => {
principal.value = "1E3";
rate.value = "1E2";
time.value = "1E1";
compounding.value = "1";
fireEvent.click(calculateButt);
expect(finalAmount.value).toBe("1024000");
});
});
});
});