Files
turnosxpress/server/src/Models/PlanSubscriptions/__tests__/PlanSubscriptons.test.ts
T

121 lines
4.2 KiB
TypeScript

import dayjs from "dayjs";
import { calculateProratedUpgradeAmount } from "../PlanSubscriptions.pricing";
import PlanSubscriptionsList from "../PlanSubscriptons";
import { MP_SUBS_STATUS } from "../PlanSubscriptions.Adapter.Mongoose";
import PlansList from "../../Plans/Plans";
jest.mock("../../../index", () => ({
io: {
to: jest.fn(() => ({ emit: jest.fn() })),
},
}));
describe("PlanSubscriptions Logic", () => {
it("should calculate correct endDate for 1, 3, 6, 12 months", () => {
const start = dayjs("2024-01-01T10:00:00.000Z");
const end1 = start.clone().add(1, "months");
expect(end1.format("YYYY-MM-DD")).toBe("2024-02-01");
const end3 = start.clone().add(3, "months");
expect(end3.format("YYYY-MM-DD")).toBe("2024-04-01");
const end6 = start.clone().add(6, "months");
expect(end6.format("YYYY-MM-DD")).toBe("2024-07-01");
const end12 = start.clone().add(12, "months");
expect(end12.format("YYYY-MM-DD")).toBe("2025-01-01");
});
it("should calculate total price correctly with discounts", () => {
const planPrice = 1000;
// 1 month, 0 discount
const total1 = planPrice * 1;
const final1 = total1 - (total1 * (0 / 100));
expect(final1).toBe(1000);
// 3 months, 10% discount
const total3 = planPrice * 3; // 3000
const final3 = total3 - (total3 * (10 / 100)); // 3000 - 300 = 2700
expect(final3).toBe(2700);
// 6 months, 20% discount
const total6 = planPrice * 6; // 6000
const final6 = total6 - (total6 * (20 / 100)); // 6000 - 1200 = 4800
expect(final6).toBe(4800);
// 12 months, 30% discount
const total12 = planPrice * 12; // 12000
const final12 = total12 - (total12 * (30 / 100)); // 12000 - 3600 = 8400
expect(final12).toBe(8400);
});
it("should calculate only the prorated difference for paid plan upgrades", () => {
const currentPlanPrice = 1000;
const requestedPlanPrice = 2500;
const remainingDays = 15;
const proratedAmount = calculateProratedUpgradeAmount(
currentPlanPrice,
requestedPlanPrice,
remainingDays
);
expect(proratedAmount).toBe(750);
});
it("should keep pending paid subscription plan instead of masking it as free", async () => {
const paidPlan = {
id: "paid-plan-id",
name: "Intermedio",
description: "",
features: [],
code: "intermediate",
price: 1000,
annualPrice: 0,
limitOrganizations: 1,
limitEmployees: 1,
limitServices: 1,
limitAppointments: 1,
limitClients: 1,
limitRepeats: 1,
mailNotifications: false,
smsNotifications: false,
wapNotifications: false,
bot: false,
active: true,
dateLimit: false,
payments: false,
};
const subscription = {
id: "subscription-id",
planId: paidPlan.id,
startDate: new Date(),
endDate: new Date(),
isActive: false,
autoRenew: false,
mpStatus: MP_SUBS_STATUS.PENDING,
mpDateCreated: new Date(),
};
const originalPlanSuscriptions = PlanSubscriptionsList.planSuscriptions;
const findOneSpy = jest.spyOn(PlansList, "findOne").mockResolvedValue(paidPlan);
PlanSubscriptionsList.planSuscriptions = {
findOne: jest.fn().mockResolvedValue(subscription),
} as any;
try {
const result = await PlanSubscriptionsList.getSubscriptionByUser({ sessionUser: "user-id" });
expect(result?.plan).toEqual(paidPlan);
expect(result?.mpStatus).toBe(MP_SUBS_STATUS.PENDING);
expect(findOneSpy).toHaveBeenCalledTimes(1);
expect(findOneSpy).toHaveBeenCalledWith({ _id: subscription.planId });
} finally {
PlanSubscriptionsList.planSuscriptions = originalPlanSuscriptions;
findOneSpy.mockRestore();
}
});
});