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

@@ -11,18 +11,26 @@ jest.mock('@googlemaps/google-maps-services-js', () => ({
}))
}));
const mockLoggerInfo = jest.fn();
const mockLoggerError = jest.fn();
jest.mock('../../../utils/logger', () => ({
info: mockLoggerInfo,
error: mockLoggerError,
warn: jest.fn(),
withRequestId: jest.fn(() => ({
info: jest.fn(),
error: jest.fn(),
warn: jest.fn(),
})),
}));
describe('GoogleMapsService', () => {
let service;
let consoleSpy, consoleErrorSpy;
beforeEach(() => {
// Clear all mocks
jest.clearAllMocks();
// Set up console spies
consoleSpy = jest.spyOn(console, 'log').mockImplementation();
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
// Reset environment
delete process.env.GOOGLE_MAPS_API_KEY;
@@ -30,18 +38,13 @@ describe('GoogleMapsService', () => {
jest.resetModules();
});
afterEach(() => {
consoleSpy.mockRestore();
consoleErrorSpy.mockRestore();
});
describe('Constructor', () => {
it('should initialize with API key and log success', () => {
process.env.GOOGLE_MAPS_API_KEY = 'test-api-key';
service = require('../../../services/googleMapsService');
expect(consoleSpy).toHaveBeenCalledWith('Google Maps service initialized');
expect(mockLoggerInfo).toHaveBeenCalledWith('Google Maps service initialized');
expect(service.isConfigured()).toBe(true);
});
@@ -50,7 +53,7 @@ describe('GoogleMapsService', () => {
service = require('../../../services/googleMapsService');
expect(consoleErrorSpy).toHaveBeenCalledWith('Google Maps API key not configured in environment variables');
expect(mockLoggerError).toHaveBeenCalledWith('Google Maps API key not configured in environment variables');
expect(service.isConfigured()).toBe(false);
});
@@ -299,10 +302,11 @@ describe('GoogleMapsService', () => {
status: 'ZERO_RESULTS'
});
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Places Autocomplete API error:',
'ZERO_RESULTS',
'No results found'
expect(mockLoggerError).toHaveBeenCalledWith(
'Places Autocomplete API error',
expect.objectContaining({
status: 'ZERO_RESULTS',
})
);
});
@@ -325,7 +329,7 @@ describe('GoogleMapsService', () => {
await expect(service.getPlacesAutocomplete('test input')).rejects.toThrow('Failed to fetch place predictions');
expect(consoleErrorSpy).toHaveBeenCalledWith('Places Autocomplete service error:', 'Network error');
expect(mockLoggerError).toHaveBeenCalledWith('Places Autocomplete service error', expect.objectContaining({ error: expect.any(Error) }));
});
});
});
@@ -557,10 +561,11 @@ describe('GoogleMapsService', () => {
await expect(service.getPlaceDetails('ChIJ123')).rejects.toThrow('The specified place was not found');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Place Details API error:',
'NOT_FOUND',
'Place not found'
expect(mockLoggerError).toHaveBeenCalledWith(
'Place Details API error',
expect.objectContaining({
status: 'NOT_FOUND',
})
);
});
@@ -582,7 +587,7 @@ describe('GoogleMapsService', () => {
await expect(service.getPlaceDetails('ChIJ123')).rejects.toThrow(originalError);
expect(consoleErrorSpy).toHaveBeenCalledWith('Place Details service error:', 'Network error');
expect(mockLoggerError).toHaveBeenCalledWith('Place Details service error', expect.objectContaining({ error: expect.any(Error) }));
});
});
});
@@ -769,10 +774,11 @@ describe('GoogleMapsService', () => {
status: 'ZERO_RESULTS'
});
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Geocoding API error:',
'ZERO_RESULTS',
'No results found'
expect(mockLoggerError).toHaveBeenCalledWith(
'Geocoding API error',
expect.objectContaining({
status: 'ZERO_RESULTS',
})
);
});
@@ -796,7 +802,7 @@ describe('GoogleMapsService', () => {
await expect(service.geocodeAddress('123 Test St')).rejects.toThrow('Failed to geocode address');
expect(consoleErrorSpy).toHaveBeenCalledWith('Geocoding service error:', 'Network error');
expect(mockLoggerError).toHaveBeenCalledWith('Geocoding service error', expect.objectContaining({ error: expect.any(Error) }));
});
});
});

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 })
);
});

View File

@@ -13,6 +13,19 @@ jest.mock('../../../services/stripeService', () => ({
createRefund: mockCreateRefund
}));
const mockLoggerError = jest.fn();
const mockLoggerWarn = jest.fn();
jest.mock('../../../utils/logger', () => ({
error: mockLoggerError,
warn: mockLoggerWarn,
info: jest.fn(),
withRequestId: jest.fn(() => ({
error: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
})),
}));
const RefundService = require('../../../services/refundService');
describe('RefundService', () => {
@@ -540,8 +553,9 @@ describe('RefundService', () => {
const result = await RefundService.processCancellation(1, 200);
expect(mockCreateRefund).not.toHaveBeenCalled();
expect(consoleWarnSpy).toHaveBeenCalledWith(
'Refund amount calculated but no payment intent ID for rental 1'
expect(mockLoggerWarn).toHaveBeenCalledWith(
'Refund amount calculated but no payment intent ID for rental',
{ rentalId: 1 }
);
expect(result.refund).toEqual({
@@ -605,9 +619,9 @@ describe('RefundService', () => {
await expect(RefundService.processCancellation(1, 200))
.rejects.toThrow('Failed to process refund: Refund failed');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error processing Stripe refund:',
stripeError
expect(mockLoggerError).toHaveBeenCalledWith(
'Error processing Stripe refund',
expect.objectContaining({ error: stripeError })
);
});

View File

@@ -41,27 +41,28 @@ jest.mock('stripe', () => {
}));
});
const mockLoggerError = jest.fn();
jest.mock('../../../utils/logger', () => ({
error: mockLoggerError,
info: jest.fn(),
warn: jest.fn(),
withRequestId: jest.fn(() => ({
error: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
})),
}));
const StripeService = require('../../../services/stripeService');
describe('StripeService', () => {
let consoleSpy, consoleErrorSpy;
beforeEach(() => {
jest.clearAllMocks();
// Set up console spies
consoleSpy = jest.spyOn(console, 'log').mockImplementation();
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
// Set environment variables for tests
process.env.FRONTEND_URL = 'http://localhost:3000';
});
afterEach(() => {
consoleSpy.mockRestore();
consoleErrorSpy.mockRestore();
});
describe('getCheckoutSession', () => {
it('should retrieve checkout session successfully', async () => {
const mockSession = {
@@ -93,9 +94,11 @@ describe('StripeService', () => {
await expect(StripeService.getCheckoutSession('invalid_session'))
.rejects.toThrow('Session not found');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error retrieving checkout session:',
stripeError
expect(mockLoggerError).toHaveBeenCalledWith(
'Error retrieving checkout session',
expect.objectContaining({
error: stripeError.message,
})
);
});
@@ -174,9 +177,11 @@ describe('StripeService', () => {
email: 'invalid-email'
})).rejects.toThrow('Invalid email address');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error creating connected account:',
stripeError
expect(mockLoggerError).toHaveBeenCalledWith(
'Error creating connected account',
expect.objectContaining({
error: stripeError.message,
})
);
});
@@ -225,9 +230,11 @@ describe('StripeService', () => {
'http://localhost:3000/return'
)).rejects.toThrow('Account not found');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error creating account link:',
stripeError
expect(mockLoggerError).toHaveBeenCalledWith(
'Error creating account link',
expect.objectContaining({
error: stripeError.message,
})
);
});
@@ -287,9 +294,11 @@ describe('StripeService', () => {
await expect(StripeService.getAccountStatus('invalid_account'))
.rejects.toThrow('Account not found');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error retrieving account status:',
stripeError
expect(mockLoggerError).toHaveBeenCalledWith(
'Error retrieving account status',
expect.objectContaining({
error: stripeError.message,
})
);
});
@@ -420,9 +429,11 @@ describe('StripeService', () => {
destination: 'acct_123456789'
})).rejects.toThrow('Insufficient funds');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error creating transfer:',
stripeError
expect(mockLoggerError).toHaveBeenCalledWith(
'Error creating transfer',
expect.objectContaining({
error: stripeError.message,
})
);
});
@@ -558,9 +569,11 @@ describe('StripeService', () => {
amount: 50.00
})).rejects.toThrow('Payment intent not found');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error creating refund:',
stripeError
expect(mockLoggerError).toHaveBeenCalledWith(
'Error creating refund',
expect.objectContaining({
error: stripeError.message,
})
);
});
@@ -617,9 +630,11 @@ describe('StripeService', () => {
await expect(StripeService.getRefund('re_invalid'))
.rejects.toThrow('Refund not found');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error retrieving refund:',
stripeError
expect(mockLoggerError).toHaveBeenCalledWith(
'Error retrieving refund',
expect.objectContaining({
error: stripeError.message,
})
);
});
@@ -698,11 +713,13 @@ describe('StripeService', () => {
'pm_invalid',
50.00,
'cus_123456789'
)).rejects.toThrow('Payment method declined');
)).rejects.toThrow('The payment could not be processed.');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error charging payment method:',
stripeError
expect(mockLoggerError).toHaveBeenCalledWith(
'Payment failed',
expect.objectContaining({
code: expect.any(String),
})
);
});
@@ -839,9 +856,11 @@ describe('StripeService', () => {
email: 'invalid-email'
})).rejects.toThrow('Invalid email format');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error creating customer:',
stripeError
expect(mockLoggerError).toHaveBeenCalledWith(
'Error creating customer',
expect.objectContaining({
error: stripeError.message,
})
);
});
@@ -886,6 +905,11 @@ describe('StripeService', () => {
mode: 'setup',
ui_mode: 'embedded',
redirect_on_completion: 'never',
payment_method_options: {
card: {
request_three_d_secure: 'any',
},
},
metadata: {
type: 'payment_method_setup',
userId: '123'
@@ -919,6 +943,11 @@ describe('StripeService', () => {
mode: 'setup',
ui_mode: 'embedded',
redirect_on_completion: 'never',
payment_method_options: {
card: {
request_three_d_secure: 'any',
},
},
metadata: {
type: 'payment_method_setup'
}
@@ -934,9 +963,11 @@ describe('StripeService', () => {
customerId: 'cus_invalid'
})).rejects.toThrow('Customer not found');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error creating setup checkout session:',
stripeError
expect(mockLoggerError).toHaveBeenCalledWith(
'Error creating setup checkout session',
expect.objectContaining({
error: stripeError.message,
})
);
});
@@ -1015,9 +1046,11 @@ describe('StripeService', () => {
destination: 'acct_123456789'
})).rejects.toThrow('Request timeout');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error creating transfer:',
timeoutError
expect(mockLoggerError).toHaveBeenCalledWith(
'Error creating transfer',
expect.objectContaining({
error: timeoutError.message,
})
);
});
@@ -1030,9 +1063,11 @@ describe('StripeService', () => {
email: 'test@example.com'
})).rejects.toThrow('Invalid API key');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Error creating customer:',
apiKeyError
expect(mockLoggerError).toHaveBeenCalledWith(
'Error creating customer',
expect.objectContaining({
error: apiKeyError.message,
})
);
});
});

View File

@@ -442,6 +442,10 @@ describe("StripeWebhookService", () => {
expect(mockUser.update).toHaveBeenCalledWith({
stripeConnectedAccountId: null,
stripePayoutsEnabled: false,
stripeDisabledReason: null,
stripeRequirementsCurrentlyDue: [],
stripeRequirementsPastDue: [],
stripeRequirementsLastUpdated: null,
});
});
@@ -525,12 +529,12 @@ describe("StripeWebhookService", () => {
);
});
it("should use name fallback when firstName is not available", async () => {
it("should use lastName fallback when firstName is not available", async () => {
const mockUser = {
id: 1,
email: "owner@test.com",
firstName: null,
name: "Full Name",
lastName: "Smith",
update: jest.fn().mockResolvedValue(true),
};
@@ -545,7 +549,7 @@ describe("StripeWebhookService", () => {
expect(emailServices.payment.sendAccountDisconnectedEmail).toHaveBeenCalledWith(
"owner@test.com",
expect.objectContaining({
ownerName: "Full Name",
ownerName: "Smith",
})
);
});