Testing Guidance
Use sandbox environment for all testing. The SDK provides methods for mocking and testing webhook payloads.
Environment Setup
Codeconst saligpay = new SaligPay({ clientId: process.env.SALIGPAY_TEST_CLIENT_ID, clientSecret: process.env.SALIGPAY_TEST_CLIENT_SECRET, env: "sandbox", });
Example Test Suite
Codeimport { SaligPay } from "saligpay-node"; describe("SaligPay SDK", () => { let saligPay: SaligPay; beforeAll(() => { saligPay = new SaligPay({ clientId: process.env.TEST_CLIENT_ID, clientSecret: process.env.TEST_CLIENT_SECRET, env: "sandbox", }); }); it("should authenticate successfully", async () => { const tokens = await saligPay.authenticate(); expect(tokens).toBeDefined(); expect(tokens.accessToken).toBeDefined(); expect(tokens.refreshToken).toBeDefined(); expect(tokens.expiresAt).toBeInstanceOf(Date); }); it("should create a checkout session", async () => { await saligPay.authenticate(); const checkout = await saligPay.checkout.create({ externalId: "test-order", amount: 10000, description: "Test payment", webhookUrl: "https://example.com/webhook", returnUrl: "https://example.com/success", contact: { name: "Test User", email: "test@example.com", }, }); expect(checkout).toBeDefined(); expect(checkout.id).toBeDefined(); expect(checkout.checkoutUrl).toBeDefined(); }); it("should handle webhook payloads", () => { const payload = { externalId: "test-order", amount: 10000, status: "COMPLETED", paymentMethod: { id: "pm-test", type: "gcash", }, }; const event = saligPay.webhooks.constructEvent(payload); expect(event.externalId).toBe("test-order"); expect(event.status).toBe("COMPLETED"); }); });
Mock and Stub Examples
Mocking the SDK
Codeimport { SaligPay } from "saligpay-node"; // Mock the entire SDK jest.mock("saligpay-node", () => ({ SaligPay: jest.fn().mockImplementation(() => ({ authenticate: jest.fn().mockResolvedValue({ accessToken: "mock-access-token", refreshToken: "mock-refresh-token", expiresAt: new Date(Date.now() + 3600000), }), checkout: { create: jest.fn().mockResolvedValue({ id: "checkout-mock-123", checkoutUrl: "https://checkout.saligpay.com/mock", }), }, webhooks: { constructEvent: jest.fn((payload) => payload), }, })), })); describe("Mocked SaligPay", () => { it("should use mocked checkout", async () => { const saligpay = new SaligPay({ clientId: "test", clientSecret: "test", env: "sandbox", }); const result = await saligpay.checkout.create({ externalId: "test", amount: 10000, }); expect(result.id).toBe("checkout-mock-123"); }); });
Stubbing Webhook Payloads
Codeconst createMockWebhookPayload = (status: string = "completed") => ({ id: `wh-test-${Date.now()}`, externalId: "test-order-123", amount: 10000, status: status, paymentMethod: { id: "pm-test", type: "gcash", }, contact: { name: "Test User", email: "test@example.com", }, metadata: { test: true }, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(), }); describe("Webhook handling", () => { it("should handle completed payment", () => { const payload = createMockWebhookPayload("completed"); const event = saligpay.webhooks.constructEvent(payload); expect(event.status).toBe("completed"); }); it("should handle failed payment", () => { const payload = createMockWebhookPayload("failed"); const event = saligpay.webhooks.constructEvent(payload); expect(event.status).toBe("failed"); }); });
Integration Testing
Codeimport { SaligPay } from "saligpay-node"; describe("Integration Tests", () => { let saligpay: SaligPay; beforeAll(async () => { saligpay = new SaligPay({ clientId: process.env.SALIGPAY_TEST_CLIENT_ID!, clientSecret: process.env.SALIGPAY_TEST_CLIENT_SECRET!, env: "sandbox", }); await saligpay.authenticate(); }); it("should complete full payment flow", async () => { // Create checkout const checkout = await saligpay.checkout.create({ externalId: `test-order-${Date.now()}`, amount: 50000, description: "Integration test payment", webhookUrl: "https://example.com/webhook", returnUrl: "https://example.com/success", contact: { name: "Test User", email: "test@example.com", }, }); expect(checkout.checkoutUrl).toBeDefined(); // Simulate webhook const webhookPayload = { id: checkout.id, externalId: checkout.externalId, amount: checkout.amount, status: "completed", paymentMethod: { id: "pm_123", type: "gcash" }, }; const event = saligpay.webhooks.constructEvent(webhookPayload); expect(event.status).toBe("completed"); }); });
Last modified on
