mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
101c4b5d7a
The picker beside the add place form only offered the places of the lists. Its list filter now also offers All places: the places of the other trips beside the lists', each place once, read when chosen, with the same search, status filter and distance order. The trip being planned stays out, it has those places already. A pick fills the form with the place's name, address, position and category.
70 lines
3.1 KiB
TypeScript
70 lines
3.1 KiB
TypeScript
// FE-COMP-PICKERLIB-001 to FE-COMP-PICKERLIB-004: All places as a source of the saved-place picker.
|
|
import { renderHook, waitFor } from '@testing-library/react';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import { libraryEntry, libraryPage, listOrigin, tripOrigin } from '../../../tests/helpers/placesLibrary';
|
|
import { placesLibraryRepo } from '../../repo/placesLibraryRepo';
|
|
import { pickedFromLibrary, pickerPlaceOf, usePickerLibrary } from './usePickerLibrary';
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe('pickerPlaceOf', () => {
|
|
it('FE-COMP-PICKERLIB-001: names the picture by the first use and keeps the trips and the list statuses', () => {
|
|
const entry = libraryEntry({
|
|
key: 'trip:70',
|
|
category: { id: 3, name: 'Bar', color: null, icon: null },
|
|
origins: [
|
|
tripOrigin({ trip_id: 5, place_id: 70 }),
|
|
listOrigin({ collection_place_id: 80, status: 'visited' }),
|
|
tripOrigin({ trip_id: 6, place_id: 71 }),
|
|
],
|
|
});
|
|
const place = pickerPlaceOf(entry);
|
|
expect(place).toMatchObject({ key: 'trip:70', id: 70, name: 'Colosseum', tripIds: [5, 6], statuses: ['visited'] });
|
|
expect(pickerPlaceOf(libraryEntry({ origins: [listOrigin({ collection_place_id: 80 })] })).id).toBe(80);
|
|
expect(pickedFromLibrary(place)).toEqual({
|
|
name: 'Colosseum',
|
|
address: 'Piazza del Colosseo',
|
|
lat: 41.89,
|
|
lng: 12.49,
|
|
category_id: 3,
|
|
});
|
|
expect(pickedFromLibrary({ ...place, category: null }).category_id).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('usePickerLibrary', () => {
|
|
it('FE-COMP-PICKERLIB-002: reads nothing until it is asked for', () => {
|
|
const list = vi.spyOn(placesLibraryRepo, 'list');
|
|
const { result } = renderHook(() => usePickerLibrary(false));
|
|
expect(result.current).toEqual({ places: [], loading: false, loaded: false });
|
|
expect(list).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('FE-COMP-PICKERLIB-003: reads every page once, then keeps them', async () => {
|
|
const list = vi
|
|
.spyOn(placesLibraryRepo, 'list')
|
|
.mockResolvedValueOnce(libraryPage([libraryEntry({ key: 'trip:1', name: 'One' })], { next_cursor: 'c1' }))
|
|
.mockResolvedValueOnce(libraryPage([libraryEntry({ key: 'trip:2', name: 'Two' })]));
|
|
const { result, rerender } = renderHook(({ on }) => usePickerLibrary(on), { initialProps: { on: true } });
|
|
expect(result.current.loading).toBe(true);
|
|
await waitFor(() => expect(result.current.loaded).toBe(true));
|
|
expect(result.current.places.map((p) => p.name)).toEqual(['One', 'Two']);
|
|
expect(list).toHaveBeenNthCalledWith(2, expect.objectContaining({ cursor: 'c1' }), expect.any(AbortSignal));
|
|
|
|
rerender({ on: false });
|
|
rerender({ on: true });
|
|
expect(result.current.loading).toBe(false);
|
|
expect(list).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it('FE-COMP-PICKERLIB-004: a failed read ends the loading with nothing to show', async () => {
|
|
vi.spyOn(placesLibraryRepo, 'list').mockRejectedValue(new Error('offline'));
|
|
const { result } = renderHook(() => usePickerLibrary(true));
|
|
await waitFor(() => expect(result.current.loading).toBe(false));
|
|
expect(result.current.places).toEqual([]);
|
|
});
|
|
});
|