ComponentsDemoPage.test.tsx 6.75 KB
import { fireEvent, render, screen } from '@testing-library/react';
import { describe, expect, it } from 'vitest';

import { ComponentsDemoPage } from './ComponentsDemoPage';
import uiStyles from '../../../packages/ui/src/components/Shared.module.css';

describe('ComponentsDemoPage', () => {
  it('shows all phase-one component groups including Upload', () => {
    render(<ComponentsDemoPage />);

    expect(screen.getByRole('heading', { name: '组件库 Demo' })).toBeInTheDocument();
    expect(screen.getByText('基础交互')).toBeInTheDocument();
    expect(screen.getByRole('heading', { name: '选择与日期' })).toBeInTheDocument();
    expect(screen.getByRole('heading', { name: '表格与分页' })).toBeInTheDocument();
    expect(screen.getByRole('heading', { name: '反馈与浮层' })).toBeInTheDocument();
    expect(screen.getByRole('heading', { name: '数据展示' })).toBeInTheDocument();
    expect(screen.getByRole('heading', { name: '导航与占位' })).toBeInTheDocument();
    expect(screen.getByRole('heading', { name: '颜色能力' })).toBeInTheDocument();
    expect(screen.getByRole('heading', { name: '上传' })).toBeInTheDocument();
    expect(screen.getByText('暂无审批记录')).toBeInTheDocument();
    expect(screen.getByRole('button', { name: '新建审批' })).toBeInTheDocument();
    expect(screen.getByRole('alert', { name: '加载失败' })).toBeInTheDocument();
    expect(screen.getByRole('status', { name: '暂无数据' })).toBeInTheDocument();
    expect(screen.getByRole('status', { name: '未找到匹配结果' })).toBeInTheDocument();
    expect(screen.getByRole('alert', { name: '权限受限' })).toBeInTheDocument();
    expect(screen.getByRole('alert', { name: '网络不可用' })).toBeInTheDocument();
    expect(screen.getByText('点击或拖拽业务资料到此上传')).toBeInTheDocument();
    expect(screen.getByText('4/20')).toBeInTheDocument();
    expect(screen.getByText('4/120')).toBeInTheDocument();
  });

  it('keeps basic component demos interactive for manual review', () => {
    render(<ComponentsDemoPage />);

    fireEvent.click(screen.getByLabelText('Demo 启用'));
    fireEvent.click(screen.getByRole('switch', { name: 'Demo 启用审批' }));

    expect(screen.getByLabelText('Demo 启用')).not.toBeChecked();
    expect(screen.getByRole('switch', { name: 'Demo 启用审批' })).toHaveAttribute('aria-checked', 'false');
  });

  it('shows every Button type, size, and core state in the demo', () => {
    render(<ComponentsDemoPage />);

    expect(screen.getByRole('button', { name: '主按钮' })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: '普通按钮' })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: '描边按钮' })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: '文字按钮' })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: '危险按钮' })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: '小按钮' })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: '中按钮' })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: '大按钮' })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: '图标按钮' })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: '禁用按钮' })).toBeDisabled();
    expect(screen.getByRole('button', { name: '加载中' })).toHaveAttribute('aria-busy', 'true');
  });

  it('shows Message in the feedback demo after clicking the trigger', () => {
    render(<ComponentsDemoPage />);

    fireEvent.click(screen.getByRole('button', { name: '展示消息提示' }));

    expect(screen.getByRole('region', { name: '全局消息提示' })).toBeInTheDocument();
    expect(screen.getByRole('status', { name: '保存成功' })).toBeInTheDocument();
    expect(screen.getByRole('status', { name: '请补充审批人' })).toBeInTheDocument();
    expect(screen.getByRole('alert', { name: '删除失败' })).toBeInTheDocument();
    expect(screen.getByRole('img', { name: '成功提示' })).toBeInTheDocument();
    expect(screen.getByRole('img', { name: '警告提示' })).toBeInTheDocument();
    expect(screen.getByRole('img', { name: '错误提示' })).toBeInTheDocument();
  });

  it('closes the feedback demo Modal when clicking cancel', () => {
    render(<ComponentsDemoPage />);

    fireEvent.click(screen.getByRole('button', { name: '取消' }));

    expect(screen.queryByRole('dialog', { name: '编辑项目' })).not.toBeInTheDocument();
  });

  it('closes the feedback demo Modal when clicking the close button', () => {
    render(<ComponentsDemoPage />);

    fireEvent.click(screen.getByRole('button', { name: '关闭编辑项目' }));

    expect(screen.queryByRole('dialog', { name: '编辑项目' })).not.toBeInTheDocument();
  });

  it('shows Form validation and horizontal FormItem layout in the feedback demo', () => {
    render(<ComponentsDemoPage />);

    fireEvent.click(screen.getByRole('button', { name: '提交表单' }));

    expect(screen.getByRole('alert', { name: '请输入项目名称' })).toBeInTheDocument();
    expect(screen.getByTestId('form-root')).toHaveClass(uiStyles.formColumnsOne);
    expect(screen.getByTestId('form-item-项目名称')).toHaveClass(uiStyles.formItemHorizontal);
    expect(screen.getByTestId('form-item-所属岗位')).toHaveClass(uiStyles.formItemSpanOne);
    expect(screen.getByTestId('form-item-备注')).toHaveClass(uiStyles.formItemSpanTwo);
    expect(screen.getByTestId('form-main-项目名称')).toHaveClass(uiStyles.formMain);
    expect(screen.getByText('从 Agent 管理自动带入,不可修改')).toHaveClass(uiStyles.formTips);
    expect(screen.getByTestId('form-error-项目名称')).toHaveClass(uiStyles.formErrorSlot);
  });

  it('shows rounded avatar and top-right progress examples in the display demo', () => {
    render(<ComponentsDemoPage />);

    expect(screen.getByLabelText('张三')).toHaveClass(uiStyles.avatarCircle, uiStyles.avatarText);
    expect(screen.getByTestId('progress-root')).toHaveClass(uiStyles.progressTopRight);
  });

  it('shows Upload file and media wall states in the demo', () => {
    render(<ComponentsDemoPage />);

    expect(screen.getByText('product_design_v2.fig')).toBeInTheDocument();
    expect(screen.getByText('需求文档.pdf')).toBeInTheDocument();
    expect(screen.getByText('素材包.zip')).toBeInTheDocument();
    expect(screen.getByText('支持 JPG、PNG、MP4、MOV,图片单张不超过 10MB,视频单个不超过 500MB')).toBeInTheDocument();
  });

  it('keeps the component demo route isolated from the admin shell copy', () => {
    render(<ComponentsDemoPage />);

    expect(screen.queryByText('岗位Agent管理后台')).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: '退出登录' })).not.toBeInTheDocument();
  });
});