Files
TREK/client/tests/helpers/msw/handlers/budget.ts
T
Maurice 5fcb307270 test(client): hold the default MSW handlers to the @trek/shared contracts
contractHandler() parses the request body against the shared request schema,
answering 400 like the server's Zod pipe on a body it would refuse, and the
mock's answer against the response schema, failing on a factory that drifted.
The trips, budget, places, todo, tags and categories, day notes, packing,
reservations and assignments handlers and the vacay plan and colour updates use
it wherever shared has a schema; the offline bundle and the other vacay routes
have none yet and stay plain.
The typed bodies showed where the old handlers echoed request shapes back as
stored rows: payers and members are no longer echoed into a budget item, and
checked and is_private come back as 0/1 the way the server stores them.
2026-10-10 21:31:34 +02:00

85 lines
2.5 KiB
TypeScript

import {
budgetCreateItemRequestSchema,
budgetItemMemberSchema,
budgetItemSchema,
budgetToggleMemberPaidRequestSchema,
budgetUpdateItemRequestSchema,
budgetUpdateMembersRequestSchema,
} from '@trek/shared';
import { HttpResponse } from 'msw';
import { z } from 'zod';
import { buildBudgetItem } from '../../factories';
import { contractHandler } from '../contract';
const itemResponse = z.object({ item: budgetItemSchema });
/**
* The fields of a create or update request that the stored item carries as
* they are. Payers, members and receipts arrive as ids and amounts and come
* back in a different shape the server builds, so they are not echoed.
*/
const itemFields = <T extends { payers?: unknown; members?: unknown; member_ids?: unknown }>(body: T) => {
const { payers: _payers, members: _members, member_ids: _memberIds, ...fields } = body;
return fields;
};
export const budgetHandlers = [
contractHandler(
'get',
'/api/trips/:id/budget',
{ response: z.object({ items: z.array(budgetItemSchema) }) },
({ params }) => ({
items: [buildBudgetItem({ trip_id: Number(params.id) })],
})
),
contractHandler(
'post',
'/api/trips/:id/budget',
{ request: budgetCreateItemRequestSchema, response: itemResponse },
({ params, body }) => ({
item: buildBudgetItem({ trip_id: Number(params.id), ...itemFields(body) }),
})
),
contractHandler(
'put',
'/api/trips/:id/budget/:itemId',
{ request: budgetUpdateItemRequestSchema, response: itemResponse },
({ params, body }) => ({
item: buildBudgetItem({ id: Number(params.itemId), trip_id: Number(params.id), ...itemFields(body) }),
})
),
contractHandler('delete', '/api/trips/:id/budget/:itemId', {}, () => HttpResponse.json({ success: true })),
contractHandler(
'put',
'/api/trips/:id/budget/:itemId/members',
{
request: budgetUpdateMembersRequestSchema,
response: itemResponse.extend({ members: z.array(budgetItemMemberSchema) }),
},
({ params, body }) => {
const members = body.user_ids.map((uid) => ({ user_id: uid, paid: 0, username: `user${uid}` }));
const item = buildBudgetItem({
id: Number(params.itemId),
trip_id: Number(params.id),
persons: body.user_ids.length,
members,
});
return { members, item };
}
),
contractHandler(
'put',
'/api/trips/:id/budget/:itemId/members/:userId/paid',
{ request: budgetToggleMemberPaidRequestSchema },
({ body }) => ({
success: true,
paid: body.paid,
})
),
];