mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 21:11:14 +00:00
test(planner): the #3074 case in a file of its own, and the category pick inside guessedCategory, so both files stay within their size
This commit is contained in:
@@ -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(<PlaceFormModal {...placeFormProps} categories={categories} onSave={onSave} />);
|
||||
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' }));
|
||||
});
|
||||
});
|
||||
@@ -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(<PlaceFormModal {...defaultProps} categories={categories} onSave={onSave} />);
|
||||
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(<PlaceFormModal {...defaultProps} onClose={onClose} />);
|
||||
|
||||
@@ -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, unknown>): 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<PlaceOpeningHours | null>(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)
|
||||
|
||||
Reference in New Issue
Block a user