asaas
Version:
Unofficial Asaas Payment Gateway SDK
117 lines (116 loc) • 5.11 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// Preferência no import do index pra garantir que tudo relevante está sendo exportado
const index_1 = require("../index");
const axios_1 = __importDefault(require("axios"));
const webhook_payment_json_1 = __importDefault(require("./fixtures/webhook_payment.json"));
const invalid_webhook_json_1 = __importDefault(require("./fixtures/invalid_webhook.json"));
jest.mock('axios');
// Implementação real para o mock do console.error
jest.spyOn(console, 'error').mockImplementation(() => undefined);
describe('AsaasClient', () => {
beforeEach(() => {
jest.clearAllMocks();
});
test('constructor should initialize correctly in Sandbox mode', () => {
const apiKey = 'my-api-key';
const options = { sandbox: true };
const client = new index_1.AsaasClient(apiKey, options);
expect(client.customers).toBeDefined();
expect(client.payments).toBeDefined();
expect(client.installments).toBeDefined();
expect(client.subscriptions).toBeDefined();
expect(client.webhooks).toBeDefined();
expect(client.pixTransactions).toBeDefined();
expect(client.pixQrCodes).toBeDefined();
expect(client.invoices).toBeDefined();
expect(client.bill).toBeDefined();
expect(client.transfers).toBeDefined();
expect(client.myAccount).toBeDefined();
expect(client.accounts).toBeDefined();
});
test('should initialize printError as true by default', async () => {
// Mock axios to throw an error
const mockError = new Error('API Error');
axios_1.default.create.mockReturnValue({
post: jest.fn().mockRejectedValue(mockError),
get: jest.fn().mockRejectedValue(mockError),
delete: jest.fn().mockRejectedValue(mockError),
});
const apiKey = 'my-api-key';
const client = new index_1.AsaasClient(apiKey);
// We need to test the real implementation, not a mock
try {
await client.payments.new({
customer: '123',
billingType: 'BOLETO',
dueDate: new Date(),
value: 100,
});
// If we reach here, the test should fail
fail('Expected an error to be thrown');
}
catch (error) {
// Verifying that console.error was called
expect(console.error).toHaveBeenCalled();
expect(error).toBe(mockError);
}
});
test('should disable error logs when printError is false', async () => {
// Mock axios to throw an error
const mockError = new Error('API Error');
axios_1.default.create.mockReturnValue({
post: jest.fn().mockRejectedValue(mockError),
get: jest.fn().mockRejectedValue(mockError),
delete: jest.fn().mockRejectedValue(mockError),
});
const apiKey = 'my-api-key';
const options = { printError: false };
const client = new index_1.AsaasClient(apiKey, options);
// We need to test the real implementation, not a mock
try {
await client.payments.new({
customer: '123',
billingType: 'BOLETO',
dueDate: new Date(),
value: 100,
});
// If we reach here, the test should fail
fail('Expected an error to be thrown');
}
catch (error) {
// Verifying that console.error was not called
expect(console.error).not.toHaveBeenCalled();
expect(error).toBe(mockError);
}
});
test('should pass printError value to all API instances', () => {
const apiKey = 'my-api-key';
const options = { printError: false };
const client = new index_1.AsaasClient(apiKey, options);
// Acessando propriedades para teste
const payments = client.payments;
const customers = client.customers;
const subscriptions = client.subscriptions;
expect(payments.printError).toBe(false);
expect(customers.printError).toBe(false);
expect(subscriptions.printError).toBe(false);
});
});
describe('Webhooks', () => {
test('should parse payment webhook correctly', () => {
const parsed = (0, index_1.parseWebhookPayload)({ ...webhook_payment_json_1.default });
expect(parsed).not.toBeNull();
// typescript infer on payment attribute
expect(parsed && 'payment' in parsed ? parsed.payment.id : null).not.toBeNull();
// typescript infer on invoice attribute
expect(parsed && 'invoice' in parsed ? parsed.invoice.id : null).toBeNull();
});
test('parse of unmapped webhook should return null', () => {
const parsed = (0, index_1.parseWebhookPayload)({ ...invalid_webhook_json_1.default });
expect(parsed).toBeNull();
});
});