Files
weiguang-jewelry-ledger/新账系统/web/test/order-card-fee.test.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

81 lines
3.3 KiB
TypeScript
Raw Normal View History

import transcriptJson from '../../e2e/baseline/golden/transcript.json?raw';
import { readOrderRecord } from '../src/pages/Orders/readOrder';
import { render, screen } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { orderViews } from '../src/pages/Orders/orderViews';
import { Cards, type LedgerState } from '../src/shared';
const date = '2026-11-20';
const account = '麦麦信用卡';
const transcript: { method?: string; path?: string; status?: number; body?: unknown }[] = JSON.parse(transcriptJson);
const saved = transcript.find(
step => step.method === 'GET' && step.path === '/api/orders/<id26>' && step.status === 200,
);
const order = readOrderRecord(saved?.body, '<id26>');
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, setup: true, mode: 'trial', today: date },
currentOrder: {
...order,
id: 'order-1',
date,
balance: 25000,
payment_account: account,
credit_card_rate_permille: orderRate,
payments: [
{
id: 'receipt-1',
kind: 'receipt',
date,
account,
amount: 25000,
reference: '',
order_id: 'order-1',
actor: '店主',
created: date,
},
],
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 => {
afterEach(() => vi.restoreAllMocks());
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 }) => {
const calculate = vi.spyOn(Cards, 'amount');
renderPayment(action, orderRate);
expect(screen.getByLabelText('信用卡手续费自动金额')).toHaveDisplayValue('');
expect(screen.getByText('信用卡手续费率未能确认,请重新读取后核对。')).toBeVisible();
expect(screen.getByLabelText('手续费付款人')).toBeRequired();
if (orderRate === undefined) expect(calculate).not.toHaveBeenCalled();
});
});