first commit
This commit is contained in:
63
tests/unit/payment_logger.helper.test.js
Normal file
63
tests/unit/payment_logger.helper.test.js
Normal file
@@ -0,0 +1,63 @@
|
||||
const mockOrderFindByPk = jest.fn();
|
||||
const mockOrderFindOne = jest.fn();
|
||||
const mockPaymentCreate = jest.fn();
|
||||
|
||||
jest.mock('../../models/migration', () => ({
|
||||
Order: { findByPk: mockOrderFindByPk, findOne: mockOrderFindOne },
|
||||
Payment: { create: mockPaymentCreate }
|
||||
}));
|
||||
|
||||
const paymentLogger = require('../../app/helpers/payment_logger.helper');
|
||||
|
||||
describe('PaymentLoggerHelper', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should log a payment successfully', async () => {
|
||||
const midtransResponse = {
|
||||
order_id: 'INV-EXT-001',
|
||||
gross_amount: '10000.00',
|
||||
transaction_status: 'settlement',
|
||||
payment_type: 'credit_card',
|
||||
status_message: 'Success'
|
||||
};
|
||||
const requestPayload = { order_id: 'u-123' };
|
||||
|
||||
mockOrderFindByPk.mockResolvedValue({ id: 'u-123', total: 10000, user_id: 1 });
|
||||
mockPaymentCreate.mockResolvedValue({ id: 'p-123' });
|
||||
|
||||
const result = await paymentLogger.log(midtransResponse, requestPayload);
|
||||
|
||||
expect(mockOrderFindByPk).toHaveBeenCalledWith('u-123');
|
||||
expect(mockPaymentCreate).toHaveBeenCalledWith(expect.objectContaining({
|
||||
status: 'SUCCESS',
|
||||
channel_code: 'credit_card',
|
||||
channel_type: 'CREDIT_CARD'
|
||||
}));
|
||||
expect(result.id).toBe('p-123');
|
||||
});
|
||||
|
||||
it('should map bank transfer types correctly', async () => {
|
||||
const midtransResponse = { order_id: 'INV-EXT', payment_type: 'bank_transfer', transaction_status: 'pending' };
|
||||
mockOrderFindOne.mockResolvedValue({ id: 'u-123', total: 10000 });
|
||||
|
||||
await paymentLogger.log(midtransResponse);
|
||||
|
||||
expect(mockPaymentCreate).toHaveBeenCalledWith(expect.objectContaining({
|
||||
channel_type: 'BANK'
|
||||
}));
|
||||
});
|
||||
|
||||
it('should return null if order not found', async () => {
|
||||
mockOrderFindByPk.mockResolvedValue(null);
|
||||
mockOrderFindOne.mockResolvedValue(null);
|
||||
|
||||
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation();
|
||||
const result = await paymentLogger.log({ order_id: 'none' });
|
||||
|
||||
expect(result).toBeNull();
|
||||
expect(consoleSpy).toHaveBeenCalled();
|
||||
consoleSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user