mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 07:07:31 +00:00
test(components): cover map, collections, settings, journey and admin
This commit is contained in:
@@ -0,0 +1,282 @@
|
||||
// FE-COMP-SAVETOCOL-001 to FE-COMP-SAVETOCOL-016
|
||||
import React from 'react';
|
||||
import { afterEach, beforeEach } from 'vitest';
|
||||
import { render, screen, fireEvent, waitFor, within } from '../../../tests/helpers/render';
|
||||
import type { Collection, CollectionListResponse, CollectionMembership, CollectionSaveResult } from '@trek/shared';
|
||||
import { collectionsApi } from '../../api/collections';
|
||||
import { useSaveToCollectionStore } from '../../store/saveToCollectionStore';
|
||||
import type { SaveToCollectionTarget } from '../../store/saveToCollectionStore';
|
||||
import SaveToCollectionModal from './SaveToCollectionModal';
|
||||
|
||||
const mockNavigate = vi.fn();
|
||||
vi.mock('react-router-dom', async () => {
|
||||
const actual = await vi.importActual('react-router-dom');
|
||||
return { ...actual, useNavigate: () => mockNavigate };
|
||||
});
|
||||
|
||||
function list(over: Partial<Collection>): Collection {
|
||||
return { id: 1, owner_id: 7, name: 'List', color: null, place_count: 0, ...over } as Collection;
|
||||
}
|
||||
|
||||
const FAVORITES = list({ id: 1, name: 'Favorites', color: '#ef4444', place_count: 4 });
|
||||
const WISHLIST = list({ id: 2, name: 'Wishlist' });
|
||||
const SHARED = list({ id: 3, name: 'Team ideas', is_owner: false });
|
||||
|
||||
const listResponse = (collections: Collection[]): CollectionListResponse => ({ collections, incomingInvites: [] });
|
||||
const EMPTY_MEMBERSHIP: CollectionMembership = { saved: false, lists: [] };
|
||||
|
||||
const TARGET: SaveToCollectionTarget = {
|
||||
name: 'Colosseum',
|
||||
source_trip_id: 5,
|
||||
source_place_id: 42,
|
||||
description: 'Ancient arena',
|
||||
lat: 41.89,
|
||||
lng: 12.49,
|
||||
address: 'Rome',
|
||||
category_id: 3,
|
||||
price: 16,
|
||||
currency: 'EUR',
|
||||
notes: 'Book ahead',
|
||||
image_url: '/uploads/places/colosseum.jpg',
|
||||
google_place_id: 'gp-1',
|
||||
google_ftid: 'ft-1',
|
||||
osm_id: 'osm-1',
|
||||
website: 'https://colosseo.example',
|
||||
phone: '+39',
|
||||
};
|
||||
|
||||
let addToast: ReturnType<typeof vi.fn>;
|
||||
|
||||
function openFor(target: SaveToCollectionTarget = TARGET) {
|
||||
useSaveToCollectionStore.setState({ target, version: 0 });
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
addToast = vi.fn();
|
||||
window.__addToast = addToast as unknown as typeof window.__addToast;
|
||||
mockNavigate.mockClear();
|
||||
useSaveToCollectionStore.setState({ target: null, version: 0 });
|
||||
vi.spyOn(collectionsApi, 'list').mockResolvedValue(listResponse([FAVORITES, WISHLIST]));
|
||||
vi.spyOn(collectionsApi, 'membership').mockResolvedValue(EMPTY_MEMBERSHIP);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
useSaveToCollectionStore.setState({ target: null, version: 0 });
|
||||
delete window.__addToast;
|
||||
});
|
||||
|
||||
describe('SaveToCollectionModal', () => {
|
||||
it('FE-COMP-SAVETOCOL-001: renders nothing and calls no api while no target is set', () => {
|
||||
render(<SaveToCollectionModal />);
|
||||
expect(screen.queryByRole('heading', { name: 'Save to list' })).not.toBeInTheDocument();
|
||||
expect(collectionsApi.list).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-COMP-SAVETOCOL-002: shows the target name plus every list once loaded', async () => {
|
||||
openFor();
|
||||
render(<SaveToCollectionModal />);
|
||||
expect(await screen.findByText('Favorites')).toBeInTheDocument();
|
||||
expect(screen.getByRole('heading', { name: 'Save to list' })).toBeInTheDocument();
|
||||
expect(screen.getByText('Colosseum')).toBeInTheDocument();
|
||||
expect(screen.getByText('Wishlist')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-COMP-SAVETOCOL-003: queries membership with the maps identity of the target', async () => {
|
||||
openFor();
|
||||
render(<SaveToCollectionModal />);
|
||||
await screen.findByText('Favorites');
|
||||
expect(collectionsApi.membership).toHaveBeenCalledWith({
|
||||
google_place_id: 'gp-1',
|
||||
google_ftid: 'ft-1',
|
||||
name: 'Colosseum',
|
||||
lat: 41.89,
|
||||
lng: 12.49,
|
||||
});
|
||||
});
|
||||
|
||||
it('FE-COMP-SAVETOCOL-004: shows the spinner until the lists arrive', async () => {
|
||||
let resolve!: (v: CollectionListResponse) => void;
|
||||
vi.spyOn(collectionsApi, 'list').mockReturnValue(new Promise(r => { resolve = r; }));
|
||||
openFor();
|
||||
render(<SaveToCollectionModal />);
|
||||
expect(document.querySelector('.animate-spin')).not.toBeNull();
|
||||
|
||||
resolve(listResponse([FAVORITES]));
|
||||
expect(await screen.findByText('Favorites')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-COMP-SAVETOCOL-005: a failing list request falls back to the empty state with a create shortcut', async () => {
|
||||
vi.spyOn(collectionsApi, 'list').mockRejectedValue(new Error('offline'));
|
||||
openFor();
|
||||
render(<SaveToCollectionModal />);
|
||||
|
||||
expect(await screen.findByText('Create a list first to save places.')).toBeInTheDocument();
|
||||
fireEvent.click(screen.getByRole('button', { name: /New list/ }));
|
||||
expect(mockNavigate).toHaveBeenCalledWith('/collections');
|
||||
expect(useSaveToCollectionStore.getState().target).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-COMP-SAVETOCOL-006: lists already holding the place are marked saved', async () => {
|
||||
vi.spyOn(collectionsApi, 'membership').mockResolvedValue({
|
||||
saved: true,
|
||||
lists: [{ collection_id: 1, name: 'Favorites', place_id: 900 }],
|
||||
});
|
||||
openFor();
|
||||
render(<SaveToCollectionModal />);
|
||||
|
||||
const favorites = await screen.findByRole('button', { name: /Favorites/ });
|
||||
expect(favorites).toHaveClass('border-accent');
|
||||
expect(favorites.querySelector('.lucide-bookmark-check')).not.toBeNull();
|
||||
expect(screen.getByRole('button', { name: /Wishlist/ })).not.toHaveClass('border-accent');
|
||||
});
|
||||
|
||||
it('FE-COMP-SAVETOCOL-007: a failing membership lookup degrades to "saved nowhere"', async () => {
|
||||
vi.spyOn(collectionsApi, 'membership').mockRejectedValue(new Error('offline'));
|
||||
openFor();
|
||||
render(<SaveToCollectionModal />);
|
||||
|
||||
const favorites = await screen.findByRole('button', { name: /Favorites/ });
|
||||
expect(favorites).not.toHaveClass('border-accent');
|
||||
});
|
||||
|
||||
it('FE-COMP-SAVETOCOL-008: a shared list carries the SHARED badge', async () => {
|
||||
vi.spyOn(collectionsApi, 'list').mockResolvedValue(listResponse([FAVORITES, SHARED]));
|
||||
openFor();
|
||||
render(<SaveToCollectionModal />);
|
||||
|
||||
const shared = await screen.findByRole('button', { name: /Team ideas/ });
|
||||
expect(within(shared).getByText('Shared')).toBeInTheDocument();
|
||||
expect(within(screen.getByRole('button', { name: /Favorites/ })).queryByText('Shared')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-COMP-SAVETOCOL-009: picking an unsaved list forwards the whole target payload and bumps the version', async () => {
|
||||
const save = vi.spyOn(collectionsApi, 'savePlace').mockResolvedValue({});
|
||||
openFor();
|
||||
render(<SaveToCollectionModal />);
|
||||
|
||||
fireEvent.click(await screen.findByRole('button', { name: /Wishlist/ }));
|
||||
|
||||
await waitFor(() => expect(addToast).toHaveBeenCalledWith('Added to Wishlist', 'success', undefined));
|
||||
expect(save).toHaveBeenCalledWith(expect.objectContaining({
|
||||
collection_id: 2,
|
||||
source_trip_id: 5,
|
||||
source_place_id: 42,
|
||||
name: 'Colosseum',
|
||||
lat: 41.89,
|
||||
lng: 12.49,
|
||||
google_place_id: 'gp-1',
|
||||
osm_id: 'osm-1',
|
||||
website: 'https://colosseo.example',
|
||||
force: true,
|
||||
}));
|
||||
expect(useSaveToCollectionStore.getState().version).toBe(1);
|
||||
});
|
||||
|
||||
it('FE-COMP-SAVETOCOL-010: a sparse target sends explicit nulls rather than undefined', async () => {
|
||||
const save = vi.spyOn(collectionsApi, 'savePlace').mockResolvedValue({});
|
||||
openFor({ name: 'Nameless bar' });
|
||||
render(<SaveToCollectionModal />);
|
||||
|
||||
fireEvent.click(await screen.findByRole('button', { name: /Wishlist/ }));
|
||||
|
||||
await waitFor(() => expect(save).toHaveBeenCalled());
|
||||
expect(save).toHaveBeenCalledWith(expect.objectContaining({
|
||||
collection_id: 2,
|
||||
name: 'Nameless bar',
|
||||
lat: null,
|
||||
lng: null,
|
||||
source_trip_id: null,
|
||||
phone: null,
|
||||
}));
|
||||
});
|
||||
|
||||
it('FE-COMP-SAVETOCOL-011: picking a saved list removes that place instead', async () => {
|
||||
vi.spyOn(collectionsApi, 'membership').mockResolvedValue({
|
||||
saved: true,
|
||||
lists: [{ collection_id: 1, name: 'Favorites', place_id: 900 }],
|
||||
});
|
||||
const del = vi.spyOn(collectionsApi, 'deletePlace').mockResolvedValue({});
|
||||
const save = vi.spyOn(collectionsApi, 'savePlace');
|
||||
openFor();
|
||||
render(<SaveToCollectionModal />);
|
||||
|
||||
fireEvent.click(await screen.findByRole('button', { name: /Favorites/ }));
|
||||
|
||||
await waitFor(() => expect(del).toHaveBeenCalledWith(900));
|
||||
expect(save).not.toHaveBeenCalled();
|
||||
expect(addToast).toHaveBeenCalledWith('Removed from Favorites', 'success', undefined);
|
||||
});
|
||||
|
||||
it('FE-COMP-SAVETOCOL-012: a failing save surfaces the server message and leaves the version alone', async () => {
|
||||
vi.spyOn(collectionsApi, 'savePlace').mockRejectedValue({ response: { data: { error: 'List is full' } } });
|
||||
openFor();
|
||||
render(<SaveToCollectionModal />);
|
||||
|
||||
fireEvent.click(await screen.findByRole('button', { name: /Wishlist/ }));
|
||||
|
||||
await waitFor(() => expect(addToast).toHaveBeenCalledWith('List is full', 'error', undefined));
|
||||
expect(useSaveToCollectionStore.getState().version).toBe(0);
|
||||
});
|
||||
|
||||
it('FE-COMP-SAVETOCOL-013: a second click on the busy row is ignored while the save runs', async () => {
|
||||
let resolve!: (v: CollectionSaveResult) => void;
|
||||
const save = vi.spyOn(collectionsApi, 'savePlace').mockReturnValue(new Promise(r => { resolve = r; }));
|
||||
openFor();
|
||||
render(<SaveToCollectionModal />);
|
||||
|
||||
const wishlist = await screen.findByRole('button', { name: /Wishlist/ });
|
||||
fireEvent.click(wishlist);
|
||||
expect(wishlist).toBeDisabled();
|
||||
fireEvent.click(screen.getByRole('button', { name: /Favorites/ }));
|
||||
expect(save).toHaveBeenCalledTimes(1);
|
||||
|
||||
resolve({});
|
||||
await waitFor(() => expect(useSaveToCollectionStore.getState().version).toBe(1));
|
||||
});
|
||||
|
||||
it('FE-COMP-SAVETOCOL-015: a membership refresh that fails after a save falls back to "saved nowhere"', async () => {
|
||||
vi.spyOn(collectionsApi, 'membership')
|
||||
.mockResolvedValueOnce(EMPTY_MEMBERSHIP)
|
||||
.mockRejectedValue(new Error('offline'));
|
||||
vi.spyOn(collectionsApi, 'savePlace').mockResolvedValue({});
|
||||
openFor();
|
||||
render(<SaveToCollectionModal />);
|
||||
|
||||
fireEvent.click(await screen.findByRole('button', { name: /Wishlist/ }));
|
||||
|
||||
await waitFor(() => expect(useSaveToCollectionStore.getState().version).toBe(1));
|
||||
expect(screen.getByRole('button', { name: /Wishlist/ })).not.toHaveClass('border-accent');
|
||||
});
|
||||
|
||||
it('FE-COMP-SAVETOCOL-016: a response landing after the picker closed is dropped', async () => {
|
||||
let resolve!: (v: CollectionListResponse) => void;
|
||||
vi.spyOn(collectionsApi, 'list').mockReturnValue(new Promise(r => { resolve = r; }));
|
||||
openFor();
|
||||
const { unmount } = render(<SaveToCollectionModal />);
|
||||
unmount();
|
||||
|
||||
resolve(listResponse([FAVORITES]));
|
||||
await Promise.resolve();
|
||||
expect(screen.queryByText('Favorites')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-COMP-SAVETOCOL-014: the footer navigates to the collections page or just closes', async () => {
|
||||
openFor();
|
||||
const { unmount } = render(<SaveToCollectionModal />);
|
||||
await screen.findByText('Favorites');
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'View' }));
|
||||
expect(mockNavigate).toHaveBeenCalledWith('/collections');
|
||||
expect(useSaveToCollectionStore.getState().target).toBeNull();
|
||||
unmount();
|
||||
|
||||
mockNavigate.mockClear();
|
||||
openFor();
|
||||
render(<SaveToCollectionModal />);
|
||||
await screen.findByText('Favorites');
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Close' }));
|
||||
expect(mockNavigate).not.toHaveBeenCalled();
|
||||
expect(useSaveToCollectionStore.getState().target).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user