mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 10:24:05 +00:00
a3f6635633
Tags on the place form are pills in their own colour, with a palette under the dot that recolours a tag for the whole trip (PUT /trips/:id/tags/:id was there already, the client now reaches it through repo and store) and a custom colour field of its own instead of the system picker. A new tag is made in a colour picked the same way. Description, notes and the note for one day's visit sit in one card under a switch, each with a line on where the text shows up; the day tab is named after its day and is only there with one visit in context. Nothing about the three fields changes underneath. Opening hours get a switch for open around the clock next to closed. Google writes a place open all week as one period on Sunday that never closes; it read as Sunday open and every other day closed, and now opens every day. The phone form follows the desktop: category as one field with a list, tags in one field, the texts switch, and phone, e-mail and opening hours, which it did not have. The hours and the text tabs are shared hooks (usePlaceHours, usePlaceTextTabs). Files and costs on the phone sheets are drawn as fields like the rest, and the paste hint, a keyboard shortcut a phone has no use for, is gone with its key.
170 lines
8.5 KiB
TypeScript
170 lines
8.5 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest'
|
|
import PlCategoryPicker from '../../../../src/mobile/screens/trip/sheets/PlCategoryPicker'
|
|
import type { Category } from '../../../../src/types'
|
|
import type { TripPlanner } from '../../../../src/mobile/screens/trip/MTripShell'
|
|
import { buildPlanner, buildTripActions } from '../../../helpers/mobileTrip'
|
|
import { fireEvent, render, screen, waitFor } from '../../../helpers/render'
|
|
|
|
// FE-MOB-PLCAT-001 to FE-MOB-PLCAT-011
|
|
// planner.t echoes the key, so every visible label is asserted as its key.
|
|
|
|
const CATEGORIES = [
|
|
{ id: 3, name: 'Food', color: '#ef4444', icon: 'Coffee', user_id: 1 },
|
|
{ id: 4, name: 'Museums', color: '#22c55e', icon: 'Landmark', user_id: 1 },
|
|
] as unknown as Category[]
|
|
|
|
function setup(plannerOverrides: Partial<TripPlanner> = {}, value = '') {
|
|
const onChange = vi.fn()
|
|
const planner = buildPlanner({ categories: CATEGORIES, ...plannerOverrides })
|
|
const view = render(<PlCategoryPicker planner={planner} value={value} onChange={onChange} />)
|
|
return { ...view, planner, onChange }
|
|
}
|
|
|
|
// The field names the category it holds; the list under it opens on a tap.
|
|
const field = () => screen.getByRole('button', { name: /^places.formCategory:/ })
|
|
const openList = () => fireEvent.click(field())
|
|
|
|
describe('PlCategoryPicker', () => {
|
|
it('FE-MOB-PLCAT-001: one field names the category; the list under it offers none, every category and a new one', () => {
|
|
setup()
|
|
expect(field()).toHaveAccessibleName('places.formCategory: places.noCategory')
|
|
expect(field()).toHaveAttribute('aria-expanded', 'false')
|
|
expect(screen.queryByRole('button', { name: /Food/ })).not.toBeInTheDocument()
|
|
openList()
|
|
expect(field()).toHaveAttribute('aria-expanded', 'true')
|
|
expect(screen.getByRole('button', { name: /^places.noCategory$/ })).toBeInTheDocument()
|
|
expect(screen.getByRole('button', { name: /Food/ })).toBeInTheDocument()
|
|
expect(screen.getByRole('button', { name: /Museums/ })).toBeInTheDocument()
|
|
expect(screen.getByRole('button', { name: /mobileTrip.newCategory/ })).toBeInTheDocument()
|
|
})
|
|
|
|
it('FE-MOB-PLCAT-002: marks "no category" picked for the empty value', () => {
|
|
setup()
|
|
openList()
|
|
expect(screen.getByRole('button', { name: /^places.noCategory$/ })).toHaveAttribute('aria-pressed', 'true')
|
|
expect(screen.getByRole('button', { name: /Food/ })).toHaveAttribute('aria-pressed', 'false')
|
|
})
|
|
|
|
it('FE-MOB-PLCAT-003: the field shows the picked category, and the list marks it', () => {
|
|
setup({}, '4')
|
|
expect(field()).toHaveAccessibleName('places.formCategory: Museums')
|
|
openList()
|
|
expect(screen.getByRole('button', { name: /Museums/ })).toHaveAttribute('aria-pressed', 'true')
|
|
expect(screen.getByRole('button', { name: /^places.noCategory$/ })).toHaveAttribute('aria-pressed', 'false')
|
|
})
|
|
|
|
it('FE-MOB-PLCAT-004: reports the picked category id as a string and folds the list away', () => {
|
|
const { onChange } = setup({}, '')
|
|
openList()
|
|
fireEvent.click(screen.getByRole('button', { name: /Food/ }))
|
|
expect(onChange).toHaveBeenCalledWith('3')
|
|
expect(screen.queryByRole('button', { name: /Museums/ })).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('FE-MOB-PLCAT-005: reports the empty value when the category is cleared', () => {
|
|
const { onChange } = setup({}, '3')
|
|
openList()
|
|
fireEvent.click(screen.getByRole('button', { name: /^places.noCategory$/ }))
|
|
expect(onChange).toHaveBeenCalledWith('')
|
|
})
|
|
|
|
it('FE-MOB-PLCAT-006: creates a category inline and selects it right away', async () => {
|
|
const tripActions = buildTripActions()
|
|
tripActions.addCategory.mockResolvedValue({ id: 9, name: 'Bars' } as unknown as Category)
|
|
const { onChange } = setup({ tripActions } as unknown as Partial<TripPlanner>)
|
|
|
|
openList()
|
|
fireEvent.click(screen.getByRole('button', { name: /mobileTrip\.newCategory/ }))
|
|
const input = screen.getByPlaceholderText('places.categoryNamePlaceholder')
|
|
fireEvent.change(input, { target: { value: ' Bars ' } })
|
|
fireEvent.click(screen.getByRole('button', { name: 'common.add' }))
|
|
|
|
await waitFor(() => expect(onChange).toHaveBeenCalledWith('9'))
|
|
expect(tripActions.addCategory).toHaveBeenCalledWith({ name: 'Bars', color: '#6366f1', icon: 'MapPin' })
|
|
// The new category is picked, so the list folds away with the name field.
|
|
await waitFor(() => expect(screen.queryByPlaceholderText('places.categoryNamePlaceholder')).not.toBeInTheDocument())
|
|
expect(field()).toHaveAttribute('aria-expanded', 'false')
|
|
})
|
|
|
|
it('FE-MOB-PLCAT-007: Enter in the name field creates the category', async () => {
|
|
const tripActions = buildTripActions()
|
|
tripActions.addCategory.mockResolvedValue({ id: 12, name: 'Beaches' } as unknown as Category)
|
|
const { onChange } = setup({ tripActions } as unknown as Partial<TripPlanner>)
|
|
|
|
openList()
|
|
fireEvent.click(screen.getByRole('button', { name: /mobileTrip\.newCategory/ }))
|
|
const input = screen.getByPlaceholderText('places.categoryNamePlaceholder')
|
|
fireEvent.change(input, { target: { value: 'Beaches' } })
|
|
fireEvent.keyDown(input, { key: 'Enter' })
|
|
|
|
await waitFor(() => expect(onChange).toHaveBeenCalledWith('12'))
|
|
})
|
|
|
|
it('FE-MOB-PLCAT-008: ignores Enter and keeps the confirm disabled while the name is blank', () => {
|
|
const tripActions = buildTripActions()
|
|
setup({ tripActions } as unknown as Partial<TripPlanner>)
|
|
|
|
openList()
|
|
fireEvent.click(screen.getByRole('button', { name: /mobileTrip\.newCategory/ }))
|
|
const input = screen.getByPlaceholderText('places.categoryNamePlaceholder')
|
|
expect(screen.getByRole('button', { name: 'common.add' })).toBeDisabled()
|
|
fireEvent.change(input, { target: { value: ' ' } })
|
|
fireEvent.keyDown(input, { key: 'a' })
|
|
fireEvent.keyDown(input, { key: 'Enter' })
|
|
expect(tripActions.addCategory).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('FE-MOB-PLCAT-009: does not fire a second create while the first is in flight', async () => {
|
|
const tripActions = buildTripActions()
|
|
let release: (cat: Category) => void = () => {}
|
|
tripActions.addCategory.mockReturnValue(new Promise<Category>(resolve => { release = resolve }))
|
|
const { onChange } = setup({ tripActions } as unknown as Partial<TripPlanner>)
|
|
|
|
openList()
|
|
fireEvent.click(screen.getByRole('button', { name: /mobileTrip\.newCategory/ }))
|
|
const input = screen.getByPlaceholderText('places.categoryNamePlaceholder')
|
|
fireEvent.change(input, { target: { value: 'Bars' } })
|
|
fireEvent.keyDown(input, { key: 'Enter' })
|
|
fireEvent.keyDown(input, { key: 'Enter' })
|
|
expect(tripActions.addCategory).toHaveBeenCalledTimes(1)
|
|
expect(screen.getByRole('button', { name: 'common.add' })).toBeDisabled()
|
|
|
|
release({ id: 21 } as unknown as Category)
|
|
await waitFor(() => expect(onChange).toHaveBeenCalledWith('21'))
|
|
})
|
|
|
|
it('FE-MOB-PLCAT-010: toasts and keeps the draft when the create fails', async () => {
|
|
const tripActions = buildTripActions()
|
|
tripActions.addCategory.mockRejectedValue(new Error('409'))
|
|
const { planner, onChange } = setup({ tripActions } as unknown as Partial<TripPlanner>)
|
|
|
|
openList()
|
|
fireEvent.click(screen.getByRole('button', { name: /mobileTrip\.newCategory/ }))
|
|
fireEvent.change(screen.getByPlaceholderText('places.categoryNamePlaceholder'), { target: { value: 'Bars' } })
|
|
fireEvent.click(screen.getByRole('button', { name: 'common.add' }))
|
|
|
|
await waitFor(() => expect(planner.toast.error).toHaveBeenCalledWith('places.categoryCreateError'))
|
|
expect(onChange).not.toHaveBeenCalled()
|
|
expect(screen.getByPlaceholderText('places.categoryNamePlaceholder')).toHaveValue('Bars')
|
|
})
|
|
|
|
it('FE-MOB-PLCAT-011: cancelling drops the draft and restores the new-category row', () => {
|
|
setup()
|
|
openList()
|
|
fireEvent.click(screen.getByRole('button', { name: /mobileTrip\.newCategory/ }))
|
|
fireEvent.change(screen.getByPlaceholderText('places.categoryNamePlaceholder'), { target: { value: 'Bars' } })
|
|
fireEvent.click(screen.getByRole('button', { name: 'common.cancel' }))
|
|
|
|
expect(screen.queryByPlaceholderText('places.categoryNamePlaceholder')).not.toBeInTheDocument()
|
|
fireEvent.click(screen.getByRole('button', { name: /mobileTrip\.newCategory/ }))
|
|
expect(screen.getByPlaceholderText('places.categoryNamePlaceholder')).toHaveValue('')
|
|
})
|
|
|
|
it('FE-MOB-PLCAT-012: tolerates a trip without any categories', () => {
|
|
setup({ categories: undefined } as unknown as Partial<TripPlanner>)
|
|
openList()
|
|
expect(screen.getByRole('button', { name: /^places\.noCategory$/ })).toBeInTheDocument()
|
|
expect(screen.queryByRole('button', { name: /Food/ })).not.toBeInTheDocument()
|
|
})
|
|
})
|