85 lines
4.0 KiB
TypeScript
85 lines
4.0 KiB
TypeScript
// 私域运营与人数登记共用同一个有版本号的写入口。
|
|||
|
|
import { App as AntApp } from 'antd';
|
||
|
|
import { fireEvent, render, screen, within } from '@testing-library/react';
|
||
|
|
import { beforeAll, beforeEach, expect, it, vi } from 'vitest';
|
||
|
|
import { PrivateOperationsPage } from '../src/pages/PrivateOperations/PrivateOperationsPage';
|
||
|
|
import type { LedgerState } from '../src/shared';
|
||
|
|
import { fakeClient } from './helpers';
|
||
|
|
|
||
|
|
const today = '2026-11-20';
|
||
|
|
const members = [
|
||
|
|
{ member: 'owner', label: '麦麦' },
|
||
|
|
{ member: 'assistant', label: '娜娜' },
|
||
|
|
{ member: 'prior', label: '前员工', enabled: false },
|
||
|
|
];
|
||
|
|
function setup(role: 'owner' | 'assistant') {
|
||
|
|
const member = role === 'owner' ? 'owner' : 'assistant';
|
||
|
|
const state: LedgerState = { user: { id: role, member, role, name: role }, members, today, mode: 'trial' };
|
||
|
|
const { client, calls } = fakeClient({
|
||
|
|
'/api/private-operations': () => ({ from: today, date: today, days: 1, members, rows: [], summaries: [] }),
|
||
|
|
'/api/private-operations/people-history': () => ({ member, rows: [] }),
|
||
|
|
'/api/private-operations/people': () => ({
|
||
|
|
member: 'assistant',
|
||
|
|
date: today,
|
||
|
|
total_count: 0,
|
||
|
|
previous: { date: '2026-11-16', total: 10 },
|
||
|
|
revision: 7,
|
||
|
|
}),
|
||
|
|
});
|
||
|
|
client.setUser(role);
|
||
|
|
render(
|
||
|
|
<AntApp>
|
||
|
|
<PrivateOperationsPage client={client} state={state} />
|
||
|
|
</AntApp>,
|
||
|
|
);
|
||
|
|
return { client, calls };
|
||
|
|
}
|
||
|
|
// jsdom 尚未实现 HTMLDialogElement 的模态开启和关闭。
|
||
|
|
beforeAll(() => {
|
||
|
|
if (!HTMLDialogElement.prototype.showModal)
|
||
|
|
HTMLDialogElement.prototype.showModal = function () {
|
||
|
|
this.open = true;
|
||
|
|
};
|
||
|
|
if (!HTMLDialogElement.prototype.close)
|
||
|
|
HTMLDialogElement.prototype.close = function () {
|
||
|
|
this.open = false;
|
||
|
|
};
|
||
|
|
});
|
||
|
|
beforeEach(() => window.localStorage.clear());
|
||
|
|
|
||
|
|
it('店主查询默认日期、包括停用员工,保存 0 人时保留版本与字符串输入', async () => {
|
||
|
|
const { calls } = setup('owner');
|
||
|
|
await screen.findByText('所选日期暂无人数记录。');
|
||
|
|
expect(calls[0].url).toBe('/api/private-operations?from=2026-11-20&date=2026-11-20&member=');
|
||
|
|
expect(screen.getByRole('option', { name: '前员工(已停用)' })).toHaveValue('prior');
|
||
|
|
fireEvent.click(screen.getAllByRole('button', { name: '记录人数' })[0]);
|
||
|
|
const dialog = within(await screen.findByRole('dialog'));
|
||
|
|
const count = await dialog.findByRole('spinbutton', { name: '当天总人数(必填)' });
|
||
|
|
expect(count).toHaveValue(0);
|
||
|
|
expect(dialog.getByText('-10 人')).toBeVisible();
|
||
|
|
expect(dialog.getByRole('combobox')).toHaveValue('assistant');
|
||
|
|
fireEvent.click(dialog.getByRole('button', { name: '保存人数' }));
|
||
|
|
await screen.findByText('人数已保存');
|
||
|
|
const save = calls.find(call => call.method === 'POST')!;
|
||
|
|
expect(save.body).toEqual({ date: today, member: 'assistant', total_count: '0', revision: 7 });
|
||
|
|
expect(save.headers['Idempotency-Key']).toBeTruthy();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('店员仅查本人历史;遇到修改冲突提供重读入口并读取当前版本', async () => {
|
||
|
|
const { calls, client } = setup('assistant');
|
||
|
|
await screen.findByText('还没有人数记录。');
|
||
|
|
expect(calls[0].url).toBe('/api/private-operations/people-history');
|
||
|
|
fireEvent.click(screen.getByRole('button', { name: '记录人数' }));
|
||
|
|
const dialog = within(await screen.findByRole('dialog'));
|
||
|
|
await dialog.findByRole('spinbutton');
|
||
|
|
expect(dialog.queryByRole('combobox')).toBeNull();
|
||
|
|
const conflict = Object.assign(new Error('人数已被更新,请重新读取后保存'), { status: 409 });
|
||
|
|
vi.spyOn(client, 'mutate').mockRejectedValueOnce(conflict);
|
||
|
|
fireEvent.click(dialog.getByRole('button', { name: '保存人数' }));
|
||
|
|
fireEvent.click(await dialog.findByRole('button', { name: '重新读取后核对' }));
|
||
|
|
await dialog.findByRole('spinbutton');
|
||
|
|
expect(
|
||
|
|
calls.filter(call => call.url === '/api/private-operations/people?member=assistant&date=2026-11-20'),
|
||
|
|
).toHaveLength(2);
|
||
|
|
expect(calls.some(call => call.url.startsWith('/api/private-operations?'))).toBe(false);
|
||
|
|
});
|