Files
TREK/client/tests/unit/mobile/trip/MTodoListTab.test.tsx
T
Maurice 8955790f17 fix(mobile): the trip's screens on an iPad and an unfolded foldable, nothing cut or covered
- Top bar: the segmented control keeps to the room between the back button
  and the trailing button, draws its segments tighter and then scrolls with
  faded edges instead of covering the back button. On a wide window every
  list tab puts its primary pill in the middle and its round buttons at the
  trailing end, so the pill no longer jumps between tabs.
- Day strip: a chevron over the faded end pages the days along on a wide
  window, and the fade reaches further; the chips keep clear of it.
- Plan pane: the day card ends on the rail's bottom line beside the map (as
  tall as its day across the page), scrolls inside a frame and fades the
  rows at its edges. Addresses and the hotel chip end in an ellipsis instead
  of being cut mid-letter; the weather chip keeps its width.
- Map: in the dark theme the map beside the pane takes the dark basemap.
- Places: the All / Unplanned / Planned chips are never narrower than their
  names and scroll with a faded edge; on a short screen Places/Tours shares
  their row.
- Chat: the list fades out under the floating controls instead of cutting a
  bubble. Notes and to-do chip rows fade at their edges and New Note stays
  outside the scrolling chips.
- Costs: the balance amounts share a line, the settle-up action wraps to a
  line of its own, the totals stand under their labels.
- Bookings and transports: one column until two cards fit, titles take two
  lines with the type under them, the time cell is never cut and a range
  names its unit once.
- Files: the preview pane shows a type tile beside the name where no PDF can
  be drawn in place, its actions keep their labels on one line and wrap as a
  row, link labels are badges, and a tile that opens its file is no toggle.
- Polls get a fill that differs from card and track, packing filters are two
  segmented controls, empty checkboxes get an edge with 3:1 contrast, and the
  middle dots between values are gone.
2026-11-07 23:25:16 +01:00

363 lines
14 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
import MTodoListTab from '../../../../src/mobile/screens/trip/tabs/MTodoListTab'
import type { MTaskSheetProps } from '../../../../src/mobile/screens/trip/tabs/MTaskSheet'
import type { TripPlanner } from '../../../../src/mobile/screens/trip/MTripShell'
import { useAuthStore } from '../../../../src/store/authStore'
import type { TodoItem, TripMember } from '../../../../src/types'
import { buildUser } from '../../../helpers/factories'
import { buildPlanner, buildTripActions } from '../../../helpers/mobileTrip'
import { resetAllStores, seedStore } from '../../../helpers/store'
import { fireEvent, render, screen, within } from '../../../helpers/render'
import { setSizeClass } from '../../../helpers/sizeClass'
// FE-MOB-TODO-001 to FE-MOB-TODO-019
// The sheet has its own suite; here it only has to report the props the tab feeds it.
vi.mock('../../../../src/mobile/screens/trip/tabs/MTaskSheet', () => ({
default: (props: MTaskSheetProps) => (
<div
data-testid="task-sheet"
data-open={String(props.open)}
data-item-id={String(props.itemId)}
data-default-category={String(props.defaultCategory)}
data-categories={props.categories.join('|')}
data-members={props.members.map(m => m.username).join('|')}
/>
),
}))
const ME = 7
const PAST = '2020-01-05'
const FUTURE = '2999-12-31'
const MEMBERS = [
{ id: ME, username: 'maurice', avatar: null, avatar_url: null },
{ id: 8, username: 'lea', avatar: null, avatar_url: '/uploads/avatars/lea.png' },
] as unknown as TripMember[]
function todo(overrides: Partial<TodoItem> = {}): TodoItem {
return {
id: 1,
trip_id: 1,
name: 'Book flights',
category: null,
checked: 0,
sort_order: 0,
due_date: null,
description: null,
assigned_user_id: null,
priority: 0,
...overrides,
}
}
function setup(items: TodoItem[], plannerOverrides: Partial<TripPlanner> = {}) {
const actions = buildTripActions()
const planner = buildPlanner({
todoItems: items,
tripMembers: MEMBERS,
tripActions: actions as unknown as TripPlanner['tripActions'],
...plannerOverrides,
})
const view = render(<MTodoListTab planner={planner} />)
return { ...view, actions, planner }
}
function sheet() {
return screen.getByTestId('task-sheet')
}
/** Row order, read off the check buttons — they are the only ones carrying the task name as aria-label. */
function rowOrder(names: string[]) {
return screen
.getAllByRole('button')
.map(b => b.getAttribute('aria-label'))
.filter((label): label is string => !!label && names.includes(label))
}
describe('MTodoListTab', () => {
beforeEach(() => {
resetAllStores()
seedStore(useAuthStore, { user: buildUser({ id: ME, username: 'maurice' }) })
})
it('FE-MOB-TODO-001: renders the empty state and no filter row without items', () => {
setup([])
expect(screen.getByText('todo.empty')).toBeInTheDocument()
expect(screen.getByText('0/0')).toBeInTheDocument()
expect(screen.getByText('0% todo.completed')).toBeInTheDocument()
expect(screen.queryByRole('button', { name: /todo.filter.all/ })).not.toBeInTheDocument()
})
it('FE-MOB-TODO-002: reports done/total and the completion percentage', () => {
setup([todo({ id: 1 }), todo({ id: 2, checked: 1 }), todo({ id: 3, checked: 1 }), todo({ id: 4 })])
expect(screen.getByText('2/4')).toBeInTheDocument()
expect(screen.getByText('50% todo.completed')).toBeInTheDocument()
})
it('FE-MOB-TODO-003: hides "new task" without packing_edit', () => {
setup([todo()], { can: vi.fn(() => false) as unknown as TripPlanner['can'] })
expect(screen.queryByRole('button', { name: /todo.newItem/ })).not.toBeInTheDocument()
expect(screen.getByText('Book flights')).toBeInTheDocument()
})
it('FE-MOB-TODO-004: shows the four built-in filters with their counts', () => {
setup([
todo({ id: 1, name: 'Open' }),
todo({ id: 2, name: 'Mine', assigned_user_id: ME }),
todo({ id: 3, name: 'Late', due_date: PAST }),
todo({ id: 4, name: 'Done', checked: 1 }),
])
expect(screen.getByRole('button', { name: 'todo.filter.all3' })).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'todo.filter.my1' })).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'todo.filter.overdue1' })).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'todo.filter.done1' })).toBeInTheDocument()
})
it('FE-MOB-TODO-005: defaults to the open items and hides done ones', () => {
setup([todo({ id: 1, name: 'Open' }), todo({ id: 2, name: 'Packed', checked: 1 })])
expect(screen.getByText('Open')).toBeInTheDocument()
expect(screen.queryByText('Packed')).not.toBeInTheDocument()
})
it('FE-MOB-TODO-006: switches to the my / overdue / done buckets', () => {
setup([
todo({ id: 1, name: 'Open' }),
todo({ id: 2, name: 'Mine', assigned_user_id: ME }),
todo({ id: 3, name: 'Late', due_date: PAST }),
todo({ id: 4, name: 'Packed', checked: 1 }),
])
fireEvent.click(screen.getByRole('button', { name: /todo.filter.my/ }))
expect(screen.getByText('Mine')).toBeInTheDocument()
expect(screen.queryByText('Open')).not.toBeInTheDocument()
fireEvent.click(screen.getByRole('button', { name: /todo.filter.overdue/ }))
expect(screen.getByText('Late')).toBeInTheDocument()
expect(screen.queryByText('Mine')).not.toBeInTheDocument()
fireEvent.click(screen.getByRole('button', { name: /todo.filter.done/ }))
expect(screen.getByText('Packed')).toBeInTheDocument()
expect(screen.queryByText('Late')).not.toBeInTheDocument()
})
it('FE-MOB-TODO-006b: leaves the "my" bucket empty when nobody is signed in', () => {
seedStore(useAuthStore, { user: null })
setup([todo({ id: 1, name: 'Mine', assigned_user_id: ME })])
expect(screen.getByRole('button', { name: 'todo.filter.my0' })).toBeInTheDocument()
fireEvent.click(screen.getByRole('button', { name: /todo.filter.my/ }))
expect(screen.getByText('todo.emptyFiltered')).toBeInTheDocument()
})
it('FE-MOB-TODO-007: shows the filtered-empty hint when a bucket is empty', () => {
setup([todo({ id: 1, name: 'Open' })])
fireEvent.click(screen.getByRole('button', { name: /todo.filter.done/ }))
expect(screen.getByText('todo.emptyFiltered')).toBeInTheDocument()
expect(screen.queryByText('todo.empty')).not.toBeInTheDocument()
})
it('FE-MOB-TODO-008: toggles the priority sort and reorders the rows', () => {
setup([
todo({ id: 1, name: 'Low', priority: 3 }),
todo({ id: 2, name: 'High', priority: 1 }),
])
expect(rowOrder(['Low', 'High'])).toEqual(['Low', 'High'])
const sortButton = screen.getByRole('button', { name: /todo.priority/ })
expect(sortButton).toHaveAttribute('aria-pressed', 'false')
fireEvent.click(sortButton)
expect(sortButton).toHaveAttribute('aria-pressed', 'true')
expect(rowOrder(['Low', 'High'])).toEqual(['High', 'Low'])
})
it('FE-MOB-TODO-020: toggles the due-date sort and puts the nearest deadline first (#2205)', () => {
setup([
todo({ id: 1, name: 'Later', due_date: '2099-07-17' }),
todo({ id: 2, name: 'Sooner', due_date: '2099-07-16' }),
todo({ id: 3, name: 'Undated' }),
])
expect(rowOrder(['Later', 'Sooner', 'Undated'])).toEqual(['Later', 'Sooner', 'Undated'])
const sortButton = screen.getByRole('button', { name: /todo.detail.dueDate/ })
expect(sortButton).toHaveAttribute('aria-pressed', 'false')
fireEvent.click(sortButton)
expect(sortButton).toHaveAttribute('aria-pressed', 'true')
expect(rowOrder(['Later', 'Sooner', 'Undated'])).toEqual(['Sooner', 'Later', 'Undated'])
// one sort at a time: flipping priority on releases the due toggle
fireEvent.click(screen.getByRole('button', { name: /todo.priority/ }))
expect(sortButton).toHaveAttribute('aria-pressed', 'false')
})
it('FE-MOB-TODO-009: floats overdue rows above the other open ones', () => {
setup([
todo({ id: 1, name: 'Open' }),
todo({ id: 2, name: 'Packed', checked: 1 }),
todo({ id: 3, name: 'Late', due_date: PAST }),
])
expect(rowOrder(['Open', 'Packed', 'Late'])).toEqual(['Late', 'Open'])
})
it('FE-MOB-TODO-009b: sinks done rows to the end of a category bucket', () => {
setup([
todo({ id: 1, name: 'Packed', category: 'Gear', checked: 1 }),
todo({ id: 2, name: 'Boots', category: 'Gear' }),
])
fireEvent.click(screen.getByRole('button', { name: 'Gear1' }))
expect(rowOrder(['Packed', 'Boots'])).toEqual(['Boots', 'Packed'])
})
it('FE-MOB-TODO-010: renders a category rail with open counts and filters by it', () => {
setup([
todo({ id: 1, name: 'Visa', category: 'Admin' }),
todo({ id: 2, name: 'Insurance', category: 'Admin', checked: 1 }),
todo({ id: 3, name: 'Boots', category: 'Gear' }),
])
expect(screen.getByRole('button', { name: 'Admin1' })).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Gear1' })).toBeInTheDocument()
fireEvent.click(screen.getByRole('button', { name: 'Admin1' }))
// the category bucket keeps done items visible
expect(screen.getByText('Visa')).toBeInTheDocument()
expect(screen.getByText('Insurance')).toBeInTheDocument()
expect(screen.queryByText('Boots')).not.toBeInTheDocument()
})
it('FE-MOB-TODO-011: toggles a task through the store action', () => {
const { actions } = setup([todo({ id: 12, name: 'Book flights' })])
fireEvent.click(screen.getByRole('button', { name: 'Book flights', pressed: false }))
expect(actions.toggleTodoItem).toHaveBeenCalledWith(1, 12, true)
})
it('FE-MOB-TODO-012: unchecks an already done task', () => {
const { actions } = setup([todo({ id: 12, name: 'Book flights', checked: 1 })])
fireEvent.click(screen.getByRole('button', { name: /todo.filter.done/ }))
fireEvent.click(screen.getByRole('button', { name: 'Book flights', pressed: true }))
expect(actions.toggleTodoItem).toHaveBeenCalledWith(1, 12, false)
})
it('FE-MOB-TODO-013: renders description, priority, due date and assignee on a card', () => {
setup([todo({
id: 3,
name: 'Book flights',
description: 'Direct if possible',
priority: 1,
due_date: PAST,
assigned_user_id: 8,
})])
expect(screen.getByText('Direct if possible')).toBeInTheDocument()
expect(screen.getByText('P1')).toBeInTheDocument()
expect(screen.getByText(/Jan/)).toBeInTheDocument()
expect(screen.getByText('lea')).toBeInTheDocument()
expect(document.querySelector('img[src="/uploads/avatars/lea.png"]')).toBeInTheDocument()
})
it('FE-MOB-TODO-014: falls back to the username initial when a member has no avatar', () => {
setup([todo({ id: 3, due_date: FUTURE, assigned_user_id: ME })])
expect(screen.getByText('maurice')).toBeInTheDocument()
expect(screen.getByText('M')).toBeInTheDocument()
expect(document.querySelector('img')).toBeNull()
})
it('FE-MOB-TODO-015: opens the sheet in create mode and closes it again', () => {
setup([todo({ id: 3, category: 'Admin' })])
expect(sheet()).toHaveAttribute('data-open', 'false')
expect(sheet()).toHaveAttribute('data-categories', 'Admin')
expect(sheet()).toHaveAttribute('data-members', 'maurice|lea')
fireEvent.click(screen.getByRole('button', { name: /todo.newItem/ }))
expect(sheet()).toHaveAttribute('data-open', 'true')
expect(sheet()).toHaveAttribute('data-item-id', 'null')
expect(sheet()).toHaveAttribute('data-default-category', 'null')
})
it('FE-MOB-TODO-016: seeds the new task with the active category filter', () => {
setup([todo({ id: 3, name: 'Visa', category: 'Admin' })])
fireEvent.click(screen.getByRole('button', { name: 'Admin1' }))
fireEvent.click(screen.getByRole('button', { name: /todo.newItem/ }))
expect(sheet()).toHaveAttribute('data-default-category', 'Admin')
})
it('FE-MOB-TODO-017: opens the sheet in edit mode for the tapped row', () => {
setup([todo({ id: 42, name: 'Book flights' })])
fireEvent.click(screen.getByText('Book flights'))
expect(sheet()).toHaveAttribute('data-open', 'true')
expect(sheet()).toHaveAttribute('data-item-id', '42')
})
it('FE-MOB-TODO-018: a category named like a built-in filter keeps its own bucket', () => {
setup([
todo({ id: 1, name: 'Visa', category: 'done' }),
todo({ id: 2, name: 'Insurance', category: 'done', checked: 1 }),
todo({ id: 3, name: 'Boots' }),
])
fireEvent.click(screen.getByRole('button', { name: 'done1' }))
expect(screen.getByText('Visa')).toBeInTheDocument()
expect(screen.getByText('Insurance')).toBeInTheDocument()
expect(screen.queryByText('Boots')).not.toBeInTheDocument()
fireEvent.click(screen.getByRole('button', { name: /todo.newItem/ }))
expect(sheet()).toHaveAttribute('data-default-category', 'done')
})
it('FE-MOB-TODO-019: shows an unparseable due date as it is stored', () => {
setup([todo({ id: 3, name: 'Book flights', due_date: 'sometime' })])
expect(screen.getByText('sometime')).toBeInTheDocument()
})
it('FE-MOB-TODO-021: with room for two panes the filters stand beside the tasks, which take their name as a head', () => {
setSizeClass('split')
setup([
todo({ id: 1, name: 'Visa', category: 'Before' }),
todo({ id: 2, name: 'Insurance', checked: 1 }),
])
const filters = screen.getByRole('complementary', { name: 'todo.sidebar.tasks' })
expect(within(filters).getByText('todo.sidebar.categories')).toBeInTheDocument()
expect(screen.getByRole('heading', { name: 'todo.filter.all' })).toBeInTheDocument()
expect(screen.getByText('Visa')).toBeInTheDocument()
expect(screen.queryByText('Insurance')).not.toBeInTheDocument()
fireEvent.click(within(filters).getByRole('button', { name: /todo.filter.done/ }))
expect(screen.getByRole('heading', { name: 'todo.filter.done' })).toBeInTheDocument()
expect(screen.getByText('Insurance')).toBeInTheDocument()
fireEvent.click(within(filters).getByRole('button', { name: /Before/ }))
expect(screen.getByRole('heading', { name: 'Before' })).toBeInTheDocument()
expect(screen.getByText('Visa')).toBeInTheDocument()
})
})