Commit f6f886c1b9fe8a4bcb0bda2679e3f6a779d0afd1

Authored by zichun
1 parent d90e520a

feat(fe-login): 版本下拉预加载/空态/重试 REQ-USR-004

frontend/tests/unit/LoginPage.companies.test.tsx 0 → 100644
  1 +import { describe, it, expect, vi, beforeEach } from 'vitest';
  2 +import { screen, waitFor, within } from '@testing-library/react';
  3 +import userEvent from '@testing-library/user-event';
  4 +
  5 +vi.mock('../../src/api/usrApi', () => ({
  6 + fetchCompanies: vi.fn(),
  7 + login: vi.fn(),
  8 +}));
  9 +
  10 +import { fetchCompanies } from '../../src/api/usrApi';
  11 +import LoginPage from '../../src/pages/usr/Login/LoginPage';
  12 +import { renderWithProviders } from './renderLogin';
  13 +
  14 +const mockedFetch = fetchCompanies as unknown as ReturnType<typeof vi.fn>;
  15 +
  16 +describe('LoginPage 版本下拉状态机', () => {
  17 + beforeEach(() => {
  18 + vi.clearAllMocks();
  19 + localStorage.clear();
  20 + });
  21 +
  22 + it('shows loading placeholder and disabled select while fetching', async () => {
  23 + // 永不 resolve 的 promise → 保持 companiesLoading
  24 + mockedFetch.mockReturnValue(new Promise(() => {}));
  25 + renderWithProviders(<LoginPage />);
  26 + // loading 占位文案
  27 + expect(await screen.findByText(/加载版本中/)).toBeInTheDocument();
  28 + // Select 处禁用态
  29 + const combobox = screen.getByRole('combobox');
  30 + expect(combobox).toBeDisabled();
  31 + });
  32 +
  33 + it('renders options with label rule on resolve (idle)', async () => {
  34 + mockedFetch.mockResolvedValue([
  35 + { id: 1, sCompanyName: '甲公司', sVersion: '标准版' },
  36 + { id: 2, sCompanyName: '乙公司', sVersion: null },
  37 + ]);
  38 + renderWithProviders(<LoginPage />);
  39 + // 加载完成后 placeholder 切到「请选择版本」
  40 + expect(await screen.findByText('请选择版本')).toBeInTheDocument();
  41 + const combobox = screen.getByRole('combobox');
  42 + await waitFor(() => expect(combobox).not.toBeDisabled());
  43 + const user = userEvent.setup();
  44 + await user.click(combobox);
  45 + // 下拉项 label 规则(D8)
  46 + const listbox = await screen.findByRole('listbox');
  47 + expect(within(listbox.parentElement as HTMLElement).getByText('甲公司(标准版)')).toBeInTheDocument();
  48 + expect(within(listbox.parentElement as HTMLElement).getByText('乙公司')).toBeInTheDocument();
  49 + });
  50 +
  51 + it('auto-selects when single option', async () => {
  52 + mockedFetch.mockResolvedValue([{ id: 7, sCompanyName: '独苗公司', sVersion: '专业版' }]);
  53 + renderWithProviders(<LoginPage />);
  54 + // 单项自动选中 → 选中项 label 出现在选择框中
  55 + expect(await screen.findByText('独苗公司(专业版)')).toBeInTheDocument();
  56 + });
  57 +
  58 + it('empty state when companies is empty', async () => {
  59 + mockedFetch.mockResolvedValue([]);
  60 + renderWithProviders(<LoginPage />);
  61 + // 轻量提示
  62 + expect(await screen.findByText('未获取到可登录版本,请联系管理员')).toBeInTheDocument();
  63 + });
  64 +
  65 + it('shows error with retry when fetch fails and retry re-calls', async () => {
  66 + mockedFetch.mockRejectedValueOnce(new Error('boom'));
  67 + renderWithProviders(<LoginPage />);
  68 + expect(await screen.findByText('版本加载失败')).toBeInTheDocument();
  69 + const retry = screen.getByRole('button', { name: /点击重试/ });
  70 + // 重试时返回正常数据
  71 + mockedFetch.mockResolvedValueOnce([{ id: 1, sCompanyName: '甲公司', sVersion: '标准版' }]);
  72 + const user = userEvent.setup();
  73 + await user.click(retry);
  74 + await waitFor(() => expect(mockedFetch).toHaveBeenCalledTimes(2));
  75 + });
  76 +});