Refactor tests and API context integration
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 5m55s
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 5m55s
- Updated tests in `useShoppingLists`, `useWatchedItems`, and various components to use `waitFor` for asynchronous assertions. - Enhanced error handling in `errorHandler` middleware tests to include validation errors and status codes. - Modified `AuthView`, `ProfileManager.Auth`, and `ProfileManager.Authenticated` tests to check for `AbortSignal` in API calls. - Removed duplicate assertions in `auth.routes.test`, `budget.routes.test`, and `gamification.routes.test`. - Introduced reusable logger matcher in `budget.routes.test`, `deals.routes.test`, `flyer.routes.test`, and `user.routes.test`. - Simplified API client mock in `aiApiClient.test` to handle token overrides correctly. - Removed unused `apiUtils.ts` file. - Added `ApiContext` and `ApiProvider` for better API client management in React components.
This commit is contained in:
@@ -18,12 +18,19 @@ vi.mock('./logger.client', () => ({
|
||||
}));
|
||||
|
||||
// 2. Mock ./apiClient to simply pass calls through to the global fetch.
|
||||
vi.mock('./apiClient', () => ({
|
||||
apiFetch: (url: string, options: RequestInit = {}) => {
|
||||
vi.mock('./apiClient', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('./apiClient')>();
|
||||
return {
|
||||
apiFetch: (url: string, options: RequestInit = {}, apiOptions: import('./apiClient').ApiOptions = {}) => {
|
||||
// The base URL must match what MSW is expecting.
|
||||
const fullUrl = url.startsWith('/')
|
||||
? `http://localhost/api${url}`
|
||||
: url;
|
||||
// FIX: Correctly handle the tokenOverride by merging it into the request headers.
|
||||
if (apiOptions.tokenOverride) {
|
||||
options.headers = { ...options.headers, Authorization: `Bearer ${apiOptions.tokenOverride}` };
|
||||
}
|
||||
|
||||
// FIX: Manually construct a Request object. This ensures that when `options.body`
|
||||
// is FormData, the contained File objects are correctly processed by MSW's parsers,
|
||||
// preserving their original filenames instead of defaulting to "blob".
|
||||
@@ -31,7 +38,8 @@ vi.mock('./apiClient', () => ({
|
||||
},
|
||||
// Add a mock for ApiOptions to satisfy the compiler
|
||||
ApiOptions: vi.fn()
|
||||
}));
|
||||
};
|
||||
});
|
||||
|
||||
// 3. Setup MSW to capture requests
|
||||
const requestSpy = vi.fn();
|
||||
@@ -269,7 +277,7 @@ describe('AI API Client (Network Mocking with MSW)', () => {
|
||||
const store: import('../types').Store = { store_id: 1, name: 'Test Store', created_at: new Date().toISOString() };
|
||||
const userLocation: GeolocationCoordinates = { latitude: 45, longitude: -75, accuracy: 0, altitude: null, altitudeAccuracy: null, heading: null, speed: null, toJSON: () => ({}) };
|
||||
|
||||
await aiApiClient.planTripWithMaps(items, store, userLocation); // This was a duplicate, fixed.
|
||||
await aiApiClient.planTripWithMaps(items, store, userLocation);
|
||||
|
||||
expect(requestSpy).toHaveBeenCalledTimes(1);
|
||||
const req = requestSpy.mock.calls[0][0];
|
||||
|
||||
Reference in New Issue
Block a user