import { render, screen } from '@testing-library/react'; import { describe, expect, it } from 'vitest'; import { orderViews } from '../src/pages/Orders/orderViews'; import type { LedgerState } from '../src/shared'; const date = '2026-11-20'; const account = '麦麦信用卡'; function renderPayment(action: 'receipt' | 'correct', orderRate?: number, shopRate?: number) { const state: LedgerState = { today: date, mode: 'trial', user: { id: 'owner-id', role: 'owner', member: 'owner', name: '店主' }, accounts: [account], shopSettings: { creditCardRatePermille: shopRate }, }; const views = orderViews({ state, session: { user: state.user }, currentOrder: { id: 'order-1', date, balance: 25000, payment_account: account, credit_card_rate_permille: orderRate, payments: [{ id: 'receipt-1', kind: 'receipt', date, account, amount: 25000 }], corrections: [], expenses: [], settlements: [], }, ui: { action, date, paymentMethod: '信用卡', values: { amount: '250.00', entry: 'payment:receipt-1', account } }, }); render(views.showAction(action)); } describe.each(['receipt', 'correct'] as const)('订单 %s 的信用卡手续费', action => { it.each([ { detail: '保留原单零费率', orderRate: 0, shopRate: 9, rate: 0, fee: '0.00' }, { detail: '沿用历史费率', orderRate: 4, shopRate: 9, rate: 4, fee: '1.00' }, { detail: '原单未提供费率时使用店铺配置', orderRate: undefined, shopRate: 9, rate: 9, fee: '2.25' }, { detail: '店铺配置可以为零', orderRate: undefined, shopRate: 0, rate: 0, fee: '0.00' }, ])('$detail', ({ orderRate, shopRate, rate, fee }) => { renderPayment(action, orderRate, shopRate); expect(screen.getByLabelText('信用卡手续费自动金额')).toHaveDisplayValue(fee); expect(screen.getByText(new RegExp(`× ${rate}‰`))).toHaveTextContent(`¥250.00 × ${rate}‰ = ¥${fee}`); }); it.each([ { detail: '原单与店铺费率均缺失', orderRate: undefined }, { detail: '原单费率无效', orderRate: 1001 }, ])('$detail时留空并提示重新读取', ({ orderRate }) => { renderPayment(action, orderRate); expect(screen.getByLabelText('信用卡手续费自动金额')).toHaveDisplayValue(''); expect(screen.getByText('信用卡手续费率未能确认,请重新读取后核对。')).toBeVisible(); expect(screen.getByLabelText('手续费付款人')).toBeRequired(); }); });