diff --git a/client/src/components/Planner/PlaceFormModal.collectionPick.test.tsx b/client/src/components/Planner/PlaceFormModal.collectionPick.test.tsx new file mode 100644 index 000000000..e8717c447 --- /dev/null +++ b/client/src/components/Planner/PlaceFormModal.collectionPick.test.tsx @@ -0,0 +1,59 @@ +// FE-PLANNER-PLACEFORM-125: a place picked from a collection inside a trip (#3074). +import { placeFormProps, seedPlaceFormStores } from '../../../tests/helpers/placeFormSetup'; +import { fireEvent, render, screen, waitFor } from '../../../tests/helpers/render'; +import { resetAllStores, seedStore } from '../../../tests/helpers/store'; +import { collectionsApi } from '../../api/collections'; +import { useAddonStore } from '../../store/addonStore'; +import PlaceFormModal from './PlaceFormModal'; + +vi.mock('../shared/CustomTimePicker', async () => ({ + default: (await import('../../../tests/helpers/placeForm')).TimePickerStub, +})); + +beforeEach(() => { + resetAllStores(); + seedPlaceFormStores(); +}); + +afterEach(() => { + vi.restoreAllMocks(); +}); + +describe('PlaceFormModal with a place from a collection', () => { + it('FE-PLANNER-PLACEFORM-125: a place picked from a collection keeps the category it was saved under (#3074)', async () => { + seedStore(useAddonStore, { + addons: [{ id: 'collections', name: 'Collections', type: 'collections', icon: '', enabled: true }], + loaded: true, + }); + vi.spyOn(collectionsApi, 'list').mockResolvedValue({ + collections: [{ id: 1, owner_id: 1, name: 'Tokyo 2026', color: '#ec4899' }], + incomingInvites: [], + }); + vi.spyOn(collectionsApi, 'get').mockResolvedValue({ + collection: { id: 1, owner_id: 1, name: 'Tokyo 2026', color: '#ec4899' }, + places: [ + { + id: 10, + collection_id: 1, + name: 'Zebra Cafe', + status: 'idea', + lat: 35.66, + lng: 139.7, + category_id: 7, + category: { id: 7, name: 'Must see', color: '#f59e0b', icon: 'Star' }, + }, + ], + }); + const onSave = vi.fn().mockResolvedValue(undefined); + // A cafe saved as "Must see": the guess from its kind would be the cafe category. + const categories = [ + { id: 3, name: 'Cafe', color: '#7c3aed', icon: 'Coffee' }, + { id: 7, name: 'Must see', color: '#f59e0b', icon: 'Star' }, + ]; + render(); + fireEvent.click(await screen.findByText('Zebra Cafe')); + fireEvent.click(screen.getByRole('button', { name: /^Add$/i })); + await waitFor(() => expect(onSave).toHaveBeenCalled()); + expect(onSave).toHaveBeenCalledWith(expect.objectContaining({ name: 'Zebra Cafe', category_id: '7' })); + }); +}); diff --git a/client/src/components/Planner/PlaceFormModal.test.tsx b/client/src/components/Planner/PlaceFormModal.test.tsx index 368c38965..a9669091f 100644 --- a/client/src/components/Planner/PlaceFormModal.test.tsx +++ b/client/src/components/Planner/PlaceFormModal.test.tsx @@ -2378,36 +2378,6 @@ describe('PlaceFormModal on the dialog frame', () => { expect(onClose).not.toHaveBeenCalled(); }); - it('FE-PLANNER-PLACEFORM-125: a place picked from a collection keeps the category it was saved under (#3074)', async () => { - withCollections(); - vi.spyOn(collectionsApi, 'get').mockResolvedValue({ - collection: { id: 1, owner_id: 1, name: 'Tokyo 2026', color: '#ec4899' }, - places: [ - { - id: 10, - collection_id: 1, - name: 'Zebra Cafe', - status: 'idea', - lat: 35.66, - lng: 139.7, - category_id: 7, - category: { id: 7, name: 'Must see', color: '#f59e0b', icon: 'Star' }, - }, - ], - }); - const onSave = vi.fn().mockResolvedValue(undefined); - // A cafe saved as "Must see": the guess from its kind would be the cafe category. - const categories = [ - { id: 3, name: 'Cafe', color: '#7c3aed', icon: 'Coffee' }, - { id: 7, name: 'Must see', color: '#f59e0b', icon: 'Star' }, - ]; - render(); - fireEvent.click(await screen.findByText('Zebra Cafe')); - fireEvent.click(screen.getByRole('button', { name: /^Add$/i })); - await waitFor(() => expect(onSave).toHaveBeenCalled()); - expect(onSave).toHaveBeenCalledWith(expect.objectContaining({ name: 'Zebra Cafe', category_id: '7' })); - }); - it('FE-PLANNER-PLACEFORM-099: Escape with nothing open closes the dialog', () => { const onClose = vi.fn(); render(); diff --git a/client/src/components/Planner/PlaceFormModal.tsx b/client/src/components/Planner/PlaceFormModal.tsx index c21349e78..9e14521b0 100644 --- a/client/src/components/Planner/PlaceFormModal.tsx +++ b/client/src/components/Planner/PlaceFormModal.tsx @@ -135,9 +135,10 @@ function usePlaceFormModal(props: PlaceFormModalProps) { // the next pick may replace it and a hand choice is never overwritten (#2282). const categoryGuessedRef = useRef(false) const guessedCategory = (result: Record): string => { - const id = guessCategoryId(result, categories || []) + const saved = savedCategoryId(result, categories || []) // a collection's place keeps its own (#3074) + const id = saved ? null : guessCategoryId(result, categories || []) categoryGuessedRef.current = id != null - return id != null ? String(id) : '' + return saved ?? (id != null ? String(id) : '') } // The hours the details column looked up, as a week the contact block can take over (#2472). const [detailsHours, setDetailsHours] = useState(null) @@ -233,12 +234,6 @@ function usePlaceFormModal(props: PlaceFormModalProps) { setForm(prev => { const merged = mergeResult(prev, result, autoFilledRef.current) if (prev.category_id && !categoryGuessedRef.current) return merged - // A place from a collection brings the category it was saved under (#3074). - const saved = savedCategoryId(result, categories || []) - if (saved) { - categoryGuessedRef.current = false - return { ...merged, category_id: saved } - } return { ...merged, category_id: guessedCategory(result) } }) setPickedSource(label)