Some checks failed
Deploy to Test Environment / deploy-to-test (push) Failing after 1m1s
85 lines
3.2 KiB
TypeScript
85 lines
3.2 KiB
TypeScript
// src/tests/setup/globalApiMock.ts
|
|
import { vi } from 'vitest';
|
|
|
|
/**
|
|
* Mocks the entire apiClient module.
|
|
* This global mock is loaded for all tests via the `setupFiles` config in vitest.config.ts.
|
|
* It prevents test failures in components that use providers (like FlyersProvider, AuthProvider)
|
|
* which make API calls on mount when using `renderWithProviders`.
|
|
*
|
|
* Individual tests can override specific functions as needed, for example:
|
|
*
|
|
* import { vi } from 'vitest';
|
|
* import * as apiClient from '../services/apiClient';
|
|
*
|
|
* const mockedApiClient = vi.mocked(apiClient);
|
|
*
|
|
* it('should test something', () => {
|
|
* mockedApiClient.someFunction.mockResolvedValue({ ... });
|
|
* // ... rest of the test
|
|
* });
|
|
*/
|
|
// Global mock for apiClient - provides defaults for tests using renderWithProviders.
|
|
// Note: Individual test files must also call vi.mock() with their relative path.
|
|
vi.mock('../../services/apiClient', () => ({
|
|
// --- Provider Mocks (with default successful responses) ---
|
|
// These are essential for any test using renderWithProviders, as AppProviders
|
|
// will mount all these data providers.
|
|
fetchFlyers: vi.fn(() =>
|
|
Promise.resolve(new Response(JSON.stringify({ flyers: [], hasMore: false }))),
|
|
),
|
|
fetchMasterItems: vi.fn(() => Promise.resolve(new Response(JSON.stringify([])))),
|
|
fetchWatchedItems: vi.fn(() => Promise.resolve(new Response(JSON.stringify([])))),
|
|
fetchShoppingLists: vi.fn(() => Promise.resolve(new Response(JSON.stringify([])))),
|
|
getAuthenticatedUserProfile: vi.fn(() => Promise.resolve(new Response(JSON.stringify(null)))),
|
|
fetchCategories: vi.fn(() => Promise.resolve(new Response(JSON.stringify([])))), // For CorrectionsPage
|
|
fetchAllBrands: vi.fn(() => Promise.resolve(new Response(JSON.stringify([])))), // For AdminBrandManager
|
|
|
|
// --- General Mocks (return empty vi.fn() by default) ---
|
|
// These functions are commonly used and can be implemented in specific tests.
|
|
suggestRecipe: vi.fn(),
|
|
getApplicationStats: vi.fn(),
|
|
getSuggestedCorrections: vi.fn(),
|
|
approveCorrection: vi.fn(),
|
|
rejectCorrection: vi.fn(),
|
|
updateSuggestedCorrection: vi.fn(),
|
|
pingBackend: vi.fn(),
|
|
checkStorage: vi.fn(),
|
|
checkDbPoolHealth: vi.fn(),
|
|
checkPm2Status: vi.fn(),
|
|
checkRedisHealth: vi.fn(),
|
|
checkDbSchema: vi.fn(),
|
|
loginUser: vi.fn(),
|
|
registerUser: vi.fn(),
|
|
requestPasswordReset: vi.fn(),
|
|
triggerFailingJob: vi.fn(),
|
|
clearGeocodeCache: vi.fn(),
|
|
uploadBrandLogo: vi.fn(),
|
|
fetchActivityLog: vi.fn(),
|
|
updateUserProfile: vi.fn(),
|
|
updateUserPassword: vi.fn(),
|
|
updateUserPreferences: vi.fn(),
|
|
exportUserData: vi.fn(),
|
|
deleteUserAccount: vi.fn(),
|
|
getUserAddress: vi.fn(),
|
|
updateUserAddress: vi.fn(),
|
|
geocodeAddress: vi.fn(),
|
|
getFlyersForReview: vi.fn(),
|
|
fetchLeaderboard: vi.fn(),
|
|
// --- Added to fix "No export is defined on the mock" errors ---
|
|
fetchFlyerItems: vi.fn(),
|
|
createShoppingList: vi.fn(),
|
|
deleteShoppingList: vi.fn(),
|
|
addShoppingListItem: vi.fn(),
|
|
updateShoppingListItem: vi.fn(),
|
|
removeShoppingListItem: vi.fn(),
|
|
addWatchedItem: vi.fn(),
|
|
removeWatchedItem: vi.fn(),
|
|
fetchBestSalePrices: vi.fn(),
|
|
resetPassword: vi.fn(),
|
|
getUserAchievements: vi.fn(),
|
|
uploadAvatar: vi.fn(),
|
|
countFlyerItemsForFlyers: vi.fn(),
|
|
fetchFlyerItemsForFlyers: vi.fn(),
|
|
}));
|