Files
TREK/client/src/components/Journey/useGalleryUpload.test.ts
T
Maurice d118423570 fix(journey): the desktop gallery Upload button shows the files counting up again
Uploading photos into a journey gallery on desktop gave no sign that
anything was happening until the toast at the end; the Upload button
stayed clickable the whole time. Up to v4.0.0 the button sat inside the
gallery and showed a spinner with "Uploading done/total" while it was
disabled. When the button moved up into the page header (onRegisterUpload)
that indicator was dropped, and the count kept being tracked with no
reader.

useGalleryUpload now also returns the progress it tracks, the gallery
hands it up through onUploadProgress into useJourneyDetail, and the
page's Upload button is disabled and shows the spinner with
journey.editor.uploadingProgress while the files go, as it did before.

Red before the fix: src/pages/JourneyDetailPage.wiring.test.tsx
FE-JRN-DETWIRE-043 failed with "expected undefined to be [Function
Mock]": the gallery was never given an onUploadProgress callback and
the button never showed a count. FE-JRN-GALLERY-021 and
FE-JRN-GALLERYUPLOAD-007 cover the gallery and the hook. No existing
assertions changed.
2026-10-09 19:00:45 +02:00

143 lines
6.4 KiB
TypeScript

// FE-JRN-GALLERYUPLOAD-001 to FE-JRN-GALLERYUPLOAD-007: the gallery upload behind both
// the desktop gallery and the phone journey screen.
import { act, renderHook } from '@testing-library/react';
import type React from 'react';
import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
import { useJourneyStore } from '../../store/journeyStore';
import { useGalleryUpload, type GalleryUploadOptions } from './useGalleryUpload';
vi.mock('../../i18n', () => ({
useTranslation: () => ({
t: (k: string, p?: Record<string, unknown>) => (p ? `${k}:${JSON.stringify(p)}` : k),
}),
}));
vi.mock('../../utils/convertHeic', () => ({
normalizeImageFiles: (files: File[]) => Promise.resolve(files.map((f) => new File([f], `${f.name}.jpg`))),
}));
let addToast: Mock<NonNullable<Window['__addToast']>>;
let upload: Mock;
const initial = useJourneyStore.getState();
function setup(over: Partial<GalleryUploadOptions> = {}) {
const props: GalleryUploadOptions = { journeyId: 12, onUploaded: vi.fn(), ...over };
const hook = renderHook((p: GalleryUploadOptions) => useGalleryUpload(p), { initialProps: props });
return { ...hook, props };
}
function changeEvent(files: File[]) {
const target = { files, value: 'C:\\fake\\x' };
return { event: { target } as unknown as React.ChangeEvent<HTMLInputElement>, target };
}
beforeEach(() => {
addToast = vi.fn<NonNullable<Window['__addToast']>>();
window.__addToast = addToast;
upload = vi.fn(async () => ({ succeeded: [], failed: [] }));
useJourneyStore.setState({ uploadGalleryPhotos: upload } as never);
});
afterEach(() => {
useJourneyStore.setState(initial, true);
delete window.__addToast;
});
describe('useGalleryUpload', () => {
it('FE-JRN-GALLERYUPLOAD-001: normalises images, sends videos as they are, toasts and reloads', async () => {
const image = new File(['i'], 'a.heic', { type: 'image/heic' });
const video = new File(['v'], 'b.mp4', { type: 'video/mp4' });
const { result, props } = setup();
const { event, target } = changeEvent([image, video]);
await act(() => result.current.handleGalleryUpload(event));
const sent = upload.mock.calls[0][1] as File[];
expect(upload.mock.calls[0]).toHaveLength(2);
expect(upload.mock.calls[0][0]).toBe(12);
expect(sent.map((f) => f.name)).toEqual(['a.heic.jpg', 'b.mp4']);
expect(addToast).toHaveBeenCalledWith('journey.photosUploaded:{"count":2}', 'success', undefined);
expect(props.onUploaded).toHaveBeenCalledTimes(1);
expect(result.current.uploading).toBe(false);
expect(target.value).toBe('');
});
it('FE-JRN-GALLERYUPLOAD-002: the desktop gallery hands the upload a progress callback', async () => {
upload.mockImplementationOnce(async (_id: number, files: File[], cbs?: { onProgress?: (p: unknown) => void }) => {
cbs?.onProgress?.({ done: 1, total: files.length });
return { succeeded: [], failed: [] };
});
const { result } = setup({ trackProgress: true });
await act(() => result.current.handleGalleryUpload(changeEvent([new File(['i'], 'a.jpg')]).event));
expect(upload.mock.calls[0]).toHaveLength(3);
expect(result.current.uploading).toBe(false);
});
it('FE-JRN-GALLERYUPLOAD-003: a partial failure says how many did not make it, and still reloads', async () => {
const a = new File(['a'], 'a.jpg');
upload.mockResolvedValueOnce({ succeeded: [], failed: [a] });
const { result, props } = setup();
await act(() => result.current.handleGalleryUpload(changeEvent([a, new File(['b'], 'b.jpg')]).event));
expect(addToast).toHaveBeenCalledWith(
'journey.editor.uploadPartialFailed:{"failed":"1","total":"2"}',
'error',
undefined
);
expect(props.onUploaded).toHaveBeenCalledTimes(1);
});
it('FE-JRN-GALLERYUPLOAD-004: a failed upload toasts the server message and does not reload', async () => {
upload.mockRejectedValueOnce({ response: { data: { error: 'Quota' } } });
const { result, props } = setup();
await act(() => result.current.handleGalleryUpload(changeEvent([new File(['a'], 'a.jpg')]).event));
expect(addToast).toHaveBeenCalledWith('Quota', 'error', undefined);
expect(props.onUploaded).not.toHaveBeenCalled();
expect(result.current.uploading).toBe(false);
});
it('FE-JRN-GALLERYUPLOAD-005: nothing goes up without files or before the journey is known', async () => {
const empty = setup();
await act(() => empty.result.current.handleGalleryUpload(changeEvent([]).event));
const unknown = setup({ journeyId: null });
const { event, target } = changeEvent([new File(['a'], 'a.jpg')]);
await act(() => unknown.result.current.handleGalleryUpload(event));
expect(upload).not.toHaveBeenCalled();
expect(target.value).toBe('C:\\fake\\x');
});
it('FE-JRN-GALLERYUPLOAD-006: the phone screen hands in the toast and translate its page hook holds', async () => {
const toast = { success: vi.fn(), error: vi.fn(), warning: vi.fn(), info: vi.fn() };
const t = vi.fn((k: string) => `view:${k}`);
const { result } = setup({ toast, t } as Partial<GalleryUploadOptions>);
await act(() => result.current.handleGalleryUpload(changeEvent([new File(['a'], 'a.jpg')]).event));
expect(toast.success).toHaveBeenCalledWith('view:journey.photosUploaded');
expect(addToast).not.toHaveBeenCalled();
});
it('FE-JRN-GALLERYUPLOAD-007: progress counts the files up while they go and is gone after', async () => {
let finish: () => void = () => {};
let report: (p: unknown) => void = () => {};
upload.mockImplementationOnce(
(_id: number, _files: File[], cbs?: { onProgress?: (p: unknown) => void }) =>
new Promise((resolve) => {
report = (p) => cbs?.onProgress?.(p);
finish = () => resolve({ succeeded: [], failed: [] });
})
);
const { result } = setup({ trackProgress: true });
expect(result.current.progress).toBeNull();
let running: Promise<void> = Promise.resolve();
await act(async () => {
running = result.current.handleGalleryUpload(
changeEvent([new File(['a'], 'a.jpg'), new File(['b'], 'b.jpg')]).event
);
});
expect(result.current.progress).toEqual({ done: 0, total: 2 });
act(() => report({ done: 1, total: 2 }));
expect(result.current.progress).toEqual({ done: 1, total: 2 });
await act(async () => {
finish();
await running;
});
expect(result.current.progress).toBeNull();
});
});