Files
TREK/client/tests/unit/mobile/trip/MTodoListTab.test.tsx
T
Maurice 0b7cee0b0d feat(mobile): the trip's tabs, administration, collections and the journal use the large screen
On an iPad or an unfolded foldable the list screens kept one phone-wide
column in the middle of the page. They now lay out for the room they have:

- Transports, bookings, polls and tasks put their cards in a grid of as many
  phone-wide columns as the page holds (CardGrid), three on a 13" iPad on
  its side, two upright, one in a narrow Split View.
- Costs: with room for two panes (split size class) the summary (total,
  owe and owed, settle up, final budget, categories) stands in a pane of its
  own beside the expenses, each scrolling on its own (TabPanes). The search
  field sits in the expenses' column again instead of running past it.
- Files: a grid of tiles with each photo's picture; with room for two panes
  the picked file is previewed beside the grid with what it belongs to and
  its actions, the way Files on an iPad does. The signed preview address
  comes through fileRepo.previewUrl.
- Packing: the categories stand in columns, each card whole in its column;
  the action menu drops from its button at a popover's width.
- To-dos: the four smart filters as tiles and the trip's lists in a pane
  beside the tasks, which take the filter's name as their head.
- Collab: the chat stays in view at the trailing edge beside the notes,
  links or polls, and the header switches only the board. The chat keeps its
  place in the tree, so a fold or a rotation into two panes keeps the draft;
  a chat at its newest message stays there when its box changes size. Notes
  stand as a board in columns.
- Administration and settings share a section list beside the page
  (MSectionNav); the stats grid takes four columns.
- Collections: the lists stand in a column beside the places, which take two
  columns, and the head names the list on show.
- Journal: the entries become a feed beside the map, under their days, with
  photo, place and the start of the story, in step with the map like the
  strip of cards it replaces; it fades out under the header.
- The assistant opens as a tall panel at the trailing edge over a dimmed
  page, as Slide Over would, instead of covering the whole screen.
- The rail fits every name: a word too wide for it shrinks the label before
  it is hyphenated, and the admin entry uses its short name.
- The journeys and notifications headers keep to their column.
- The address screen of the native shell sets the photo as a panel beside
  the choices on a large screen on its side.

On a foldable that reports its fold, a leading pane ends at the fold and a
trailing one starts there. Phones keep their layout.
2026-11-07 23:25:12 +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()
})
})