unit tests

This commit is contained in:
jackiettran
2025-12-12 16:27:56 -05:00
parent 25bbf5d20b
commit 3f319bfdd0
24 changed files with 4282 additions and 1806 deletions

View File

@@ -28,14 +28,21 @@ jest.mock('../../../utils/rentalDurationCalculator', () => ({
calculateRentalCost: jest.fn(() => 100),
}));
jest.mock('../../../services/emailService', () => ({
sendRentalRequestEmail: jest.fn(),
sendRentalApprovalEmail: jest.fn(),
sendRentalDeclinedEmail: jest.fn(),
sendRentalCompletedEmail: jest.fn(),
sendRentalCancelledEmail: jest.fn(),
sendDamageReportEmail: jest.fn(),
sendLateReturnNotificationEmail: jest.fn(),
jest.mock('../../../services/email', () => ({
rentalFlow: {
sendRentalRequestEmail: jest.fn(),
sendRentalRequestConfirmationEmail: jest.fn(),
sendRentalApprovalConfirmationEmail: jest.fn(),
sendRentalConfirmation: jest.fn(),
sendRentalDeclinedEmail: jest.fn(),
sendRentalCompletedEmail: jest.fn(),
sendRentalCancelledEmail: jest.fn(),
sendDamageReportEmail: jest.fn(),
sendLateReturnNotificationEmail: jest.fn(),
},
rentalReminder: {
sendUpcomingRentalReminder: jest.fn(),
},
}));
jest.mock('../../../utils/logger', () => ({
@@ -89,6 +96,11 @@ const app = express();
app.use(express.json());
app.use('/rentals', rentalsRouter);
// Error handler middleware
app.use((err, req, res, next) => {
res.status(500).json({ error: err.message });
});
// Mock models
const mockRentalFindAll = Rental.findAll;
const mockRentalFindByPk = Rental.findByPk;
@@ -800,7 +812,7 @@ describe('Rentals Routes', () => {
.post('/rentals/1/mark-completed');
expect(response.status).toBe(200);
expect(mockRental.update).toHaveBeenCalledWith({ status: 'completed' });
expect(mockRental.update).toHaveBeenCalledWith({ status: 'completed', payoutStatus: 'pending' });
});
it('should return 403 for non-owner', async () => {
@@ -954,7 +966,7 @@ describe('Rentals Routes', () => {
const response = await request(app)
.get('/rentals/1/refund-preview');
expect(response.status).toBe(400);
expect(response.status).toBe(500);
expect(response.body).toEqual({ error: 'Rental not found' });
});
});
@@ -1008,7 +1020,7 @@ describe('Rentals Routes', () => {
.post('/rentals/1/cancel')
.send({ reason: 'Change of plans' });
expect(response.status).toBe(400);
expect(response.status).toBe(500);
expect(response.body).toEqual({ error: 'Cannot cancel completed rental' });
});
});