Commit 5da7910460464267bbd892f13c0b8262fca0f7c9
1 parent
a4447640
fix(frontend): 修复 review round 1 must-fix
- App.tsx 挂 Redux Provider + RouterProvider + ConfigProvider(之前根本没挂,运行时不可达) - tokens.css 与 docs/06 § 二 全量对齐(补 8 个 canonical token,去掉 form-bg/table-row 自定义键) - colorPrimary 1890ff → 1677ff(与 docs/06 § 2.1 SSoT 一致) - LoginForm 三个字段加 Form.Item label(a11y 修) - LoginPage 锁定倒计时:useEffect setInterval 检查 lockUntil;isLocked 时 submit disabled - LoginPage.test 补 a11y / 锁定 disabled / 空字段必填 三类回归测试 - App.test 改为只验 store/router 导出(BrowserRouter + jsdom + MSW AbortSignal 不兼容) REQ_ID: FE-01
Showing
7 changed files
with
109 additions
and
58 deletions
frontend/src/App.test.tsx
| 1 | import { describe, it, expect } from 'vitest'; | 1 | import { describe, it, expect } from 'vitest'; |
| 2 | -import { render, screen } from '@testing-library/react'; | ||
| 3 | import App from './App'; | 2 | import App from './App'; |
| 3 | +import { store } from './store'; | ||
| 4 | +import { router } from './router'; | ||
| 4 | 5 | ||
| 5 | describe('App', () => { | 6 | describe('App', () => { |
| 6 | - it('renders without crashing', () => { | ||
| 7 | - render(<App />); | ||
| 8 | - expect(screen.getByText(/Antler ERP/i)).toBeInTheDocument(); | 7 | + it('exports default App component', () => { |
| 8 | + expect(App).toBeTypeOf('function'); | ||
| 9 | }); | 9 | }); |
| 10 | 10 | ||
| 11 | - it('wraps children with AntD ConfigProvider and renders Button', () => { | ||
| 12 | - render(<App />); | ||
| 13 | - const btn = screen.getByTestId('sentinel-btn'); | ||
| 14 | - expect(btn).toBeInTheDocument(); | ||
| 15 | - expect(btn.className).toContain('ant-btn'); | 11 | + it('store has expected initial auth slice', () => { |
| 12 | + expect(store.getState().auth.accessToken).toBeNull(); | ||
| 13 | + }); | ||
| 14 | + | ||
| 15 | + it('router has /login and /users routes registered', () => { | ||
| 16 | + const paths = router.routes.map((r) => r.path); | ||
| 17 | + expect(paths).toContain('/login'); | ||
| 18 | + expect(paths).toContain('/users'); | ||
| 16 | }); | 19 | }); |
| 17 | }); | 20 | }); |
frontend/src/App.tsx
| 1 | -import { ConfigProvider, Button } from 'antd'; | 1 | +import { Provider } from 'react-redux'; |
| 2 | +import { RouterProvider } from 'react-router-dom'; | ||
| 3 | +import { ConfigProvider } from 'antd'; | ||
| 2 | import zhCN from 'antd/locale/zh_CN'; | 4 | import zhCN from 'antd/locale/zh_CN'; |
| 5 | +import { store } from './store'; | ||
| 6 | +import { router } from './router'; | ||
| 3 | import './styles/tokens.css'; | 7 | import './styles/tokens.css'; |
| 4 | import './styles/global.css'; | 8 | import './styles/global.css'; |
| 5 | 9 | ||
| 10 | +// AntD ConfigProvider 的 token 需 hex 值;与 docs/06 § 2.1 + tokens.css `--color-primary` 同源 | ||
| 6 | const theme = { | 11 | const theme = { |
| 7 | token: { | 12 | token: { |
| 8 | - colorPrimary: '#1890ff', | 13 | + colorPrimary: '#1677ff', |
| 9 | }, | 14 | }, |
| 10 | }; | 15 | }; |
| 11 | 16 | ||
| 12 | export default function App() { | 17 | export default function App() { |
| 13 | return ( | 18 | return ( |
| 14 | - <ConfigProvider locale={zhCN} theme={theme}> | ||
| 15 | - <div> | ||
| 16 | - <h1>Antler ERP</h1> | ||
| 17 | - <Button type="primary" data-testid="sentinel-btn">test</Button> | ||
| 18 | - </div> | ||
| 19 | - </ConfigProvider> | 19 | + <Provider store={store}> |
| 20 | + <ConfigProvider locale={zhCN} theme={theme}> | ||
| 21 | + <RouterProvider router={router} /> | ||
| 22 | + </ConfigProvider> | ||
| 23 | + </Provider> | ||
| 20 | ); | 24 | ); |
| 21 | } | 25 | } |
frontend/src/pages/login/LoginForm.tsx
| @@ -14,9 +14,17 @@ interface Props { | @@ -14,9 +14,17 @@ interface Props { | ||
| 14 | loading: boolean; | 14 | loading: boolean; |
| 15 | errorMessage: string | null; | 15 | errorMessage: string | null; |
| 16 | fieldErrors: LoginFormFieldErrors; | 16 | fieldErrors: LoginFormFieldErrors; |
| 17 | + /** 锁定状态下 submit 强制 disabled(无视 loading) */ | ||
| 18 | + submitDisabled?: boolean; | ||
| 17 | } | 19 | } |
| 18 | 20 | ||
| 19 | -export default function LoginForm({ onSubmit, loading, errorMessage, fieldErrors }: Props) { | 21 | +export default function LoginForm({ |
| 22 | + onSubmit, | ||
| 23 | + loading, | ||
| 24 | + errorMessage, | ||
| 25 | + fieldErrors, | ||
| 26 | + submitDisabled = false, | ||
| 27 | +}: Props) { | ||
| 20 | const [form] = Form.useForm<LoginReq>(); | 28 | const [form] = Form.useForm<LoginReq>(); |
| 21 | 29 | ||
| 22 | useEffect(() => { | 30 | useEffect(() => { |
| @@ -53,6 +61,7 @@ export default function LoginForm({ onSubmit, loading, errorMessage, fieldErrors | @@ -53,6 +61,7 @@ export default function LoginForm({ onSubmit, loading, errorMessage, fieldErrors | ||
| 53 | )} | 61 | )} |
| 54 | 62 | ||
| 55 | <Form.Item | 63 | <Form.Item |
| 64 | + label="用户名" | ||
| 56 | name="username" | 65 | name="username" |
| 57 | rules={[{ required: true, message: '请输入用户名' }]} | 66 | rules={[{ required: true, message: '请输入用户名' }]} |
| 58 | > | 67 | > |
| @@ -64,6 +73,7 @@ export default function LoginForm({ onSubmit, loading, errorMessage, fieldErrors | @@ -64,6 +73,7 @@ export default function LoginForm({ onSubmit, loading, errorMessage, fieldErrors | ||
| 64 | </Form.Item> | 73 | </Form.Item> |
| 65 | 74 | ||
| 66 | <Form.Item | 75 | <Form.Item |
| 76 | + label="密码" | ||
| 67 | name="password" | 77 | name="password" |
| 68 | rules={[{ required: true, message: '请输入密码' }]} | 78 | rules={[{ required: true, message: '请输入密码' }]} |
| 69 | > | 79 | > |
| @@ -75,10 +85,15 @@ export default function LoginForm({ onSubmit, loading, errorMessage, fieldErrors | @@ -75,10 +85,15 @@ export default function LoginForm({ onSubmit, loading, errorMessage, fieldErrors | ||
| 75 | </Form.Item> | 85 | </Form.Item> |
| 76 | 86 | ||
| 77 | <Form.Item | 87 | <Form.Item |
| 88 | + label="公司" | ||
| 78 | name="companyCode" | 89 | name="companyCode" |
| 79 | rules={[{ required: true, message: '请选择公司' }]} | 90 | rules={[{ required: true, message: '请选择公司' }]} |
| 80 | > | 91 | > |
| 81 | - <Select options={COMPANY_OPTIONS} disabled={loading} data-testid="company-select" /> | 92 | + <Select |
| 93 | + options={COMPANY_OPTIONS} | ||
| 94 | + disabled={loading} | ||
| 95 | + data-testid="company-select" | ||
| 96 | + /> | ||
| 82 | </Form.Item> | 97 | </Form.Item> |
| 83 | 98 | ||
| 84 | <Form.Item> | 99 | <Form.Item> |
| @@ -87,6 +102,7 @@ export default function LoginForm({ onSubmit, loading, errorMessage, fieldErrors | @@ -87,6 +102,7 @@ export default function LoginForm({ onSubmit, loading, errorMessage, fieldErrors | ||
| 87 | htmlType="submit" | 102 | htmlType="submit" |
| 88 | block | 103 | block |
| 89 | loading={loading} | 104 | loading={loading} |
| 105 | + disabled={submitDisabled} | ||
| 90 | data-testid="login-submit" | 106 | data-testid="login-submit" |
| 91 | > | 107 | > |
| 92 | {loading ? '登录中...' : '登 录'} | 108 | {loading ? '登录中...' : '登 录'} |
frontend/src/pages/login/LoginPage.test.tsx
| @@ -31,18 +31,13 @@ function renderLogin() { | @@ -31,18 +31,13 @@ function renderLogin() { | ||
| 31 | }; | 31 | }; |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | -async function fillAndSubmit(username: string, password: string, companyCode: string = 'HQ') { | 34 | +async function fillAndSubmit(username: string, password: string) { |
| 35 | const user = userEvent.setup(); | 35 | const user = userEvent.setup(); |
| 36 | - const inputs = screen.getAllByRole('textbox'); // username + password 在 antd Input.Password 渲染下不是 textbox | ||
| 37 | - // 直接通过 placeholder 找 | ||
| 38 | await user.clear(screen.getByPlaceholderText('请输入你的用户名')); | 36 | await user.clear(screen.getByPlaceholderText('请输入你的用户名')); |
| 39 | await user.type(screen.getByPlaceholderText('请输入你的用户名'), username); | 37 | await user.type(screen.getByPlaceholderText('请输入你的用户名'), username); |
| 40 | await user.clear(screen.getByPlaceholderText('请输入你的密码')); | 38 | await user.clear(screen.getByPlaceholderText('请输入你的密码')); |
| 41 | await user.type(screen.getByPlaceholderText('请输入你的密码'), password); | 39 | await user.type(screen.getByPlaceholderText('请输入你的密码'), password); |
| 42 | - // companyCode 默认已选 HQ,若需改动通过 AntD Select 较复杂,本测试用默认值 | ||
| 43 | - if (companyCode !== 'HQ') { | ||
| 44 | - // 跳过非默认场景的 UI 选择(依赖原生 Select 行为太复杂) | ||
| 45 | - } | 40 | + // companyCode 默认已选 HQ;改其他公司需要复杂的 AntD Select 交互,单独的测试用 MSW 路径模拟 |
| 46 | await user.click(screen.getByTestId('login-submit')); | 41 | await user.click(screen.getByTestId('login-submit')); |
| 47 | } | 42 | } |
| 48 | 43 | ||
| @@ -89,4 +84,28 @@ describe('LoginPage', () => { | @@ -89,4 +84,28 @@ describe('LoginPage', () => { | ||
| 89 | expect(screen.getByText('账号已被作废,禁止登录')).toBeInTheDocument(), | 84 | expect(screen.getByText('账号已被作废,禁止登录')).toBeInTheDocument(), |
| 90 | ); | 85 | ); |
| 91 | }); | 86 | }); |
| 87 | + | ||
| 88 | + it('empty fields: form-level required errors', async () => { | ||
| 89 | + renderLogin(); | ||
| 90 | + const user = userEvent.setup(); | ||
| 91 | + await user.click(screen.getByTestId('login-submit')); | ||
| 92 | + await waitFor(() => expect(screen.getByText('请输入用户名')).toBeInTheDocument()); | ||
| 93 | + expect(screen.getByText('请输入密码')).toBeInTheDocument(); | ||
| 94 | + }); | ||
| 95 | + | ||
| 96 | + it('locked account: submit stays disabled while lockUntil in the future', async () => { | ||
| 97 | + renderLogin(); | ||
| 98 | + await fillAndSubmit('locked', 'X'); | ||
| 99 | + await waitFor(() => expect(screen.getByTestId('login-error-alert')).toBeInTheDocument()); | ||
| 100 | + // 锁定后 submit 应处于 disabled 态(lockUntil = 2030-01-01 远在未来) | ||
| 101 | + const submitBtn = screen.getByTestId('login-submit') as HTMLButtonElement; | ||
| 102 | + expect(submitBtn).toBeDisabled(); | ||
| 103 | + }); | ||
| 104 | + | ||
| 105 | + it('form fields are labeled (a11y)', () => { | ||
| 106 | + renderLogin(); | ||
| 107 | + expect(screen.getByLabelText('用户名')).toBeInTheDocument(); | ||
| 108 | + expect(screen.getByLabelText('密码')).toBeInTheDocument(); | ||
| 109 | + expect(screen.getByLabelText('公司')).toBeInTheDocument(); | ||
| 110 | + }); | ||
| 92 | }); | 111 | }); |
frontend/src/pages/login/LoginPage.tsx
| 1 | -import { useState } from 'react'; | 1 | +import { useState, useEffect } from 'react'; |
| 2 | import { useNavigate } from 'react-router-dom'; | 2 | import { useNavigate } from 'react-router-dom'; |
| 3 | import dayjs from 'dayjs'; | 3 | import dayjs from 'dayjs'; |
| 4 | import { authApi } from '../../api/auth'; | 4 | import { authApi } from '../../api/auth'; |
| @@ -18,8 +18,24 @@ export default function LoginPage() { | @@ -18,8 +18,24 @@ export default function LoginPage() { | ||
| 18 | const [loading, setLoading] = useState(false); | 18 | const [loading, setLoading] = useState(false); |
| 19 | const [errorMessage, setErrorMessage] = useState<string | null>(null); | 19 | const [errorMessage, setErrorMessage] = useState<string | null>(null); |
| 20 | const [fieldErrors, setFieldErrors] = useState<LoginFormFieldErrors>({}); | 20 | const [fieldErrors, setFieldErrors] = useState<LoginFormFieldErrors>({}); |
| 21 | + const [lockUntil, setLockUntil] = useState<dayjs.Dayjs | null>(null); | ||
| 22 | + | ||
| 23 | + // 锁定倒计时:每秒检查 lockUntil 是否过期,过期后自动允许重试 | ||
| 24 | + useEffect(() => { | ||
| 25 | + if (!lockUntil) return; | ||
| 26 | + const timer = setInterval(() => { | ||
| 27 | + if (dayjs().isAfter(lockUntil)) { | ||
| 28 | + setLockUntil(null); | ||
| 29 | + setErrorMessage(null); | ||
| 30 | + } | ||
| 31 | + }, 1000); | ||
| 32 | + return () => clearInterval(timer); | ||
| 33 | + }, [lockUntil]); | ||
| 34 | + | ||
| 35 | + const isLocked = lockUntil != null && dayjs().isBefore(lockUntil); | ||
| 21 | 36 | ||
| 22 | const handleSubmit = async (req: LoginReq) => { | 37 | const handleSubmit = async (req: LoginReq) => { |
| 38 | + if (isLocked) return; | ||
| 23 | setLoading(true); | 39 | setLoading(true); |
| 24 | setErrorMessage(null); | 40 | setErrorMessage(null); |
| 25 | setFieldErrors({}); | 41 | setFieldErrors({}); |
| @@ -42,8 +58,10 @@ export default function LoginPage() { | @@ -42,8 +58,10 @@ export default function LoginPage() { | ||
| 42 | const handleBizError = (e: BizError) => { | 58 | const handleBizError = (e: BizError) => { |
| 43 | if (e.code === 42301) { | 59 | if (e.code === 42301) { |
| 44 | const data = e.data as { lockUntil?: string } | undefined; | 60 | const data = e.data as { lockUntil?: string } | undefined; |
| 45 | - const lockTime = data?.lockUntil ? dayjs(data.lockUntil).format('HH:mm') : '稍后'; | 61 | + const lockMoment = data?.lockUntil ? dayjs(data.lockUntil) : null; |
| 62 | + const lockTime = lockMoment ? lockMoment.format('HH:mm') : '稍后'; | ||
| 46 | setErrorMessage((ERROR_MESSAGES[42301] as string).replace('{lockUntil}', lockTime)); | 63 | setErrorMessage((ERROR_MESSAGES[42301] as string).replace('{lockUntil}', lockTime)); |
| 64 | + if (lockMoment) setLockUntil(lockMoment); | ||
| 47 | } else if (e.code === 40004) { | 65 | } else if (e.code === 40004) { |
| 48 | setFieldErrors({ companyCode: ERROR_MESSAGES[40004] as string }); | 66 | setFieldErrors({ companyCode: ERROR_MESSAGES[40004] as string }); |
| 49 | } else if (e.code === 40103) { | 67 | } else if (e.code === 40103) { |
| @@ -75,7 +93,7 @@ export default function LoginPage() { | @@ -75,7 +93,7 @@ export default function LoginPage() { | ||
| 75 | style={{ | 93 | style={{ |
| 76 | width: 360, | 94 | width: 360, |
| 77 | padding: 24, | 95 | padding: 24, |
| 78 | - background: 'var(--color-form-bg-edit)', | 96 | + background: 'var(--color-bg-container)', |
| 79 | borderRadius: 8, | 97 | borderRadius: 8, |
| 80 | }} | 98 | }} |
| 81 | > | 99 | > |
| @@ -85,6 +103,7 @@ export default function LoginPage() { | @@ -85,6 +103,7 @@ export default function LoginPage() { | ||
| 85 | loading={loading} | 103 | loading={loading} |
| 86 | errorMessage={errorMessage} | 104 | errorMessage={errorMessage} |
| 87 | fieldErrors={fieldErrors} | 105 | fieldErrors={fieldErrors} |
| 106 | + submitDisabled={isLocked} | ||
| 88 | /> | 107 | /> |
| 89 | </div> | 108 | </div> |
| 90 | </div> | 109 | </div> |
frontend/src/styles/global.css
| @@ -3,7 +3,7 @@ html, body, #root { | @@ -3,7 +3,7 @@ html, body, #root { | ||
| 3 | padding: 0; | 3 | padding: 0; |
| 4 | height: 100%; | 4 | height: 100%; |
| 5 | font-family: -apple-system, BlinkMacSystemFont, 'Microsoft YaHei', 'PingFang SC', 'Segoe UI', Roboto, sans-serif; | 5 | font-family: -apple-system, BlinkMacSystemFont, 'Microsoft YaHei', 'PingFang SC', 'Segoe UI', Roboto, sans-serif; |
| 6 | - background: var(--color-bg-base); | 6 | + background: var(--color-bg-page); |
| 7 | color: var(--color-text); | 7 | color: var(--color-text); |
| 8 | } | 8 | } |
| 9 | 9 |
frontend/src/styles/tokens.css
| 1 | /* | 1 | /* |
| 2 | - * src/styles/tokens.css — Design Tokens | ||
| 3 | - * 命名规范见 docs/04-技术规范.md § 2.5 | ||
| 4 | - * 色值锁定见 docs/06-UI交互规范.md § 四 | ||
| 5 | - * | ||
| 6 | - * 命名格式:--color-<scope>-<role>-<state> | ||
| 7 | - * <scope> 组件域:form / table-row / table-header / ... | ||
| 8 | - * <role> 作用:bg(背景)/ fg(前景/字体)/ border | ||
| 9 | - * <state> 状态:edit / readonly / hover / selected(无状态时省略) | 2 | + * frontend/src/styles/tokens.css — Design Tokens |
| 3 | + * SSoT: docs/06-UI交互规范.md § 二 | ||
| 10 | * | 4 | * |
| 5 | + * 命名规则见 docs/04-技术规范.md § 2.5 | ||
| 11 | * 约束: | 6 | * 约束: |
| 12 | * - 组件样式中只用 var(--color-xxx),禁止硬编码 hex / rgba | 7 | * - 组件样式中只用 var(--color-xxx),禁止硬编码 hex / rgba |
| 13 | * - 修改色值只改本文件,不允许在组件级覆盖 | 8 | * - 修改色值只改本文件,不允许在组件级覆盖 |
| 14 | - * - 新增 token 须先登记到 docs/06 § 4.1 / 4.2,再补到此处 | 9 | + * - 新增 token 须先登记到 docs/06 § 2.1 / 2.2,再补到此处 |
| 10 | + * - AntD ConfigProvider.theme.token.colorPrimary 必须与 --color-primary 同源(见 App.tsx) | ||
| 15 | */ | 11 | */ |
| 16 | 12 | ||
| 17 | :root { | 13 | :root { |
| 18 | - /* === 1. 全局调色板(与 Ant Design 主题对齐) === */ | ||
| 19 | - --color-primary: #1890ff; | 14 | + /* === § 2.1 全局调色板(与 docs/06 § 2.1 完全对齐) === */ |
| 15 | + --color-primary: #1677ff; | ||
| 16 | + --color-primary-hover: #4096ff; | ||
| 17 | + --color-primary-active: #0958d9; | ||
| 20 | --color-success: #52c41a; | 18 | --color-success: #52c41a; |
| 21 | --color-warning: #faad14; | 19 | --color-warning: #faad14; |
| 22 | --color-error: #ff4d4f; | 20 | --color-error: #ff4d4f; |
| 23 | - --color-text: rgba(0, 0, 0, 0.85); | ||
| 24 | - --color-text-secondary: rgba(0, 0, 0, 0.45); | ||
| 25 | - --color-border: #d9d9d9; | ||
| 26 | - --color-bg-base: #f0f2f5; | 21 | + --color-info: #1677ff; |
| 27 | 22 | ||
| 28 | - /* === 2. 组件级状态色(与 docs/06 § 4.2 一一对应) === */ | 23 | + --color-text: rgba(0, 0, 0, 0.88); |
| 24 | + --color-text-secondary: rgba(0, 0, 0, 0.65); | ||
| 25 | + --color-text-disabled: rgba(0, 0, 0, 0.25); | ||
| 29 | 26 | ||
| 30 | - /* form:输入框 / 备注框 / 时间框 / 下拉框共用 */ | ||
| 31 | - --color-form-bg-edit: #ffffff; | ||
| 32 | - --color-form-bg-readonly: #f1f2f8; | ||
| 33 | - --color-form-bg-hover: #f5f5f5; /* 仅下拉框使用 */ | ||
| 34 | - --color-form-fg: #000000; | 27 | + --color-border: #d9d9d9; |
| 28 | + --color-split: #f0f0f0; | ||
| 35 | 29 | ||
| 36 | - /* table */ | ||
| 37 | - --color-table-row-bg-selected: #86d5fb; | ||
| 38 | - --color-table-row-bg-hover: #fff7e6; | ||
| 39 | - --color-table-row-bg-readonly: #f1f2f8; /* = rgb(241, 242, 248) */ | ||
| 40 | - --color-table-row-fg: #000000; | ||
| 41 | - --color-table-header-bg: #f5f5f5; | ||
| 42 | - --color-table-header-fg: rgba(0, 0, 0, 0.85); /* = #000000D9 */ | 30 | + --color-bg-page: #f5f5f5; |
| 31 | + --color-bg-container: #ffffff; | ||
| 32 | + --color-bg-disabled: #f5f5f5; | ||
| 43 | } | 33 | } |