diff --git a/client/src/components/Budget/CostsPanel.test.tsx b/client/src/components/Budget/CostsPanel.test.tsx index d84f687e7..578c6d0e1 100644 --- a/client/src/components/Budget/CostsPanel.test.tsx +++ b/client/src/components/Budget/CostsPanel.test.tsx @@ -222,6 +222,7 @@ describe('CostsPanel — settlements in the ledger', () => { }) it('records a recorded-total expense with nobody to split with (#1286)', async () => { + seedStore(useAuthStore, { user: buildUser({ id: 1, username: 'alice' }), isAuthenticated: true }) let posted: Record | null = null server.use( http.get('/api/trips/1/budget', () => HttpResponse.json({ items: [] })), @@ -239,11 +240,12 @@ describe('CostsPanel — settlements in the ledger', () => { await user.type(await screen.findByPlaceholderText('e.g. Dinner, souvenirs, gas…'), 'Hotel') await user.type(screen.getAllByPlaceholderText('0,00')[0], '120') // total only, paid on-site later - // Deselect everyone — the cost is recorded without a split (the bug: this was blocked). - // The participant toggles are buttons; the same names also appear as plain text in - // the Balances sidebar, so target the buttons specifically. - await user.click(screen.getByRole('button', { name: /alice/i })) - await user.click(screen.getByRole('button', { name: /bob/i })) + // Deselect everyone so the cost carries no split, and mark it as unpaid: a picked + // payer now always goes out (#1766), so "nobody paid" must be said explicitly. + await user.click(screen.getByRole('button', { name: 'Y You' })) + await user.click(screen.getByRole('button', { name: 'B bob' })) + await user.click(screen.getByRole('button', { name: 'You' })) // open the Who-paid select + pickOption('No one paid yet') const addBtns = screen.getAllByRole('button', { name: 'Add expense' }) const submit = addBtns[addBtns.length - 1] // footer submit @@ -256,6 +258,40 @@ describe('CostsPanel — settlements in the ledger', () => { expect(posted!.payers).toEqual([]) }) + it('keeps a picked payer when nobody splits the expense (#1766)', async () => { + seedStore(useAuthStore, { user: buildUser({ id: 1, username: 'alice' }), isAuthenticated: true }) + let posted: Record | null = null + server.use( + http.get('/api/trips/1/budget', () => HttpResponse.json({ items: [] })), + http.get('/api/trips/1/budget/settlement', () => HttpResponse.json({ balances: [], flows: [], settlements: [] })), + http.post('/api/trips/1/budget', async ({ request }) => { + posted = await request.json() as Record + return HttpResponse.json({ item: { ...buildBudgetItem({ trip_id: 1, name: 'Flight' }), id: 11 } }) + }), + ) + const { default: userEvent } = await import('@testing-library/user-event') + const user = userEvent.setup() + render() + + await user.click(await screen.findByRole('button', { name: 'Add expense' })) + await user.type(await screen.findByPlaceholderText('e.g. Dinner, souvenirs, gas…'), 'Flight') + await user.type(screen.getAllByPlaceholderText('0,00')[0], '100') + + // A personal expense: alice (the default "You" payer) fronted it, but nobody shares + // the split. The web used to drop the payer once no participants remained. + await user.click(screen.getByRole('button', { name: 'Y You' })) + await user.click(screen.getByRole('button', { name: 'B bob' })) + + const addBtns = screen.getAllByRole('button', { name: 'Add expense' }) + const submit = addBtns[addBtns.length - 1] // footer submit + expect(submit).not.toBeDisabled() + await user.click(submit) + + await waitFor(() => expect(posted).toBeTruthy()) + expect(posted!.member_ids).toEqual([]) + expect(posted!.payers).toEqual([{ user_id: 1, amount: 100 }]) + }) + it('keeps "no one paid yet" when reopening a payer-less expense (#1533)', async () => { seedStore(useAuthStore, { user: buildUser({ id: 1, username: 'alice' }), isAuthenticated: true }) let put: Record | null = null diff --git a/client/src/components/Budget/CostsPanel.tsx b/client/src/components/Budget/CostsPanel.tsx index d0e99bb18..9eb5183d8 100644 --- a/client/src/components/Budget/CostsPanel.tsx +++ b/client/src/components/Budget/CostsPanel.tsx @@ -1228,11 +1228,14 @@ export function ExpenseModal({ tripId, base, people, me, editing, prefill, onClo const save = async () => { if (!valid) return setSaving(true) + // A picked payer always goes out, even when nobody shares the expense: the + // server re-derives total_price from the payer sum (CostsPanel.helpers), so + // dropping the payer would store the entry with a total of 0. const payerList = multiPayer ? [...payerIds] .map(id => ({ user_id: id, amount: parseFloat(payerAmounts[id]) || 0 })) .filter(p => p.amount > 0) - : (payerId > 0 && participants.size > 0) ? [{ user_id: payerId, amount: totalNum }] : [] + : payerId > 0 ? [{ user_id: payerId, amount: totalNum }] : [] const memberList = [...participants].map(id => ({ user_id: id, amount: splitMode === 'custom'