mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
91bf941e4c
All lists in the place dialog's picker now shows a place kept in several lists once, as All saved does, and All places keeps the places of the trip being planned, so it always holds at least what All lists holds and its count matches the rail.
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 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', 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([]);
|
|
});
|
|
});
|