Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

8.2 KB
Raw Blame
// FE-COMP-MRESCARD-001 to FE-COMP-MRESCARD-010
import { fireEvent, render, screen } from '@testing-library/react';
import { Hotel, Plane } from 'lucide-react';
import { describe, expect, it, vi } from 'vitest';

import type { useReservationCard } from '../../../../components/Planner/bookings/useReservationCard';
import type { Reservation } from '../../../../types';
import {
  CardActions,
  CardDeleteSheet,
  CardFiles,
  CardNotes,
  CardTitleButton,
  CardWhenAndCode,
} from './MReservationCardParts';

const t = (key: string, params?: Record<string, string | number>) =>
  params ? `${key}:${Object.values(params).join(',')}` : key;

function reservation(overrides: Partial<Reservation> = {}): Reservation {
  return { id: 1, trip_id: 7, type: 'flight', title: 'LH 400', status: 'confirmed', ...overrides } as Reservation;
}

function cardState(overrides: Partial<ReturnType<typeof useReservationCard>> = {}) {
  return {
    codeBlurred: false,
    toggleCode: undefined,
    confirmingDelete: false,
    askDelete: vi.fn(),
    cancelDelete: vi.fn(),
    confirmDelete: vi.fn(),
    openFile: vi.fn(),
    downloadFile: vi.fn(),
    ...overrides,
  } as unknown as ReturnType<typeof useReservationCard>;
}

describe('MReservationCardParts', () => {
  it('FE-COMP-MRESCARD-001: the title button shows the type pill and title, and opens the card (transport variant)', () => {
    const onOpen = vi.fn();
    render(<CardTitleButton res={reservation()} TypeIcon={Plane} typeColor="#123456" onOpen={onOpen} t={t} />);

    const button = screen.getByRole('button');
    expect(button).toHaveTextContent('reservations.type.flight');
    expect(button).toHaveTextContent('LH 400');
    expect(screen.queryByText('reservations.needsReview')).toBeNull();
    expect(button.querySelector('.lucide-plane')).toHaveStyle({ color: '#123456' });
    fireEvent.click(button);
    expect(onOpen).toHaveBeenCalledOnce();
  });

  it('FE-COMP-MRESCARD-011: the title leads and may take two lines; the pills follow it and wrap under it', () => {
    render(<CardTitleButton res={reservation()} TypeIcon={Plane} typeColor="#123456" onOpen={vi.fn()} t={t} />);
    const title = screen.getByText('LH 400');
    expect(title).toHaveClass('line-clamp-2', 'flex-[1_1_auto]', 'wide:basis-full');
    expect(title).not.toHaveClass('truncate');
    const button = screen.getByRole('button');
    expect(button).toHaveClass('flex-wrap');
    expect(button.firstElementChild).toBe(title);
  });

  it('FE-COMP-MRESCARD-012: the time cell is never narrower than its value', () => {
    render(
      <CardWhenAndCode
        dayValue="Day 1"
        timeValue="09:05 – 10:15 Uhr"
        code={null}
        card={cardState()}
        onOpen={vi.fn()}
        t={t}
      />
    );
    const cell = screen.getByText('09:05 – 10:15 Uhr').parentElement as HTMLElement;
    expect(cell).toHaveClass('min-w-max');
    expect(screen.getByText('Day 1').parentElement).toHaveClass('min-w-0');
  });

  it('FE-COMP-MRESCARD-002: the title button flags a booking that needs review (booking variant)', () => {
    render(
      <CardTitleButton
        res={reservation({ type: 'hotel', title: 'Ryokan', needs_review: 1 } as Partial<Reservation>)}
        TypeIcon={Hotel}
        typeColor="#654321"
        onOpen={vi.fn()}
        t={t}
      />
    );

    expect(screen.getByText('reservations.type.hotel')).toBeInTheDocument();
    expect(screen.getByText('reservations.needsReview')).toBeInTheDocument();
  });

  it('FE-COMP-MRESCARD-003: the actions call edit and delete', () => {
    const onEdit = vi.fn();
    const onDelete = vi.fn();
    render(<CardActions onEdit={onEdit} onDelete={onDelete} t={t} />);

    fireEvent.click(screen.getByRole('button', { name: 'common.edit' }));
    fireEvent.click(screen.getByRole('button', { name: 'common.delete' }));
    expect(onEdit).toHaveBeenCalledOnce();
    expect(onDelete).toHaveBeenCalledOnce();
  });

  it('FE-COMP-MRESCARD-004: date and time open the card, and a missing code draws no code box', () => {
    const onOpen = vi.fn();
    render(<CardWhenAndCode dayValue="Mon 3" timeValue="09:00" code={null} card={cardState()} onOpen={onOpen} t={t} />);

    fireEvent.click(screen.getByRole('button'));
    expect(onOpen).toHaveBeenCalledOnce();
    expect(screen.getByText('Mon 3')).toBeInTheDocument();
    expect(screen.getByText('09:00')).toBeInTheDocument();
    expect(screen.queryByText('reservations.confirmationCode')).toBeNull();
  });

  it('FE-COMP-MRESCARD-005: a blurred code is its own reveal control', () => {
    const toggleCode = vi.fn();
    render(
      <CardWhenAndCode
        dayValue="Mon 3"
        timeValue="09:00"
        code="ABC123"
        card={cardState({ codeBlurred: true, toggleCode })}
        onOpen={vi.fn()}
        t={t}
      />
    );

    const reveal = screen.getByRole('button', { name: 'ABC123' });
    expect(reveal).toHaveAttribute('aria-pressed', 'false');
    fireEvent.click(reveal);
    expect(toggleCode).toHaveBeenCalledOnce();
  });

  it('FE-COMP-MRESCARD-006: each file row opens the file by its name and downloads it by the icon (#2585)', () => {
    const { container, rerender } = render(<CardFiles files={[]} card={cardState()} t={t} />);
    expect(container).toBeEmptyDOMElement();

    const files = [{ id: 5, original_name: 'ticket.pdf', url: '/uploads/f/5' }] as Parameters<
      typeof CardFiles
    >[0]['files'];
    const card = cardState();
    rerender(<CardFiles files={files} card={card} t={t} />);
    expect(screen.getByText('files.title')).toBeInTheDocument();

    const open = screen.getByRole('button', { name: 'ticket.pdf' });
    const download = screen.getByRole('button', { name: 'files.download: ticket.pdf' });
    // Two sibling buttons, never one inside the other.
    expect(open.tagName).toBe('BUTTON');
    expect(open.contains(download)).toBe(false);
    expect(download.closest('button')).toBe(download);

    fireEvent.click(open);
    expect(card.openFile).toHaveBeenCalledWith(files[0]);
    expect(card.downloadFile).not.toHaveBeenCalled();
    fireEvent.click(download);
    expect(card.downloadFile).toHaveBeenCalledWith(files[0]);
    expect(card.openFile).toHaveBeenCalledOnce();
  });

  it('FE-COMP-MRESCARD-007: the delete sheet names the reservation and confirms through the card', () => {
    const card = cardState({ confirmingDelete: true });
    render(<CardDeleteSheet title="LH 400" card={card} t={t} />);

    expect(screen.getByText('reservations.confirm.deleteBody:LH 400')).toBeInTheDocument();
    fireEvent.click(screen.getByRole('button', { name: 'common.delete' }));
    expect(card.confirmDelete).toHaveBeenCalledOnce();
  });

  it('FE-COMP-MRESCARD-009: a booking with a linked expense says the expense is deleted with it (#2604)', () => {
    render(
      <CardDeleteSheet title="LH 400" card={cardState({ confirmingDelete: true, deleteTakesExpense: true })} t={t} />
    );

    expect(screen.getByText('reservations.confirm.deleteBodyWithExpense:LH 400')).toBeInTheDocument();
  });

  it('FE-COMP-MRESCARD-008: the date sits beside the day title and never shrinks, a range stacks the time under it (#2590)', () => {
    const { container } = render(
      <CardWhenAndCode
        dayValue="Germany Departure Flight – Korea Arrival"
        dayDate="Oct 21 – Oct 22"
        dayRange
        timeValue="15:35 – 09:55"
        code={null}
        card={cardState()}
        onOpen={vi.fn()}
        t={t}
      />
    );

    const title = screen.getByText('Germany Departure Flight – Korea Arrival');
    const date = screen.getByText('Oct 21 – Oct 22');
    expect(title).toHaveClass('truncate');
    expect(date).toHaveClass('flex-none');
    expect(title.contains(date)).toBe(false);
    expect(container.querySelector('button > div')).toHaveClass('flex-col');
  });

  it('FE-COMP-MRESCARD-010: shows the notes of a booking, and nothing for empty ones (Discord, 15.10.2026)', () => {
    const notes = 'Gate closes 40 min early\nBag drop at counter C';
    const { container, rerender } = render(<CardNotes notes={notes} />);
    expect(container.querySelector('p')?.textContent).toBe(notes);

    rerender(<CardNotes notes="   " />);
    expect(container).toBeEmptyDOMElement();
    rerender(<CardNotes notes={null} />);
    expect(container).toBeEmptyDOMElement();
  });
});