108 lines
4.4 KiB
TypeScript
108 lines
4.4 KiB
TypeScript
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|||
|
|
import { describe, expect, it, vi } from 'vitest';
|
||
|
|
import { TeamFundsPage } from '../src/pages/TeamFunds/TeamFundsPage';
|
||
|
|
import type { LedgerState } from '../src/shared';
|
||
|
|
import { fakeClient } from './helpers';
|
||
|
|
|
||
|
|
const state: LedgerState = {
|
||
|
|
mode: 'trial',
|
||
|
|
today: '2026-11-20',
|
||
|
|
user: { id: 'owner-id', name: '店主', member: 'owner', role: 'owner' },
|
||
|
|
members: [
|
||
|
|
{ member: 'owner', label: '店主' },
|
||
|
|
{ member: 'assistant', label: '助理甲' },
|
||
|
|
],
|
||
|
|
};
|
||
|
|
function report(month: string) {
|
||
|
|
return {
|
||
|
|
basis: 'orderDate',
|
||
|
|
month,
|
||
|
|
from: month + '-01',
|
||
|
|
date: month + '-20',
|
||
|
|
rows: [
|
||
|
|
{
|
||
|
|
member: 'assistant',
|
||
|
|
name: '甲',
|
||
|
|
recordCount: 1,
|
||
|
|
orderCount: 1,
|
||
|
|
afterSaleCount: 0,
|
||
|
|
draftCount: 0,
|
||
|
|
unknownCostCount: 0,
|
||
|
|
unknownProfitCount: 0,
|
||
|
|
sales: 1000,
|
||
|
|
knownTotalCost: 1105,
|
||
|
|
knownProfit: -105,
|
||
|
|
profit: -105,
|
||
|
|
incomplete: false,
|
||
|
|
shareRate: 50,
|
||
|
|
estimatedShare: -53,
|
||
|
|
},
|
||
|
|
],
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
describe('团队账目', () => {
|
||
|
|
it('只在查询后应用月份,刷新仍查询已提交的月份,并忽略过期响应', async () => {
|
||
|
|
const { client } = fakeClient({});
|
||
|
|
let finishOld!: (value: unknown) => void;
|
||
|
|
const api = vi
|
||
|
|
.spyOn(client, 'api')
|
||
|
|
.mockReturnValueOnce(
|
||
|
|
new Promise(resolve => {
|
||
|
|
finishOld = resolve;
|
||
|
|
}),
|
||
|
|
)
|
||
|
|
.mockResolvedValue(report('2026-10'));
|
||
|
|
const view = render(<TeamFundsPage client={client} state={state} />);
|
||
|
|
const month = screen.getByLabelText('月份');
|
||
|
|
expect(month).toHaveAttribute('type', 'month');
|
||
|
|
expect(month).toHaveAttribute('min', '2000-01');
|
||
|
|
expect(month).toHaveAttribute('max', '2026-11');
|
||
|
|
expect(month).toBeRequired();
|
||
|
|
expect(month).toHaveValue('2026-11');
|
||
|
|
expect(api).toHaveBeenCalledWith('/api/team-profits?month=2026-11');
|
||
|
|
fireEvent.change(month, { target: { value: '2026-10' } });
|
||
|
|
expect(api).toHaveBeenCalledTimes(1);
|
||
|
|
fireEvent.click(screen.getByRole('button', { name: '查询' }));
|
||
|
|
expect(await screen.findByText('¥-0.53')).toBeInTheDocument();
|
||
|
|
expect(screen.getByText('助理甲')).toBeInTheDocument();
|
||
|
|
await act(async () => finishOld({}));
|
||
|
|
expect(screen.queryByText(/未完整返回/)).not.toBeInTheDocument();
|
||
|
|
fireEvent.change(month, { target: { value: '2026-08' } });
|
||
|
|
fireEvent.click(screen.getByRole('button', { name: '刷新' }));
|
||
|
|
await waitFor(() => expect(api).toHaveBeenCalledTimes(3));
|
||
|
|
expect(api).toHaveBeenLastCalledWith('/api/team-profits?month=2026-10');
|
||
|
|
view.unmount();
|
||
|
|
render(<TeamFundsPage client={client} state={state} />);
|
||
|
|
expect(screen.getByLabelText('月份')).toHaveValue('2026-10');
|
||
|
|
expect(api).toHaveBeenLastCalledWith('/api/team-profits?month=2026-10');
|
||
|
|
await screen.findByText('¥-0.53');
|
||
|
|
});
|
||
|
|
it('拒绝错误分成,重新读取后区分待核算和零利润', async () => {
|
||
|
|
const valid = report('2026-11');
|
||
|
|
const { client } = fakeClient({});
|
||
|
|
const api = vi
|
||
|
|
.spyOn(client, 'api')
|
||
|
|
.mockResolvedValueOnce({ ...valid, rows: [{ ...valid.rows[0], estimatedShare: -52 }] })
|
||
|
|
.mockResolvedValueOnce({
|
||
|
|
...valid,
|
||
|
|
rows: [{ ...valid.rows[0], incomplete: true, unknownProfitCount: 1, profit: null, estimatedShare: null }],
|
||
|
|
})
|
||
|
|
.mockResolvedValueOnce({ ...valid, rows: [{ ...valid.rows[0], knownProfit: 0, profit: 0, estimatedShare: 0 }] });
|
||
|
|
render(<TeamFundsPage client={client} state={state} />);
|
||
|
|
expect(await screen.findByText('月度利润未完整返回,请重新读取。')).toBeInTheDocument();
|
||
|
|
fireEvent.click(screen.getByRole('button', { name: '重新读取' }));
|
||
|
|
expect(await screen.findByText('已核算部分 ¥-1.05 · 1 笔待补齐')).toBeInTheDocument();
|
||
|
|
expect(screen.getAllByText('待核算')).toHaveLength(2);
|
||
|
|
fireEvent.click(screen.getByRole('button', { name: '刷新' }));
|
||
|
|
expect(await screen.findAllByText('¥0.00')).toHaveLength(2);
|
||
|
|
expect(api).toHaveBeenCalledTimes(3);
|
||
|
|
});
|
||
|
|
it('店员不能读取团队月度利润', () => {
|
||
|
|
const { client, calls } = fakeClient({});
|
||
|
|
render(<TeamFundsPage client={client} state={{ ...state, user: { ...state.user, role: 'assistant' } }} />);
|
||
|
|
expect(screen.queryByRole('form')).not.toBeInTheDocument();
|
||
|
|
expect(screen.queryByLabelText('月份')).not.toBeInTheDocument();
|
||
|
|
expect(calls).toHaveLength(0);
|
||
|
|
});
|
||
|
|
});
|