Files
turnosxpress/server/src/api/MercadoPago/__tests__/MercadoPagoWebhook.Service.test.ts
T

75 lines
2.8 KiB
TypeScript

import { MercadoPagoWebhookService } from "../MercadoPagoWebhook.Service";
import PlanPaymentsList from "../../../Models/PlanPayments/PlanPayments";
jest.mock("../../../Models/PlanPayments/PlanPayments", () => ({
__esModule: true,
default: {
createIfMissingByTransactionId: jest.fn(),
},
}));
jest.mock("../../../Models/PlanSubscriptions/PlanSubscriptons", () => ({
__esModule: true,
default: {},
}));
describe("MercadoPagoWebhookService", () => {
beforeEach(() => {
jest.clearAllMocks();
});
it("should clear pending upgrade metadata without changing the current plan or dates when payment is rejected", async () => {
const startDate = new Date("2026-07-01T00:00:00.000Z");
const endDate = new Date("2026-08-01T00:00:00.000Z");
const subscription = {
_id: "subscription-id",
planId: "current-plan-id",
startDate,
endDate,
pendingPaymentInitPoint: "https://checkout.example",
pendingPaymentPreferenceId: "preference-id",
pendingPaymentBillingMonths: undefined,
pendingPaymentType: "upgrade",
pendingPaymentPlanId: "requested-plan-id",
pendingPaymentCurrentPlanPrice: 1000,
pendingPaymentRequestedPlanPrice: 2000,
pendingPaymentProratedAmount: 500,
pendingPaymentRemainingDays: 15,
pendingPaymentPeriodEndDate: endDate,
save: jest.fn().mockResolvedValue(undefined),
};
const service = new MercadoPagoWebhookService() as any;
await service.applyRejectedPayment(
subscription,
{
id: "payment-id",
status: "rejected",
transaction_amount: 500,
payment_method_id: "visa",
},
"user-id",
"preference-id"
);
expect(subscription.planId).toBe("current-plan-id");
expect(subscription.startDate).toBe(startDate);
expect(subscription.endDate).toBe(endDate);
expect(subscription.pendingPaymentInitPoint).toBe("");
expect(subscription.pendingPaymentPreferenceId).toBe("");
expect(subscription.pendingPaymentType).toBeUndefined();
expect(subscription.pendingPaymentPlanId).toBeUndefined();
expect(subscription.pendingPaymentProratedAmount).toBeUndefined();
expect(subscription.save).toHaveBeenCalledTimes(1);
expect(PlanPaymentsList.createIfMissingByTransactionId).toHaveBeenCalledWith({
userId: "user-id",
subscriptionId: "subscription-id",
amount: 500,
paymentDate: expect.any(Date),
paymentMethod: "visa",
status: "failed",
transactionId: "payment-id",
});
});
});