updated tests

This commit is contained in:
jackiettran
2026-01-15 18:47:43 -05:00
parent 35d5050286
commit 63385e049c
13 changed files with 256 additions and 201 deletions

View File

@@ -18,6 +18,19 @@ jest.mock('sequelize', () => ({
}
}));
const mockLoggerError = jest.fn();
const mockLoggerInfo = jest.fn();
jest.mock('../../../utils/logger', () => ({
error: mockLoggerError,
info: mockLoggerInfo,
warn: jest.fn(),
withRequestId: jest.fn(() => ({
error: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
})),
}));
const PayoutService = require('../../../services/payoutService');
const { Rental, User, Item } = require('../../../models');
const StripeService = require('../../../services/stripeService');
@@ -30,19 +43,15 @@ const mockItemModel = Item;
const mockCreateTransfer = StripeService.createTransfer;
describe('PayoutService', () => {
let consoleSpy, consoleErrorSpy;
let consoleSpy;
beforeEach(() => {
jest.clearAllMocks();
// Set up console spies
consoleSpy = jest.spyOn(console, 'log').mockImplementation();
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
});
afterEach(() => {
consoleSpy.mockRestore();
consoleErrorSpy.mockRestore();
});
describe('getEligiblePayouts', () => {
@@ -87,7 +96,8 @@ describe('PayoutService', () => {
where: {
stripeConnectedAccountId: {
'not': null
}
},
stripePayoutsEnabled: true
}
},
{
@@ -106,7 +116,7 @@ describe('PayoutService', () => {
await expect(PayoutService.getEligiblePayouts()).rejects.toThrow('Database connection failed');
expect(consoleErrorSpy).toHaveBeenCalledWith('Error getting eligible payouts:', dbError);
expect(mockLoggerError).toHaveBeenCalledWith('Error getting eligible payouts', expect.objectContaining({ error: dbError.message }));
});
it('should return empty array when no eligible rentals found', async () => {
@@ -269,9 +279,9 @@ describe('PayoutService', () => {
payoutStatus: 'failed'
});
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error processing payout for rental 1:',
stripeError
expect(mockLoggerError).toHaveBeenCalledWith(
'Error processing payout for rental',
expect.objectContaining({ rentalId: 1 })
);
});
@@ -287,9 +297,9 @@ describe('PayoutService', () => {
await expect(PayoutService.processRentalPayout(mockRental))
.rejects.toThrow('Database update failed');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error processing payout for rental 1:',
dbError
expect(mockLoggerError).toHaveBeenCalledWith(
'Error processing payout for rental',
expect.objectContaining({ rentalId: 1 })
);
});
@@ -306,9 +316,9 @@ describe('PayoutService', () => {
.rejects.toThrow('Database completion update failed');
expect(mockCreateTransfer).toHaveBeenCalled();
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error processing payout for rental 1:',
dbError
expect(mockLoggerError).toHaveBeenCalledWith(
'Error processing payout for rental',
expect.objectContaining({ rentalId: 1 })
);
});
@@ -438,9 +448,9 @@ describe('PayoutService', () => {
await expect(PayoutService.processAllEligiblePayouts())
.rejects.toThrow('Database connection failed');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error processing all eligible payouts:',
dbError
expect(mockLoggerError).toHaveBeenCalledWith(
'Error processing all eligible payouts',
expect.objectContaining({ error: dbError.message })
);
});
@@ -520,7 +530,8 @@ describe('PayoutService', () => {
where: {
stripeConnectedAccountId: {
'not': null
}
},
stripePayoutsEnabled: true
}
},
{
@@ -613,9 +624,9 @@ describe('PayoutService', () => {
await expect(PayoutService.retryFailedPayouts())
.rejects.toThrow('Database query failed');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error retrying failed payouts:',
dbError
expect(mockLoggerError).toHaveBeenCalledWith(
'Error retrying failed payouts',
expect.objectContaining({ error: dbError.message })
);
});
@@ -655,9 +666,9 @@ describe('PayoutService', () => {
await expect(PayoutService.processRentalPayout(mockRental))
.rejects.toThrow('Update failed');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error processing payout for rental 123:',
expect.any(Error)
expect(mockLoggerError).toHaveBeenCalledWith(
'Error processing payout for rental',
expect.objectContaining({ rentalId: 123 })
);
});