Files
TREK/client/src/components/Packing/PackingExportMenu.test.tsx
T
Maurice 5536d29cbf feat(packing): a calmer packing list, with every item's choices in one window
A row now shows its box, its name and only what is set: the amount from two
up, the pieces packed while packing, and who brings it. A click on the name
opens a small window at the row with the list, amount, pieces packed, weight,
bag, who brings it and who sees it, each as one pill, plus copy and delete,
instead of fields, a bin and menus in menus on every row. Above the lists the
Lists bar keeps Add list and gathers templates, import, export, the order,
hiding packed items, the bags on a narrow screen and the clean-up in one menu;
the progress moves next to the Shared/My list switch. The bag overview stays.

The phone follows the same shape: one "..." sheet for the list's actions, a
sheet per list for its own, a slim row and an item sheet that saves as you go.
Who may change what about an item lives in one module both views read.
2026-11-15 12:11:15 +01:00

117 lines
5.2 KiB
TypeScript

// FE-PACKEXPORTMENU-001 to FE-PACKEXPORTMENU-007
import { beforeEach, describe, expect, it, vi } from 'vitest'
import userEvent from '@testing-library/user-event'
import { fireEvent, render, screen } from '../../../tests/helpers/render'
import PackingExportMenu from './PackingExportMenu'
import PackingPrintPreview from './PackingPrintPreview'
import type { PackingExport } from './usePackingExport'
const packingExport: PackingExport = {
hasItems: true,
exportMarkdown: vi.fn(),
exportCsv: vi.fn(async () => {}),
openPrint: vi.fn(async () => {}),
printHtml: null,
printTitle: 'Packing List: Lisbon & Porto',
closePrint: vi.fn(),
}
vi.mock('./usePackingExport', () => ({ usePackingExport: () => packingExport }))
const renderMenu = () => render(<PackingExportMenu tripId={7} view="common" className="btn" style={{}} />)
beforeEach(() => {
vi.clearAllMocks()
packingExport.hasItems = true
packingExport.printHtml = null
})
describe('PackingExportMenu', () => {
it('FE-PACKEXPORTMENU-001: renders nothing while the view has no items', () => {
packingExport.hasItems = false
const { container } = renderMenu()
expect(container).toBeEmptyDOMElement()
})
it('FE-PACKEXPORTMENU-002: is an icon button named Export that opens a menu of three ways out', async () => {
const user = userEvent.setup()
renderMenu()
const button = screen.getByRole('button', { name: 'Export' })
expect(button).toHaveAttribute('title', 'Export')
expect(button).toHaveAttribute('aria-expanded', 'false')
await user.click(button)
expect(button).toHaveAttribute('aria-expanded', 'true')
expect(screen.getAllByRole('menuitem').map(i => i.textContent)).toEqual([
'Print or save as PDF', 'Markdown checklist (.md)', 'CSV for import (.csv)',
])
})
it('FE-PACKEXPORTMENU-003: runs the chosen export and closes the menu', async () => {
const user = userEvent.setup()
renderMenu()
for (const [label, action] of [
['Print or save as PDF', packingExport.openPrint],
['Markdown checklist (.md)', packingExport.exportMarkdown],
['CSV for import (.csv)', packingExport.exportCsv],
] as const) {
await user.click(screen.getByRole('button', { name: 'Export' }))
await user.click(screen.getByRole('menuitem', { name: label }))
expect(action).toHaveBeenCalledTimes(1)
expect(screen.queryByRole('menu')).not.toBeInTheDocument()
}
})
it('FE-PACKEXPORTMENU-004: closes on Escape and on a click anywhere else', async () => {
const user = userEvent.setup()
renderMenu()
await user.click(screen.getByRole('button', { name: 'Export' }))
fireEvent.keyDown(document, { key: 'Escape' })
expect(screen.queryByRole('menu')).not.toBeInTheDocument()
await user.click(screen.getByRole('button', { name: 'Export' }))
fireEvent.mouseDown(document.body)
expect(screen.queryByRole('menu')).not.toBeInTheDocument()
})
it('FE-PACKEXPORTMENU-005: keeps the preview open even if the view has been emptied meanwhile', () => {
packingExport.hasItems = false
packingExport.printHtml = '<html><body><h1>Lisbon</h1></body></html>'
renderMenu()
expect(screen.getByTitle('Packing List: Lisbon & Porto')).toBeInTheDocument()
})
})
describe('PackingPrintPreview', () => {
it('FE-PACKEXPORTMENU-006: shows the page in a frame, prints that frame, and closes', async () => {
const user = userEvent.setup()
const onClose = vi.fn()
const { rerender } = render(<PackingPrintPreview html={null} title="Packing List" onClose={onClose} />)
expect(screen.queryByTitle('Packing List')).not.toBeInTheDocument()
rerender(<PackingPrintPreview html="<html><body>List</body></html>" title="Packing List" onClose={onClose} />)
const frame = screen.getByTitle('Packing List') as HTMLIFrameElement
expect(frame.getAttribute('srcdoc')).toBe('<html><body>List</body></html>')
expect(frame.getAttribute('sandbox')).toBe('allow-same-origin allow-modals')
const print = vi.fn()
Object.defineProperty(frame, 'contentWindow', { value: { print }, configurable: true })
await user.click(screen.getByRole('button', { name: 'Print or save as PDF' }))
expect(print).toHaveBeenCalledTimes(1)
// The modal's X and the preview's own button both close; the footer one is pressed here.
const closeButtons = screen.getAllByRole('button', { name: 'Close' })
await user.click(closeButtons[closeButtons.length - 1])
expect(onClose).toHaveBeenCalledTimes(1)
})
it('FE-PACKEXPORTMENU-007: as menu lines it offers the three ways out and reports the choice', async () => {
const user = userEvent.setup()
const onChosen = vi.fn()
render(<PackingExportMenu tripId={7} view="personal" variant="menu" onChosen={onChosen} />)
expect(screen.queryByRole('button', { name: 'Export' })).toBeNull()
await user.click(screen.getByRole('button', { name: 'CSV for import (.csv)' }))
expect(packingExport.exportCsv).toHaveBeenCalledTimes(1)
expect(onChosen).toHaveBeenCalledTimes(1)
await user.click(screen.getByRole('button', { name: 'Print or save as PDF' }))
expect(packingExport.openPrint).toHaveBeenCalledTimes(1)
await user.click(screen.getByRole('button', { name: 'Markdown checklist (.md)' }))
expect(packingExport.exportMarkdown).toHaveBeenCalledTimes(1)
})
})