mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-06-19 13:21:46 +00:00
fix(tests): mock FormData uploads at API boundary to fix CI timeouts
jsdom's FormData is incompatible with undici's ReadableStream serialisation used by MSW 2.x — requests hang under CI resource constraints but pass locally. Replace server.use() + implicit HTTP roundtrip with vi.spyOn().mockResolvedValueOnce() for all five FormData POST tests (uploadAvatar, uploadRestore, addFile, importGpx).
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { useTripStore } from '../../../src/store/tripStore';
|
||||
import { filesApi } from '../../../src/api/client';
|
||||
import { resetAllStores, seedStore } from '../../helpers/store';
|
||||
import { buildTripFile } from '../../helpers/factories';
|
||||
import { server } from '../../helpers/msw/server';
|
||||
@@ -56,14 +57,14 @@ describe('filesSlice', () => {
|
||||
seedStore(useTripStore, { files: [existing] });
|
||||
|
||||
const uploaded = buildTripFile({ trip_id: 1, filename: 'new-upload.pdf' });
|
||||
server.use(
|
||||
http.post('/api/trips/1/files', () => HttpResponse.json({ file: uploaded })),
|
||||
);
|
||||
// FormData POST hangs on CI — mock at the API boundary instead of MSW.
|
||||
const uploadSpy = vi.spyOn(filesApi, 'upload').mockResolvedValueOnce({ file: uploaded });
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('file', new Blob(['content'], { type: 'application/pdf' }), 'new-upload.pdf');
|
||||
|
||||
const result = await useTripStore.getState().addFile(1, formData);
|
||||
uploadSpy.mockRestore();
|
||||
|
||||
expect(result.filename).toBe('new-upload.pdf');
|
||||
const files = useTripStore.getState().files;
|
||||
|
||||
@@ -2,6 +2,7 @@ import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { server } from '../../helpers/msw/server';
|
||||
import { useAuthStore } from '../../../src/store/authStore';
|
||||
import { authApi } from '../../../src/api/client';
|
||||
import { resetAllStores } from '../../helpers/store';
|
||||
import { buildUser } from '../../helpers/factories';
|
||||
|
||||
@@ -425,11 +426,8 @@ describe('authStore', () => {
|
||||
|
||||
describe('FE-STORE-AUTH-UPLOAD: uploadAvatar', () => {
|
||||
it('updates avatar_url from response', async () => {
|
||||
server.use(
|
||||
http.post('/api/auth/avatar', () =>
|
||||
HttpResponse.json({ avatar_url: '/uploads/avatar-new.png' })
|
||||
)
|
||||
);
|
||||
// FormData POST hangs on CI — mock at the API boundary instead of MSW.
|
||||
const uploadSpy = vi.spyOn(authApi, 'uploadAvatar').mockResolvedValueOnce({ avatar_url: '/uploads/avatar-new.png' });
|
||||
|
||||
useAuthStore.setState({ user: buildUser() });
|
||||
|
||||
@@ -438,6 +436,7 @@ describe('authStore', () => {
|
||||
|
||||
expect(result.avatar_url).toBe('/uploads/avatar-new.png');
|
||||
expect(useAuthStore.getState().user?.avatar_url).toBe('/uploads/avatar-new.png');
|
||||
uploadSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user