@wernerthiago/teo
Version:
Test Execution Optimizer
97 lines (78 loc) • 2.87 kB
JavaScript
// Tests for payment service
import { jest } from '@jest/globals'
import PaymentService from '../../src/payment/payment-service.js'
describe('PaymentService', () => {
let paymentService
beforeEach(() => {
paymentService = new PaymentService()
})
describe('addPaymentMethod', () => {
test('should add payment method successfully', async () => {
const result = await paymentService.addPaymentMethod(
'user123',
'4111111111111111',
'12/25',
'123'
)
expect(result).toHaveProperty('id')
expect(result).toHaveProperty('userId', 'user123')
expect(result.cardNumber).toBe('************1111')
})
})
describe('processPayment', () => {
let paymentMethodId
beforeEach(async () => {
const paymentMethod = await paymentService.addPaymentMethod(
'user123',
'4111111111111111',
'12/25',
'123'
)
paymentMethodId = paymentMethod.id
})
test('should process payment successfully', async () => {
const result = await paymentService.processPayment(100, paymentMethodId)
expect(result).toHaveProperty('id')
expect(result).toHaveProperty('amount', 100)
expect(result).toHaveProperty('status')
expect(['completed', 'failed']).toContain(result.status)
})
test('should throw error for invalid payment method', async () => {
await expect(
paymentService.processPayment(100, 'invalid-method-id')
).rejects.toThrow('Invalid payment method')
})
test('should throw error for invalid amount', async () => {
await expect(
paymentService.processPayment(-10, paymentMethodId)
).rejects.toThrow('Invalid amount')
})
})
describe('refundPayment', () => {
let transactionId
beforeEach(async () => {
const paymentMethod = await paymentService.addPaymentMethod(
'user123',
'4111111111111111',
'12/25',
'123'
)
// Mock successful payment
jest.spyOn(paymentService, 'simulatePaymentProcessing').mockResolvedValue(true)
const transaction = await paymentService.processPayment(100, paymentMethod.id)
transactionId = transaction.id
})
test('should refund payment successfully', async () => {
const result = await paymentService.refundPayment(transactionId, 50)
expect(result).toHaveProperty('id')
expect(result).toHaveProperty('amount', 50)
expect(result).toHaveProperty('originalTransactionId', transactionId)
expect(result).toHaveProperty('status', 'completed')
})
test('should throw error for non-existent transaction', async () => {
await expect(
paymentService.refundPayment('invalid-transaction-id', 50)
).rejects.toThrow('Transaction not found')
})
})
})