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