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.3 KB
Raw Blame
// FE-PLANNER-CONTACT-001 to FE-PLANNER-CONTACT-004
import { describe, expect, it, vi } from 'vitest'
import { useState } from 'react'
import userEvent from '@testing-library/user-event'
import { render, screen } from '../../../tests/helpers/render'
import { PlaceContactFields } from './PlaceContactFields'
import type { PlaceOpeningHours } from '@trek/shared'

function Harness({ onChange, start = '', suggested }: { onChange: (field: string, value: string) => void; start?: string; suggested?: PlaceOpeningHours | null }) {
  const [state, setState] = useState({ phone: '', email: '', opening_hours: start })
  return (
    <PlaceContactFields phone={state.phone} email={state.email} openingHours={state.opening_hours} suggestedHours={suggested}
      onChange={(field, value) => { onChange(field, value); setState(s => ({ ...s, [field]: value })) }} />
  )
}

describe('PlaceContactFields (#2472)', () => {
  it('FE-PLANNER-CONTACT-001: phone and e-mail are plain fields', async () => {
    const user = userEvent.setup()
    const onChange = vi.fn()
    render(<Harness onChange={onChange} />)
    await user.type(screen.getByLabelText('Phone'), '+1 555')
    await user.type(screen.getByLabelText('E-mail'), '[email protected]')
    expect(onChange).toHaveBeenLastCalledWith('email', '[email protected]')
    expect(screen.getByLabelText('Phone')).toHaveValue('+1 555')
  })

  it('FE-PLANNER-CONTACT-002: the hours unfold from one row, close a day, copy Monday, and fold away again', async () => {
    const user = userEvent.setup()
    const onChange = vi.fn()
    render(<Harness onChange={onChange} />)
    expect(screen.queryByRole('switch', { name: 'Monday: Closed' })).not.toBeInTheDocument()
    await user.click(screen.getByRole('button', { name: 'Add opening hours' }))

    await user.click(screen.getByRole('switch', { name: 'Monday: Closed' }))
    let stored = JSON.parse(onChange.mock.lastCall![1])
    expect(stored[0]).toEqual({ closed: true })

    await user.click(screen.getByRole('button', { name: "Use Monday's hours for every day" }))
    stored = JSON.parse(onChange.mock.lastCall![1])
    expect(stored.every((d: { closed: boolean }) => d.closed)).toBe(true)
    expect(screen.getAllByText('Closed').length).toBeGreaterThanOrEqual(7)

    await user.click(screen.getByRole('button', { name: 'Remove opening hours' }))
    expect(onChange).toHaveBeenLastCalledWith('opening_hours', '')
    expect(screen.getByRole('button', { name: 'Add opening hours' })).toBeInTheDocument()
  })

  it('FE-PLANNER-CONTACT-003: a time can be typed key by key without the field resetting', async () => {
    const user = userEvent.setup()
    const onChange = vi.fn()
    render(<Harness onChange={onChange} />)
    await user.click(screen.getByRole('button', { name: 'Add opening hours' }))
    const opens = screen.getAllByRole('textbox', { name: /Monday opens/ })
    await user.type(opens[0], '0930')
    expect(opens[0]).toHaveValue('09:30')
    expect(JSON.parse(onChange.mock.lastCall?.[1])[0]).toEqual({ closed: false, open: '09:30' })
  })

  it('FE-PLANNER-CONTACT-004: looked-up hours fill an empty place, and an edited one offers to take them over', async () => {
    const user = userEvent.setup()
    const looked: PlaceOpeningHours = [{ closed: false, open: '10:00', close: '16:00' }, ...Array.from({ length: 6 }, () => ({ closed: true }))]
    const onChange = vi.fn()
    const { unmount } = render(<Harness onChange={onChange} suggested={looked} />)
    expect(JSON.parse(onChange.mock.lastCall![1])).toEqual(looked)
    expect(screen.queryByRole('button', { name: 'Take over from the place details' })).toBeNull()
    unmount()

    onChange.mockClear()
    const own = JSON.stringify([{ closed: false, open: '08:00', close: '09:00' }, ...Array.from({ length: 6 }, () => ({ closed: false }))])
    render(<Harness onChange={onChange} start={own} suggested={looked} />)
    expect(onChange).not.toHaveBeenCalled()
    await user.click(screen.getByRole('button', { name: 'Take over from the place details' }))
    expect(JSON.parse(onChange.mock.lastCall![1])).toEqual(looked)
  })

  it('FE-PLANNER-CONTACT-005: the place\'s phone and e-mail keep password managers out', () => {
    render(<Harness onChange={vi.fn()} />)
    // A place's address is not the user's login; 1Password filled it in (5.0.0-pre.1 feedback).
    for (const label of ['Phone', 'E-mail']) {
      const field = screen.getByLabelText(label)
      expect(field).toHaveAttribute('data-1p-ignore', 'true')
      expect(field).toHaveAttribute('data-lpignore', 'true')
      expect(field).toHaveAttribute('data-bwignore', 'true')
      expect(field).toHaveAttribute('autocomplete', 'off')
    }
    // Still an e-mail field, so the keyboard and the browser's check stay.
    expect(screen.getByLabelText('E-mail')).toHaveAttribute('type', 'email')
  })

  it('FE-PLANNER-CONTACT-003: a day opens around the clock with one switch, and an all-day lookup shows it on', async () => {
    const user = userEvent.setup()
    const onChange = vi.fn()
    render(<Harness onChange={onChange} />)
    await user.click(screen.getByRole('button', { name: 'Add opening hours' }))

    const allDay = screen.getByRole('switch', { name: 'Monday: Open 24 hours' })
    expect(allDay).toHaveAttribute('aria-checked', 'false')
    await user.click(allDay)
    expect(JSON.parse(onChange.mock.lastCall?.[1])[0]).toEqual({ closed: false, open: '00:00', close: '23:59' })
    expect(allDay).toHaveAttribute('aria-checked', 'true')
    // The two time fields give way to the word.
    expect(screen.queryByRole('textbox', { name: /Monday opens/ })).not.toBeInTheDocument()
    expect(screen.getByText('Open 24 hours', { selector: 'span' })).toBeInTheDocument()

    // Closing the day turns the all-day switch off.
    await user.click(screen.getByRole('switch', { name: 'Monday: Closed' }))
    expect(JSON.parse(onChange.mock.lastCall?.[1])[0]).toEqual({ closed: true })
    expect(allDay).toHaveAttribute('aria-checked', 'false')
  })

  it('FE-PLANNER-CONTACT-004: looked-up hours open around the clock arrive with the all-day switch on', () => {
    const suggested = Array.from({ length: 7 }, () => ({ closed: false, open: '00:00', close: '23:59' }))
    render(<Harness onChange={vi.fn()} suggested={suggested} />)
    for (const day of ['Monday', 'Sunday']) {
      expect(screen.getByRole('switch', { name: `${day}: Open 24 hours` })).toHaveAttribute('aria-checked', 'true')
    }
  })
})