CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/util/purchases/charge-amount.test.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// System under test
7
//
8
import getChargeAmount from "./charge-amount";
9
import { round2up } from "@cocalc/util/misc";
10
11
describe("charge-amount", () => {
12
describe("#getChargeAmount", () => {
13
it("charges nothing when sufficient account balance is available", async () => {
14
// Arrange
15
//
16
const testChargeParams = {
17
balance: 17.99914,
18
minPayment: 2.5,
19
minBalance: 0.0,
20
cost: 17,
21
};
22
23
// Act
24
//
25
const charge = getChargeAmount(testChargeParams);
26
27
// Assert
28
//
29
expect(charge.chargeAmount).toEqual(0.0);
30
expect(charge.amountDue).toEqual(0.0);
31
expect(charge.cureAmount).toEqual(0.0);
32
expect(charge.minimumPaymentCharge).toEqual(0.0);
33
});
34
35
it("charges the difference between cost and account balance when sufficient account balance is not available", async () => {
36
// Arrange
37
//
38
const testChargeParams = {
39
balance: 17.55,
40
minPayment: 0.0,
41
minBalance: 0.0,
42
cost: 18,
43
};
44
45
// Act
46
//
47
const charge = getChargeAmount(testChargeParams);
48
49
// Assert
50
//
51
expect(charge.chargeAmount).toEqual(0.45);
52
expect(charge.amountDue).toEqual(0.45);
53
expect(charge.cureAmount).toEqual(0.0);
54
expect(charge.minimumPaymentCharge).toEqual(0.0);
55
});
56
57
it("charges exact minimum payment when difference between cost and balance is sufficiently small", async () => {
58
// Arrange
59
//
60
const testChargeParams = {
61
balance: 17.55,
62
minPayment: 2.54321,
63
minBalance: 0,
64
cost: 18,
65
};
66
67
// Act
68
//
69
const charge = getChargeAmount(testChargeParams);
70
71
// Assert
72
//
73
expect(charge.chargeAmount).toEqual(2.54321);
74
expect(charge.amountDue).toEqual(0.45);
75
expect(charge.cureAmount).toEqual(0.0);
76
expect(charge.minimumPaymentCharge.toFixed(10)).toEqual(
77
(2.54321 - 0.45).toFixed(10),
78
);
79
});
80
81
it("uses credit when available", async () => {
82
// Arrange
83
//
84
const testChargeParams = {
85
balance: 1.543,
86
minPayment: 5.0,
87
minBalance: -5.0,
88
cost: 10.0,
89
};
90
91
// Act
92
//
93
const charge = getChargeAmount(testChargeParams);
94
95
// Assert
96
//
97
expect(charge.chargeAmount).toEqual(5.0);
98
expect(charge.amountDue).toEqual(3.46);
99
expect(charge.cureAmount).toEqual(0.0);
100
expect(charge.minimumPaymentCharge.toFixed(3)).toEqual(
101
(1.543).toFixed(3),
102
);
103
});
104
105
it("rounds amount due to two decimal places", async () => {
106
// Arrange
107
const testChargeParams = {
108
balance: 17.5678,
109
minPayment: 0,
110
minBalance: 0,
111
cost: 18,
112
};
113
114
// Act
115
const charge = getChargeAmount(testChargeParams);
116
117
// Assert
118
expect(charge.chargeAmount).toEqual(
119
round2up(testChargeParams.cost - testChargeParams.balance),
120
);
121
expect(charge.amountDue).toEqual(
122
round2up(testChargeParams.cost - testChargeParams.balance),
123
);
124
expect(charge.cureAmount).toEqual(0.0);
125
expect(charge.minimumPaymentCharge).toEqual(0.0);
126
});
127
128
it("adds charge when user balance is less than minimum balance", async () => {
129
// Arrange
130
const testChargeParams = {
131
balance: -0.023,
132
minPayment: 0.0,
133
minBalance: 0.0,
134
cost: 0.0,
135
};
136
137
// Act
138
const charge = getChargeAmount(testChargeParams);
139
140
// Assert
141
expect(charge.chargeAmount).toEqual(
142
round2up(testChargeParams.cost - testChargeParams.balance),
143
);
144
expect(charge.amountDue).toEqual(
145
round2up(testChargeParams.cost - testChargeParams.balance),
146
);
147
expect(charge.cureAmount).toEqual(0.023);
148
expect(charge.minimumPaymentCharge).toEqual(0.0);
149
});
150
151
it("enforces a non-negative due amount", async () => {
152
// Arrange
153
//
154
const testChargeParams = {
155
balance: 18,
156
minPayment: 0.0,
157
minBalance: 0.0,
158
cost: 16,
159
};
160
161
// Act
162
//
163
const charge = getChargeAmount(testChargeParams);
164
165
// Assert
166
//
167
expect(charge.chargeAmount).toEqual(0.0);
168
expect(charge.amountDue).toEqual(0.0);
169
expect(charge.cureAmount).toEqual(0.0);
170
expect(charge.minimumPaymentCharge).toEqual(0.0);
171
});
172
});
173
});
174
175