first commit
This commit is contained in:
45
tests/unit/credit-card.service.test.js
Normal file
45
tests/unit/credit-card.service.test.js
Normal file
@@ -0,0 +1,45 @@
|
||||
const mockCharge = jest.fn();
|
||||
jest.mock('../../app/core/midtrans.service', () => ({
|
||||
getCoreApi: jest.fn().mockReturnValue({
|
||||
charge: mockCharge,
|
||||
transaction: { status: jest.fn(), refund: jest.fn(), cancel: jest.fn() }
|
||||
}),
|
||||
getSnap: jest.fn().mockReturnValue({ createTransaction: jest.fn() })
|
||||
}));
|
||||
|
||||
const mockOrderFindByPk = jest.fn();
|
||||
jest.mock('../../models/migration', () => ({
|
||||
Order: { findByPk: mockOrderFindByPk, findOne: jest.fn(), update: jest.fn() },
|
||||
Payment: { findOne: jest.fn(), create: jest.fn(), update: jest.fn() }
|
||||
}));
|
||||
|
||||
const creditCardService = require('../../app/modules/credit-card/services/credit-card.service');
|
||||
|
||||
describe('CreditCardService', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should create a credit card transaction successfully', async () => {
|
||||
const payload = { order_id: 'u-cc', external_id: 'INV-CC-001', amount: 500000, credit_card_token: 't-123' };
|
||||
mockCharge.mockResolvedValue({ status_code: '201', transaction_status: 'capture' });
|
||||
const result = await creditCardService.createTransaction(payload);
|
||||
expect(mockCharge).toHaveBeenCalled();
|
||||
expect(result.status_code).toBe('201');
|
||||
});
|
||||
|
||||
it('should fallback to order total if amount is missing', async () => {
|
||||
const payload = { order_id: 'u-cc', external_id: 'INV-CC-001', credit_card_token: 't-123' };
|
||||
mockOrderFindByPk.mockResolvedValue({ total: 500000 });
|
||||
mockCharge.mockResolvedValue({ status_code: '201', transaction_status: 'capture' });
|
||||
|
||||
await creditCardService.createTransaction(payload);
|
||||
|
||||
expect(mockOrderFindByPk).toHaveBeenCalledWith('u-cc');
|
||||
expect(mockCharge).toHaveBeenCalledWith(expect.objectContaining({
|
||||
transaction_details: expect.objectContaining({
|
||||
gross_amount: 500000
|
||||
})
|
||||
}));
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user