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.

6.9 KB
Raw Blame
// FE-COMP-JOURNEYDATEPICKER-001 to FE-COMP-JOURNEYDATEPICKER-008
import React from 'react'
import { describe, it, expect, vi } from 'vitest'
import { render, screen } from '../../../tests/helpers/render'
import userEvent from '@testing-library/user-event'
import { DatePicker } from './JourneyDetailPageDatePicker'
import { useSettingsStore } from '../../store/settingsStore'

function open(value = '2026-09-11') {
  const onChange = vi.fn()
  render(<DatePicker value={value} onChange={onChange} />)
  return { onChange }
}

const header = () => screen.getByRole('button', { name: /select month|select year|\d{4} – \d{4}/i })
const prev = (name: RegExp) => screen.getByRole('button', { name })

describe('DatePicker year navigation (#2318)', () => {
  it('FE-COMP-JOURNEYDATEPICKER-001: opens on the month of the value with weekday headers', async () => {
    const user = userEvent.setup()
    open()
    await user.click(screen.getByText(/Sep 11, 2026/))
    expect(screen.getByText('Mon')).toBeInTheDocument()
    expect(screen.getByRole('button', { name: 'Select month' })).toHaveTextContent('September 2026')
  })

  it('FE-COMP-JOURNEYDATEPICKER-002: the header climbs to the months, then to the years', async () => {
    const user = userEvent.setup()
    open()
    await user.click(screen.getByText(/Sep 11, 2026/))

    await user.click(screen.getByRole('button', { name: 'Select month' }))
    expect(screen.queryByText('Mon')).not.toBeInTheDocument()
    expect(screen.getByRole('button', { name: 'Jan' })).toBeInTheDocument()
    expect(screen.getByRole('button', { name: 'Select year' })).toHaveTextContent('2026')

    await user.click(screen.getByRole('button', { name: 'Select year' }))
    expect(screen.getByRole('button', { name: '2016' })).toBeInTheDocument()
    expect(screen.getByRole('button', { name: '2027' })).toBeInTheDocument()
    expect(header()).toHaveTextContent('2016 – 2027')
  })

  it('FE-COMP-JOURNEYDATEPICKER-003: picking a year three back lands on that year in one move', async () => {
    const user = userEvent.setup()
    open()
    await user.click(screen.getByText(/Sep 11, 2026/))
    await user.click(screen.getByRole('button', { name: 'Select month' }))
    await user.click(screen.getByRole('button', { name: 'Select year' }))
    await user.click(screen.getByRole('button', { name: '2023' }))

    // Back on the months, of the chosen year.
    expect(screen.getByRole('button', { name: 'Select year' })).toHaveTextContent('2023')
    await user.click(screen.getByRole('button', { name: 'Mar' }))
    expect(screen.getByRole('button', { name: 'Select month' })).toHaveTextContent('March 2023')
    expect(screen.getByText('Mon')).toBeInTheDocument()
  })

  it('FE-COMP-JOURNEYDATEPICKER-004: the arrows step the level they are on', async () => {
    const user = userEvent.setup()
    open()
    await user.click(screen.getByText(/Sep 11, 2026/))

    await user.click(prev(/previous month/i))
    expect(screen.getByRole('button', { name: 'Select month' })).toHaveTextContent('August 2026')

    await user.click(screen.getByRole('button', { name: 'Select month' }))
    await user.click(prev(/previous year/i))
    expect(screen.getByRole('button', { name: 'Select year' })).toHaveTextContent('2025')

    await user.click(screen.getByRole('button', { name: 'Select year' }))
    await user.click(prev(/previous years/i))
    expect(header()).toHaveTextContent('2004 – 2015')
    await user.click(screen.getByRole('button', { name: /next years/i }))
    expect(header()).toHaveTextContent('2016 – 2027')
  })

  it('FE-COMP-JOURNEYDATEPICKER-005: a day picked after the jump is handed back as ISO', async () => {
    const user = userEvent.setup()
    const { onChange } = open()
    await user.click(screen.getByText(/Sep 11, 2026/))
    await user.click(screen.getByRole('button', { name: 'Select month' }))
    await user.click(screen.getByRole('button', { name: 'Select year' }))
    await user.click(screen.getByRole('button', { name: '2023' }))
    await user.click(screen.getByRole('button', { name: 'Mar' }))
    await user.click(screen.getByRole('button', { name: '15' }))
    expect(onChange).toHaveBeenCalledWith('2023-03-15')
    expect(screen.queryByText('Mon')).not.toBeInTheDocument()
  })

  it('FE-COMP-JOURNEYDATEPICKER-006: reopening starts on the days again', async () => {
    const user = userEvent.setup()
    open()
    const trigger = screen.getByText(/Sep 11, 2026/)
    await user.click(trigger)
    await user.click(screen.getByRole('button', { name: 'Select month' }))
    await user.click(screen.getByRole('button', { name: 'Select year' }))
    await user.click(trigger)
    expect(screen.queryByRole('button', { name: '2023' })).not.toBeInTheDocument()
    await user.click(trigger)
    expect(screen.getByText('Mon')).toBeInTheDocument()
  })
})

describe('DatePicker week start (#2029)', () => {
  it('FE-COMP-JOURNEYDATEPICKER-007: follows the first weekday the user picked, like the other date pickers', async () => {
    const original = useSettingsStore.getState().settings
    useSettingsStore.setState({ settings: { ...original, week_start: 'sunday' } })
    try {
      const user = userEvent.setup()
      render(<DatePicker value="2026-03-15" onChange={vi.fn()} />)
      await user.click(screen.getByText(/Mar 15, 2026/))
      const header = screen.getByText('Sun').parentElement!
      expect(Array.from(header.children).map(c => c.textContent)).toEqual(['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'])
      // 1 March 2026 is a Sunday, so it opens the first row without a blank before it.
      const day1 = screen.getByRole('button', { name: '1' })
      expect(Array.from(day1.parentElement!.children).indexOf(day1)).toBe(0)
    } finally {
      useSettingsStore.setState({ settings: original })
    }
  })
})

describe('DatePicker inside a clipping dialog (#2858)', () => {
  it('FE-COMP-JOURNEYDATEPICKER-008: the calendar floats over the page, stays inside the viewport and closes on a click away', async () => {
    const user = userEvent.setup()
    const { container } = render(
      <div style={{ overflow: 'hidden' }}>
        <DatePicker value="2026-03-15" onChange={vi.fn()} />
      </div>,
    )
    const trigger = screen.getByText(/Mar 15, 2026/).closest('button') as HTMLButtonElement
    // A trigger at the right edge, where the end date of a range sits.
    trigger.getBoundingClientRect = () => ({ top: 100, bottom: 136, left: window.innerWidth - 120, right: window.innerWidth - 8, width: 112, height: 36, x: 0, y: 0, toJSON: () => ({}) })
    await user.click(trigger)

    const day31 = screen.getByRole('button', { name: '31' })
    // Rendered outside the dialog that would clip it.
    expect(container.contains(day31)).toBe(false)
    const panel = day31.closest('.fixed') as HTMLElement
    expect(panel.style.top).toBe('140px')
    expect(parseFloat(panel.style.left)).toBe(window.innerWidth - 280 - 8)

    await user.click(document.querySelector('[role="presentation"]') as HTMLElement)
    expect(screen.queryByRole('button', { name: '31' })).toBeNull()
  })
})