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

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