Files
weiguang-jewelry-ledger/新账系统/web/test/shop-hours.test.tsx
T

73 lines
2.7 KiB
TypeScript

import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { afterEach, expect, it, vi } from 'vitest';
import { ShopForm } from '../src/pages/Settings/Shop';
import { fakeClient } from './helpers';
afterEach(cleanup);
it('营业时间使用原始时间字符串,保存关店设置后才通知页面', async () => {
const shop = {
revision: 1,
storeName: '测试店铺',
receiptSubtitle: '',
contact: '',
businessHours: { open: '09:00', close: '18:00' },
creditCardRatePermille: 6,
profitSharePercent: 30,
profitShareEffectiveMonth: '2026-12',
feeDefaults: { certificate: '4', packaging: '3', shipping: '6', packagingPayer: 'owner' },
members: [{ member: 'owner', label: '店主' }],
};
const { client, calls } = fakeClient({ '/api/shop-settings': () => ({ shopSettings: shop }) });
client.setUser('owner');
const changed = vi.fn(),
notice = vi.fn();
const view = render(
<ShopForm
client={client}
shop={shop}
today="2026-11-15"
members={shop.members}
team
busy={false}
dirty={false}
onDirty={vi.fn()}
onBusy={vi.fn()}
onSaved={vi.fn()}
notice={notice}
/>,
);
window.addEventListener('ledger-business-hours-changed', changed);
try {
const open = () => screen.getByLabelText('开始时间');
const close = () => screen.getByLabelText('结束时间');
const submit = () => fireEvent.submit(view.container.querySelector('form')!);
expect(open()).toHaveValue('09:00');
expect(close()).toHaveValue('18:00');
submit();
await waitFor(() => expect(notice).toHaveBeenCalledWith('店铺配置已保存'));
expect(calls[0].body).not.toHaveProperty('businessHours');
expect(changed).not.toHaveBeenCalled();
fireEvent.change(open(), { target: { value: '18:00' } });
submit();
expect(await screen.findByText('开始和结束时间不能相同')).toBeInTheDocument();
fireEvent.change(close(), { target: { value: '' } });
submit();
expect(await screen.findByText('请完整填写开始和结束时间')).toBeInTheDocument();
expect(calls).toHaveLength(1);
fireEvent.change(close(), { target: { value: '02:00' } });
submit();
await waitFor(() => expect(changed).toHaveBeenCalledTimes(1));
expect(calls[1].body).toHaveProperty('businessHours', { open: '18:00', close: '02:00' });
fireEvent.change(open(), { target: { value: '' } });
fireEvent.change(close(), { target: { value: '' } });
submit();
await waitFor(() => expect(changed).toHaveBeenCalledTimes(2));
expect(calls[2].body).toHaveProperty('businessHours', null);
} finally {
window.removeEventListener('ledger-business-hours-changed', changed);
}
});