mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 21:11:14 +00:00
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.
This commit is contained in:
@@ -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<string, Record<string, unknown>> = {
|
||||
'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' });
|
||||
});
|
||||
});
|
||||
@@ -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<string, unknown> } | null>(null);
|
||||
const key = detailId ? `${detailId}|${language}` : null;
|
||||
|
||||
Reference in New Issue
Block a user