From 314b087f98f99169469912e64e39ca23fabcae5d Mon Sep 17 00:00:00 2001 From: Maurice Date: Fri, 30 Oct 2026 23:14:32 +0000 Subject: [PATCH] planner: the refresh pill compares with the place picked anew in the search box After another place was picked while editing, the pill still offered the saved place's details and would have written them onto the new one. --- .../Planner/usePlaceRefresh.test.ts | 38 +++++++++++++++++++ .../src/components/Planner/usePlaceRefresh.ts | 12 ++++-- 2 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 client/src/components/Planner/usePlaceRefresh.test.ts diff --git a/client/src/components/Planner/usePlaceRefresh.test.ts b/client/src/components/Planner/usePlaceRefresh.test.ts new file mode 100644 index 000000000..e984da72c --- /dev/null +++ b/client/src/components/Planner/usePlaceRefresh.test.ts @@ -0,0 +1,38 @@ +// FE-PLANNER-REFRESHHOOK-001 to FE-PLANNER-REFRESHHOOK-002 +import { renderHook, waitFor } from '@testing-library/react'; +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { placeDetailsRepo } from '../../repo/placeDetailsRepo'; +import type { Place } from '../../types'; +import { DEFAULT_FORM, type PlaceFormData } from './PlaceFormModal.helpers'; +import { usePlaceRefresh } from './usePlaceRefresh'; + +const SAVED = { id: 1, name: 'Café Luna', google_place_id: 'g-luna', osm_id: null } as unknown as Place; + +const DETAILS: Record> = { + 'g-luna': { website: 'https://cafe-luna.de' }, + 'g-sole': { website: 'https://sole.example', phone: '+39 06 1234' }, +}; + +afterEach(() => { + vi.restoreAllMocks(); +}); + +describe('usePlaceRefresh', () => { + it('FE-PLANNER-REFRESHHOOK-001: a saved place is compared with its own fresh details', async () => { + const details = vi.spyOn(placeDetailsRepo, 'details').mockImplementation(async (id) => DETAILS[id] ?? null); + const form: PlaceFormData = { ...DEFAULT_FORM }; + const { result } = renderHook(() => usePlaceRefresh({ place: SAVED, form, language: 'de' })); + await waitFor(() => expect(result.current.candidates).toHaveLength(1)); + expect(details).toHaveBeenCalledWith('g-luna', 'de'); + expect(result.current.candidates[0]).toMatchObject({ field: 'website', current: '' }); + }); + + it('FE-PLANNER-REFRESHHOOK-002: after another place is picked in the search box, its details are the ones compared', async () => { + const details = vi.spyOn(placeDetailsRepo, 'details').mockImplementation(async (id) => DETAILS[id] ?? null); + const form: PlaceFormData = { ...DEFAULT_FORM, google_place_id: 'g-sole', website: 'https://sole.example' }; + const { result } = renderHook(() => usePlaceRefresh({ place: SAVED, form, language: 'de' })); + await waitFor(() => expect(result.current.candidates).toHaveLength(1)); + expect(details).not.toHaveBeenCalledWith('g-luna', 'de'); + expect(result.current.candidates[0]).toMatchObject({ field: 'phone', next: '+39 06 1234' }); + }); +}); diff --git a/client/src/components/Planner/usePlaceRefresh.ts b/client/src/components/Planner/usePlaceRefresh.ts index 37a826028..08199478f 100644 --- a/client/src/components/Planner/usePlaceRefresh.ts +++ b/client/src/components/Planner/usePlaceRefresh.ts @@ -6,10 +6,14 @@ import type { PlaceFormData } from './PlaceFormModal.helpers'; import { resultOrigin } from './placeFieldOrigins'; import { refreshCandidates, type RefreshCandidate } from './placeRefresh'; -/** The id the details lookup takes for a saved place: its Google id, else its OSM or index id. */ -function detailIdOf(place: Place | null | undefined): string | null { +/** + * The id the details lookup takes for a saved place: its Google id, else its + * OSM or index id. A place picked anew in the search box brings its own ids + * into the form, and those win, so the details compared are the picked place's. + */ +function detailIdOf(place: Place | null | undefined, form: PlaceFormData): string | null { if (!place) return null; - return place.google_place_id || place.osm_id || null; + return form.google_place_id || form.osm_id || place.google_place_id || place.osm_id || null; } /** @@ -26,7 +30,7 @@ export function usePlaceRefresh({ form: PlaceFormData; language: string; }): { candidates: RefreshCandidate[]; origin: PlaceClientOrigin } { - const detailId = detailIdOf(place); + const detailId = detailIdOf(place, form); // Keyed by what was asked, so a stale answer for another place is never read. const [fresh, setFresh] = useState<{ key: string; details: Record } | null>(null); const key = detailId ? `${detailId}|${language}` : null;