共用计算经构建插件直接导入 public/ 下的 UMD 源文件;请求客户端沿用旧界面的重试、 同一操作号自动确认、恢复原操作与账号切换约定;附 vitest 单元与组件测试。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
75 lines
4.1 KiB
TypeScript
75 lines
4.1 KiB
TypeScript
// 登录页三种入口:演示一键进入、员工下拉加密码、试用空库初始化。
|
|
import { describe, expect, it, vi } from 'vitest';
|
|
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
import { App as AntApp } from 'antd';
|
|
import { LoginPage } from '../src/pages/Login/LoginPage';
|
|
import type { SessionInfo } from '../src/shared';
|
|
import { fakeClient } from './helpers';
|
|
|
|
const base: SessionInfo = { user: null, setup: false, mode: 'trial', today: '2026-11-20', brand: { storeName: '对照珠宝' } };
|
|
const show = (session: SessionInfo, client: ReturnType<typeof fakeClient>['client'], onLoggedIn = vi.fn()) =>
|
|
render(
|
|
<AntApp>
|
|
<LoginPage client={client} session={session} onLoggedIn={onLoggedIn} />
|
|
</AntApp>,
|
|
);
|
|
|
|
describe('登录页', () => {
|
|
it('演示模式:每位成员一个进入按钮,点击后 POST /api/demo-login', async () => {
|
|
const { client, calls } = fakeClient({ '/api/demo-login': () => ({ user: {} }) });
|
|
const onLoggedIn = vi.fn();
|
|
show(
|
|
{ ...base, mode: 'demo', loginOptions: [{ member: 'owner', label: '麦麦' }, { member: 'assistant', label: '娜娜' }] },
|
|
client,
|
|
onLoggedIn,
|
|
);
|
|
expect(screen.getByText('对照珠宝 · 新账系统')).toBeInTheDocument();
|
|
fireEvent.click(screen.getByRole('button', { name: '以娜娜身份进入演示' }));
|
|
await waitFor(() => expect(onLoggedIn).toHaveBeenCalled());
|
|
expect(calls[0]).toMatchObject({ url: '/api/demo-login', method: 'POST', body: { member: 'assistant' } });
|
|
expect(calls[0].headers['Idempotency-Key']).toBeUndefined();
|
|
expect(screen.getByRole('link', { name: '返回旧版界面' })).toHaveAttribute('href', '/');
|
|
});
|
|
it('密码登录:选择账号与密码后提交;错误密码显示服务端提示', async () => {
|
|
let attempts = 0;
|
|
const { client, calls } = fakeClient({
|
|
'/api/login': () => {
|
|
if (++attempts === 1) throw new Error('账号或密码不正确');
|
|
return { user: {} };
|
|
},
|
|
});
|
|
const onLoggedIn = vi.fn();
|
|
const { container } = show({ ...base, loginOptions: [{ member: 'owner', label: '麦麦' }] }, client, onLoggedIn);
|
|
fireEvent.click(screen.getByRole('button', { name: /登\s*录/ }));
|
|
expect(await screen.findByText('请选择登录账号')).toBeInTheDocument();
|
|
fireEvent.mouseDown(container.querySelector('.ant-select-selector')!);
|
|
fireEvent.click(await screen.findByTitle('麦麦'));
|
|
fireEvent.change(screen.getByLabelText('密码'), { target: { value: 'wrong-password-1' } });
|
|
fireEvent.click(screen.getByRole('button', { name: /登\s*录/ }));
|
|
expect(await screen.findByText('账号或密码不正确')).toBeInTheDocument();
|
|
fireEvent.change(screen.getByLabelText('密码'), { target: { value: 'right-password-1' } });
|
|
fireEvent.click(screen.getByRole('button', { name: /登\s*录/ }));
|
|
await waitFor(() => expect(onLoggedIn).toHaveBeenCalledTimes(1));
|
|
expect(calls.at(-1)?.body).toEqual({ member: 'owner', password: 'right-password-1' });
|
|
});
|
|
it('试用空库:初始化表单校验密码长度,提交店铺与店主资料', async () => {
|
|
const { client, calls } = fakeClient({ '/api/setup': () => ({ user: {} }) });
|
|
const onLoggedIn = vi.fn();
|
|
show({ ...base, setup: true }, client, onLoggedIn);
|
|
fireEvent.change(screen.getByLabelText('店铺名称'), { target: { value: '新店' } });
|
|
fireEvent.change(screen.getByLabelText('店主密码'), { target: { value: 'short' } });
|
|
fireEvent.click(screen.getByRole('button', { name: /建立试用账号/ }));
|
|
expect(await screen.findByText('店主密码至少12个字符')).toBeInTheDocument();
|
|
expect(calls).toHaveLength(0);
|
|
fireEvent.change(screen.getByLabelText('店主密码'), { target: { value: 'long-enough-pass' } });
|
|
fireEvent.click(screen.getByRole('button', { name: /建立试用账号/ }));
|
|
await waitFor(() => expect(onLoggedIn).toHaveBeenCalled());
|
|
expect(calls[0].body).toEqual({
|
|
storeName: '新店',
|
|
ownerLabel: '店主',
|
|
ownerLoginName: 'owner',
|
|
ownerPassword: 'long-enough-pass',
|
|
});
|
|
});
|
|
});
|