mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 03:50:30 +00:00
test(places): the picture chosen in the editor, the gallery and the lightbox
Tests for the picker, the shared image field, staging and saving the picture, the gallery hook and trigger, the lightbox's dialog, focus and reading direction, the credit and the enrichment cache; the inspector, phone sheet, details column, form and wiring tests follow the picture into the editor. The picker's upload tile and credit line are components of their own, the heading goes with an empty picker, the gallery's state names its place, and 'No picture' only shows when a picture is being removed.
This commit is contained in:
@@ -1046,14 +1046,16 @@ describe('TripPlannerPage — day detail and inspector', () => {
|
||||
expect(useTripStore.getState().assignments['7'][0].participants).toEqual([{ user_id: 5 }])
|
||||
|
||||
await act(async () => { await props('inspector').onUpdatePlace(1, { name: 'X' }) })
|
||||
await act(async () => { await props('inspector').onUploadImage(1, new File(['x'], 'a.png')) })
|
||||
// The picture is chosen in the editor now: the inspector gets no way to upload or take one.
|
||||
expect(props('inspector').onUploadImage).toBeUndefined()
|
||||
expect(props('inspector').onImageFromFile).toBeUndefined()
|
||||
await act(async () => { await props('inspector').onRate(1, 4) })
|
||||
await act(async () => { await props('inspector').onFileUpload(new FormData()) })
|
||||
|
||||
const tripActions = hookState.tripActions as Record<string, ReturnType<typeof vi.fn>>
|
||||
expect(tripActions.addFile).toHaveBeenCalled()
|
||||
expect(tripActions.updatePlace).toHaveBeenCalledWith(42, 1, { name: 'X' })
|
||||
expect(tripActions.uploadPlaceImage).toHaveBeenCalled()
|
||||
expect(tripActions.uploadPlaceImage).not.toHaveBeenCalled()
|
||||
expect(tripActions.ratePlace).toHaveBeenCalledWith(42, 1, 4)
|
||||
})
|
||||
|
||||
@@ -1095,7 +1097,7 @@ describe('TripPlannerPage — day detail and inspector', () => {
|
||||
expect(hookState.handleDeletePlace).toHaveBeenCalledWith(1)
|
||||
|
||||
await act(async () => { await props('inspector').onUpdatePlace(1, { name: 'Y' }) })
|
||||
await act(async () => { await props('inspector').onUploadImage(1, new File(['x'], 'a.png')) })
|
||||
expect(props('inspector').onUploadImage).toBeUndefined()
|
||||
await act(async () => { await props('inspector').onRate(1, 3) })
|
||||
await act(async () => { await props('inspector').onFileUpload(new FormData()) })
|
||||
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
// FE-TP-PLIMGSAVE-001 to FE-TP-PLIMGSAVE-006: the waiting picture taken off what the place
|
||||
// form saves, and put in place once the place has an id.
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { setForcedOffline } from '../../sync/networkMode';
|
||||
import type { Place } from '../../types';
|
||||
import { savePendingImage, takePendingImage } from './placeImageSave';
|
||||
|
||||
const needsConnection = () => 'places.image.needsConnection';
|
||||
|
||||
function actions() {
|
||||
return {
|
||||
uploadPlaceImage: vi.fn(async () => ({ id: 1 }) as Place),
|
||||
setPlaceImageFromFile: vi.fn(async () => ({ id: 1 }) as Place),
|
||||
};
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
setForcedOffline(false);
|
||||
});
|
||||
|
||||
describe('takePendingImage', () => {
|
||||
it('FE-TP-PLIMGSAVE-001: takes the picture off the data, online', () => {
|
||||
const file = new File(['x'], 'a.png');
|
||||
const data: { name: string; _pendingImage?: File | { fileId: number } } = { name: 'Lake', _pendingImage: file };
|
||||
expect(takePendingImage(data, needsConnection)).toBe(file);
|
||||
expect('_pendingImage' in data).toBe(false);
|
||||
expect(data).toEqual({ name: 'Lake' });
|
||||
});
|
||||
|
||||
it('FE-TP-PLIMGSAVE-002: without a picture nothing is taken, offline included', () => {
|
||||
setForcedOffline(true);
|
||||
const data: { name: string; _pendingImage?: File } = { name: 'Lake' };
|
||||
expect(takePendingImage(data, needsConnection)).toBeUndefined();
|
||||
expect(data).toEqual({ name: 'Lake' });
|
||||
});
|
||||
|
||||
it('FE-TP-PLIMGSAVE-003: offline, a waiting picture refuses the save with the reason', () => {
|
||||
setForcedOffline(true);
|
||||
const data = { _pendingImage: { fileId: 4 } };
|
||||
expect(() => takePendingImage(data, needsConnection)).toThrow('places.image.needsConnection');
|
||||
});
|
||||
});
|
||||
|
||||
describe('savePendingImage', () => {
|
||||
it('FE-TP-PLIMGSAVE-004: a file from the device is uploaded, an attached file is taken by its id', async () => {
|
||||
const a = actions();
|
||||
const onError = vi.fn();
|
||||
const file = new File(['x'], 'a.png');
|
||||
await savePendingImage(a, 42, 7, file, onError);
|
||||
expect(a.uploadPlaceImage).toHaveBeenCalledWith(42, 7, file);
|
||||
expect(a.setPlaceImageFromFile).not.toHaveBeenCalled();
|
||||
|
||||
await savePendingImage(a, 42, 7, { fileId: 13 }, onError);
|
||||
expect(a.setPlaceImageFromFile).toHaveBeenCalledWith(42, 7, 13);
|
||||
expect(onError).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-TP-PLIMGSAVE-005: nothing waiting, or no place to put it on, sends nothing', async () => {
|
||||
const a = actions();
|
||||
await savePendingImage(a, 42, 7, undefined, vi.fn());
|
||||
await savePendingImage(a, 42, undefined, new File(['x'], 'a.png'), vi.fn());
|
||||
expect(a.uploadPlaceImage).not.toHaveBeenCalled();
|
||||
expect(a.setPlaceImageFromFile).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-TP-PLIMGSAVE-006: a refused picture goes to onError instead of failing the save', async () => {
|
||||
const a = actions();
|
||||
const refused = new Error('too large');
|
||||
a.uploadPlaceImage.mockRejectedValueOnce(refused);
|
||||
a.setPlaceImageFromFile.mockRejectedValueOnce(refused);
|
||||
const onError = vi.fn();
|
||||
await expect(savePendingImage(a, 42, 7, new File(['x'], 'a.png'), onError)).resolves.toBeUndefined();
|
||||
await expect(savePendingImage(a, 42, 7, { fileId: 2 }, onError)).resolves.toBeUndefined();
|
||||
expect(onError).toHaveBeenCalledTimes(2);
|
||||
expect(onError).toHaveBeenCalledWith(refused);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
// FE-TP-PLACEEDITS-001 to FE-TP-PLACEEDITS-017
|
||||
// FE-TP-PLACEEDITS-001 to FE-TP-PLACEEDITS-020
|
||||
//
|
||||
// Writes to the trip's places, driven straight through usePlaceEdits. The store holds
|
||||
// the places and day lists the delete and category handlers capture for their undo,
|
||||
@@ -51,6 +51,8 @@ function makeActions() {
|
||||
setAssignmentLegMode: vi.fn(async () => undefined),
|
||||
ratePlace: vi.fn(async () => undefined),
|
||||
addFile: vi.fn(async () => undefined),
|
||||
uploadPlaceImage: vi.fn(async () => undefined),
|
||||
setPlaceImageFromFile: vi.fn(async () => undefined),
|
||||
deletePlace: vi.fn(async (): Promise<{ tourPlaceIds?: number[] } | undefined> => undefined),
|
||||
deletePlacesMany: vi.fn(async (): Promise<{ tourPlaceIds?: number[] } | undefined> => undefined),
|
||||
updatePlacesMany: vi.fn(async () => undefined),
|
||||
@@ -481,4 +483,57 @@ describe('usePlaceEdits', () => {
|
||||
Object.defineProperty(navigator, 'onLine', { value: true, writable: true, configurable: true })
|
||||
}
|
||||
})
|
||||
it('FE-TP-PLACEEDITS-018: an edited place gets its waiting picture after the update, uploaded or taken from its files', async () => {
|
||||
const { result } = renderEdits({ editingPlace: lake })
|
||||
const picture = new File(['x'], 'front.png', { type: 'image/png' })
|
||||
|
||||
await act(async () => { await result.current.handleSavePlace({ name: 'Lake', _pendingImage: picture }) })
|
||||
|
||||
// The picture rides its own route, never the place's update.
|
||||
expect(actions.updatePlace).toHaveBeenCalledWith(42, 1, { name: 'Lake' })
|
||||
expect(actions.uploadPlaceImage).toHaveBeenCalledWith(42, 1, picture)
|
||||
expect(actions.updatePlace.mock.invocationCallOrder[0]).toBeLessThan(actions.uploadPlaceImage.mock.invocationCallOrder[0])
|
||||
|
||||
await act(async () => { await result.current.handleSavePlace({ name: 'Lake', _pendingImage: { fileId: 33 } }) })
|
||||
expect(actions.setPlaceImageFromFile).toHaveBeenCalledWith(42, 1, 33)
|
||||
expect(toast.success).toHaveBeenCalledWith('trip.toast.placeUpdated')
|
||||
})
|
||||
|
||||
it('FE-TP-PLACEEDITS-019: a new place gets its picture once the create has given it an id, and a refused one is reported', async () => {
|
||||
const { result } = renderEdits()
|
||||
const picture = new File(['x'], 'front.png', { type: 'image/png' })
|
||||
actions.uploadPlaceImage.mockRejectedValueOnce(new Error('too large'))
|
||||
|
||||
let saved: unknown
|
||||
await act(async () => { saved = await result.current.handleSavePlace({ name: 'Pump', _pendingImage: picture }) })
|
||||
|
||||
expect(actions.addPlace).toHaveBeenCalledWith(42, { name: 'Pump' })
|
||||
expect(actions.uploadPlaceImage).toHaveBeenCalledWith(42, 900, picture)
|
||||
// The place is saved all the same; only the picture failed.
|
||||
expect(saved).toMatchObject({ id: 900 })
|
||||
expect(toast.error).toHaveBeenCalledWith('places.imageUploadError')
|
||||
expect(toast.success).toHaveBeenCalledWith('trip.toast.placeAdded')
|
||||
|
||||
// A create the store could not make has no place to put an attached file on.
|
||||
actions.addPlace.mockResolvedValueOnce(null)
|
||||
await act(async () => { await result.current.handleSavePlace({ name: 'Ghost', _pendingImage: { fileId: 5 } }) })
|
||||
expect(actions.setPlaceImageFromFile).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('FE-TP-PLACEEDITS-020: offline, a save with a waiting picture is refused before anything is written', async () => {
|
||||
Object.defineProperty(navigator, 'onLine', { value: false, writable: true, configurable: true })
|
||||
try {
|
||||
const { result } = renderEdits({ editingPlace: lake })
|
||||
let refused: unknown
|
||||
await act(async () => {
|
||||
await result.current.handleSavePlace({ name: 'Lake', _pendingImage: { fileId: 5 } }).catch((err: unknown) => { refused = err })
|
||||
})
|
||||
|
||||
expect(refused).toEqual(new Error('places.image.needsConnection'))
|
||||
expect(actions.updatePlace).not.toHaveBeenCalled()
|
||||
expect(actions.setPlaceImageFromFile).not.toHaveBeenCalled()
|
||||
} finally {
|
||||
Object.defineProperty(navigator, 'onLine', { value: true, writable: true, configurable: true })
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user