Files
TREK/client/src/components/shared/usePlacePhoto.test.ts
T
Maurice db6bb911c2 feat(roadtrip): show the place photo on the stop sheet and the stop dialog (#2629)
On the road a stop's picture is often what decides whether it is worth the
detour. The phone's stop sheet shows the place's picture full width under its
name and the desktop stop dialog shows it in place of the kind tile; both open
the place's photos on tap. The picture is the place's own image, else the
provider photo for its Google or OSM id through the shared photo cache.
2026-11-13 10:17:14 +01:00

66 lines
2.8 KiB
TypeScript

// FE-SHARED-PLACEPHOTO-001 to FE-SHARED-PLACEPHOTO-005
import { renderHook } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { resetAllStores, seedStore } from '../../../tests/helpers/store';
import { fetchPhoto, getCached } from '../../services/photoService';
import { useAuthStore } from '../../store/authStore';
import { usePlacePhoto } from './usePlacePhoto';
vi.mock('../../services/photoService', () => ({ fetchPhoto: vi.fn(), getCached: vi.fn() }));
const PROXY = '/api/maps/place-photo/ChIJ123/bytes';
beforeEach(() => {
resetAllStores();
vi.clearAllMocks();
vi.mocked(getCached).mockReturnValue(undefined);
vi.mocked(fetchPhoto).mockImplementation((_key, _id, _lat, _lng, _name, callback) => {
callback?.({ photoUrl: PROXY, thumbDataUrl: null });
});
});
describe('usePlacePhoto (#2629)', () => {
it("FE-SHARED-PLACEPHOTO-001: the place's own picture wins and asks the provider for nothing", () => {
const { result } = renderHook(() =>
usePlacePhoto({ name: 'Dom', image_url: '/uploads/places/dom.jpg', google_place_id: 'ChIJ123' })
);
expect(result.current).toBe('/uploads/places/dom.jpg');
expect(fetchPhoto).not.toHaveBeenCalled();
});
it('FE-SHARED-PLACEPHOTO-002: without one, the provider picture is asked by its id through the shared cache', () => {
const { result } = renderHook(() =>
usePlacePhoto({ name: 'Dom', google_place_id: 'ChIJ123', lat: 50.9, lng: 6.9 })
);
expect(fetchPhoto).toHaveBeenCalledWith('ChIJ123', 'ChIJ123', 50.9, 6.9, 'Dom', expect.any(Function));
expect(result.current).toBe(PROXY);
});
it('FE-SHARED-PLACEPHOTO-003: a place known only by its position gets no picture of its surroundings', () => {
const { result } = renderHook(() => usePlacePhoto({ name: 'Somewhere', lat: 50.9, lng: 6.9 }));
expect(fetchPhoto).not.toHaveBeenCalled();
expect(result.current).toBeNull();
});
it('FE-SHARED-PLACEPHOTO-004: with photos switched off nothing is asked and nothing is shown', () => {
seedStore(useAuthStore, { placesPhotosEnabled: false });
const { result } = renderHook(() => usePlacePhoto({ name: 'Dom', osm_id: 'way:42' }));
expect(fetchPhoto).not.toHaveBeenCalled();
expect(result.current).toBeNull();
});
it("FE-SHARED-PLACEPHOTO-005: the next place never shows the previous one's picture while its own loads", () => {
vi.mocked(fetchPhoto).mockImplementation((key, _id, _lat, _lng, _name, callback) => {
if (key === 'ChIJ123') callback?.({ photoUrl: PROXY, thumbDataUrl: null });
});
const { result, rerender } = renderHook(({ id }) => usePlacePhoto({ name: 'Stop', google_place_id: id }), {
initialProps: { id: 'ChIJ123' },
});
expect(result.current).toBe(PROXY);
rerender({ id: 'ChIJ456' });
expect(result.current).toBeNull();
});
});