Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/util/purchases/charge-amount.test.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45// System under test6//7import getChargeAmount from "./charge-amount";8import { round2up } from "@cocalc/util/misc";910describe("charge-amount", () => {11describe("#getChargeAmount", () => {12it("charges nothing when sufficient account balance is available", async () => {13// Arrange14//15const testChargeParams = {16balance: 17.99914,17minPayment: 2.5,18minBalance: 0.0,19cost: 17,20};2122// Act23//24const charge = getChargeAmount(testChargeParams);2526// Assert27//28expect(charge.chargeAmount).toEqual(0.0);29expect(charge.amountDue).toEqual(0.0);30expect(charge.cureAmount).toEqual(0.0);31expect(charge.minimumPaymentCharge).toEqual(0.0);32});3334it("charges the difference between cost and account balance when sufficient account balance is not available", async () => {35// Arrange36//37const testChargeParams = {38balance: 17.55,39minPayment: 0.0,40minBalance: 0.0,41cost: 18,42};4344// Act45//46const charge = getChargeAmount(testChargeParams);4748// Assert49//50expect(charge.chargeAmount).toEqual(0.45);51expect(charge.amountDue).toEqual(0.45);52expect(charge.cureAmount).toEqual(0.0);53expect(charge.minimumPaymentCharge).toEqual(0.0);54});5556it("charges exact minimum payment when difference between cost and balance is sufficiently small", async () => {57// Arrange58//59const testChargeParams = {60balance: 17.55,61minPayment: 2.54321,62minBalance: 0,63cost: 18,64};6566// Act67//68const charge = getChargeAmount(testChargeParams);6970// Assert71//72expect(charge.chargeAmount).toEqual(2.54321);73expect(charge.amountDue).toEqual(0.45);74expect(charge.cureAmount).toEqual(0.0);75expect(charge.minimumPaymentCharge.toFixed(10)).toEqual(76(2.54321 - 0.45).toFixed(10),77);78});7980it("uses credit when available", async () => {81// Arrange82//83const testChargeParams = {84balance: 1.543,85minPayment: 5.0,86minBalance: -5.0,87cost: 10.0,88};8990// Act91//92const charge = getChargeAmount(testChargeParams);9394// Assert95//96expect(charge.chargeAmount).toEqual(5.0);97expect(charge.amountDue).toEqual(3.46);98expect(charge.cureAmount).toEqual(0.0);99expect(charge.minimumPaymentCharge.toFixed(3)).toEqual(100(1.543).toFixed(3),101);102});103104it("rounds amount due to two decimal places", async () => {105// Arrange106const testChargeParams = {107balance: 17.5678,108minPayment: 0,109minBalance: 0,110cost: 18,111};112113// Act114const charge = getChargeAmount(testChargeParams);115116// Assert117expect(charge.chargeAmount).toEqual(118round2up(testChargeParams.cost - testChargeParams.balance),119);120expect(charge.amountDue).toEqual(121round2up(testChargeParams.cost - testChargeParams.balance),122);123expect(charge.cureAmount).toEqual(0.0);124expect(charge.minimumPaymentCharge).toEqual(0.0);125});126127it("adds charge when user balance is less than minimum balance", async () => {128// Arrange129const testChargeParams = {130balance: -0.023,131minPayment: 0.0,132minBalance: 0.0,133cost: 0.0,134};135136// Act137const charge = getChargeAmount(testChargeParams);138139// Assert140expect(charge.chargeAmount).toEqual(141round2up(testChargeParams.cost - testChargeParams.balance),142);143expect(charge.amountDue).toEqual(144round2up(testChargeParams.cost - testChargeParams.balance),145);146expect(charge.cureAmount).toEqual(0.023);147expect(charge.minimumPaymentCharge).toEqual(0.0);148});149150it("enforces a non-negative due amount", async () => {151// Arrange152//153const testChargeParams = {154balance: 18,155minPayment: 0.0,156minBalance: 0.0,157cost: 16,158};159160// Act161//162const charge = getChargeAmount(testChargeParams);163164// Assert165//166expect(charge.chargeAmount).toEqual(0.0);167expect(charge.amountDue).toEqual(0.0);168expect(charge.cureAmount).toEqual(0.0);169expect(charge.minimumPaymentCharge).toEqual(0.0);170});171});172});173174175