more test improvements
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 21m5s

This commit is contained in:
2026-01-03 00:02:05 -08:00
parent 007ff8e538
commit 498fbd9e0e
14 changed files with 492 additions and 106 deletions

View File

@@ -24,58 +24,8 @@ import { cleanupFiles } from '../tests/utils/cleanupFiles';
import { logger } from '../services/logger.server';
import { userService } from '../services/userService';
// 1. Mock the Service Layer directly.
// The user.routes.ts file imports from '.../db/index.db'. We need to mock that module.
vi.mock('../services/db/index.db', () => ({
// Repository instances
userRepo: {
findUserProfileById: vi.fn(),
updateUserProfile: vi.fn(),
updateUserPreferences: vi.fn(),
},
personalizationRepo: {
getWatchedItems: vi.fn(),
removeWatchedItem: vi.fn(),
addWatchedItem: vi.fn(),
getUserDietaryRestrictions: vi.fn(),
setUserDietaryRestrictions: vi.fn(),
getUserAppliances: vi.fn(),
setUserAppliances: vi.fn(),
},
shoppingRepo: {
getShoppingLists: vi.fn(),
createShoppingList: vi.fn(),
deleteShoppingList: vi.fn(),
addShoppingListItem: vi.fn(),
updateShoppingListItem: vi.fn(),
removeShoppingListItem: vi.fn(),
getShoppingListById: vi.fn(), // Added missing mock
},
recipeRepo: {
deleteRecipe: vi.fn(),
updateRecipe: vi.fn(),
},
addressRepo: {
getAddressById: vi.fn(),
upsertAddress: vi.fn(),
},
notificationRepo: {
getNotificationsForUser: vi.fn(),
markAllNotificationsAsRead: vi.fn(),
markNotificationAsRead: vi.fn(),
},
}));
// Mock userService
vi.mock('../services/userService', () => ({
userService: {
updateUserAvatar: vi.fn(),
updateUserPassword: vi.fn(),
deleteUserAccount: vi.fn(),
getUserAddress: vi.fn(),
upsertUserAddress: vi.fn(),
},
}));
// Mocks for db/index.db, userService, and logger are now centralized in `src/tests/setup/tests-setup-unit.ts`.
// This avoids repetition across test files.
// Mock the logger
vi.mock('../services/logger.server', async () => ({
@@ -1080,7 +1030,7 @@ describe('User Routes (/api/users)', () => {
it('should upload an avatar and update the user profile', async () => {
const mockUpdatedProfile = createMockUserProfile({
...mockUserProfile,
avatar_url: '/uploads/avatars/new-avatar.png',
avatar_url: 'http://localhost:3001/uploads/avatars/new-avatar.png',
});
vi.mocked(userService.updateUserAvatar).mockResolvedValue(mockUpdatedProfile);
@@ -1092,7 +1042,7 @@ describe('User Routes (/api/users)', () => {
.attach('avatar', Buffer.from('dummy-image-content'), dummyImagePath);
expect(response.status).toBe(200);
expect(response.body.avatar_url).toContain('/uploads/avatars/'); // This was a duplicate, fixed.
expect(response.body.avatar_url).toContain('http://localhost:3001/uploads/avatars/');
expect(userService.updateUserAvatar).toHaveBeenCalledWith(
mockUserProfile.user.user_id,
expect.any(Object),