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

import { ComponentsDemoPage } from './ComponentsDemoPage';

describe('ComponentsDemoPage', () => {
  it('shows TDesign examples and the global business component groups', () => {
    render(<ComponentsDemoPage />);

    expect(screen.getByRole('heading', { name: '组件库 Demo' })).toBeInTheDocument();
    expect(screen.getByText('TDesign 基础控件与全局业务组合组件展示入口')).toBeInTheDocument();
    for (const heading of ['基础交互', '搜索区域', '选择与日期', '表格与分页', '反馈与浮层', '业务表单', '数据展示', '导航与占位', '颜色能力', '上传']) {
      expect(screen.getByRole('heading', { name: heading })).toBeInTheDocument();
    }

    const tableSection = screen.getByRole('heading', { name: '表格与分页' }).closest('section');
    const tableDemo = within(tableSection as HTMLElement);

    expect(tableDemo.getAllByText('项目A').length).toBeGreaterThanOrEqual(1);
    expect(tableDemo.getByText('暂无数据')).toBeInTheDocument();
    expect(tableDemo.getByText('未找到匹配结果')).toBeInTheDocument();
    expect(tableDemo.getAllByText('请求失败').length).toBeGreaterThanOrEqual(1);
    expect(tableDemo.getAllByText('权限受限').length).toBeGreaterThanOrEqual(1);
    expect(tableDemo.getByText('当前账号暂无访问权限')).toBeInTheDocument();
    expect(tableDemo.getByRole('button', { name: '申请权限' })).toBeInTheDocument();
    expect(tableDemo.getAllByText('加载中').length).toBeGreaterThanOrEqual(1);
    expect(tableDemo.queryByText('暂无审批记录')).not.toBeInTheDocument();
    expect(tableDemo.queryByText('请先创建项目')).not.toBeInTheDocument();
    expect(tableDemo.queryByText('请调整搜索或筛选条件')).not.toBeInTheDocument();
    expect(tableDemo.queryByText('列表请求失败,当前数据状态未知')).not.toBeInTheDocument();
    expect(tableDemo.queryByRole('button', { name: '清空筛选' })).not.toBeInTheDocument();
    expect(tableDemo.queryByRole('button', { name: '重试' })).not.toBeInTheDocument();

    const navigationSection = screen.getByRole('heading', { name: '导航与占位' }).closest('section');
    const navigationDemo = within(navigationSection as HTMLElement);
    expect(navigationDemo.queryByText('网络不可用')).not.toBeInTheDocument();
    expect(navigationDemo.queryByText('请检查网络连接后重试')).not.toBeInTheDocument();
    expect(navigationDemo.queryByRole('button', { name: '重新连接' })).not.toBeInTheDocument();
    expect(screen.getByText('product_design_v2.fig')).toBeInTheDocument();

    const remark = screen.getByLabelText('备注');
    expect(remark).toHaveValue('补充说明');
    expect(remark.closest('.t-textarea')).toBeInTheDocument();
    expect(screen.getByText('4/120')).toBeInTheDocument();
  });

  it('shows the global search filter demo with expand, search, and reset behavior', () => {
    render(<ComponentsDemoPage />);

    const searchSection = screen.getByRole('heading', { name: '搜索区域' }).closest('section');
    const scoped = within(searchSection as HTMLElement);

    fireEvent.click(scoped.getByRole('button', { name: '展开' }));
    expect(scoped.getByRole('button', { name: '收起' })).toBeInTheDocument();

    fireEvent.click(scoped.getByRole('button', { name: '查询' }));
    expect(scoped.getByText(/"keyword": "智能审批"/)).toBeInTheDocument();

    fireEvent.click(scoped.getByRole('button', { name: '重置' }));
    expect(scoped.getByText('已重置,等待下一次搜索')).toBeInTheDocument();
  });

  it('keeps the checkbox and switch examples interactive', () => {
    render(<ComponentsDemoPage />);

    const checkbox = screen.getByRole('checkbox', { name: 'Demo 启用' });
    const toggle = screen.getByRole('switch', { name: 'Demo 启用审批' });

    fireEvent.click(checkbox);
    fireEvent.click(toggle);

    expect(checkbox).not.toBeChecked();
    expect(toggle).not.toHaveClass('t-is-checked');
  });

  it('shows Button variants, sizes, and states', () => {
    render(<ComponentsDemoPage />);

    const basicsSection = screen.getByRole('heading', { name: '基础交互' }).closest('section');
    const basicsDemo = within(basicsSection as HTMLElement);

    for (const name of ['主按钮', '普通按钮', '描边按钮', '文字按钮', '危险按钮', '小按钮', '中按钮', '大按钮', '图标按钮']) {
      expect(basicsDemo.getByRole('button', { name })).toBeInTheDocument();
    }
    expect(basicsDemo.getByText('禁用按钮').closest('.t-button')).toHaveAttribute('disabled');
    expect(basicsDemo.getByText('加载中').closest('.t-button')).toHaveAttribute('disabled');
  });

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

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

    const messages = screen.getByLabelText('消息提示示例');
    expect(within(messages).getByText('保存成功')).toBeInTheDocument();
    expect(within(messages).getByText('请补充审批人')).toBeInTheDocument();
    expect(within(messages).getByText('删除失败')).toBeInTheDocument();
  });

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

    expect(screen.getByText('编辑项目')).toBeInTheDocument();
    fireEvent.click(screen.getByRole('button', { name: '取消' }));

    await waitFor(() => expect(screen.queryByText('编辑项目')).not.toBeVisible());
  });

  it('shows TDesign form validation in the feedback demo', async () => {
    render(<ComponentsDemoPage />);

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

    expect(await screen.findByText('请输入项目名称')).toBeInTheDocument();
    expect(screen.getByText('从 Agent 管理自动带入,不可修改')).toBeInTheDocument();
  });

  it('shows the business form demo with dynamic fields, custom actions, and upload states', () => {
    render(<ComponentsDemoPage />);

    const businessFormSection = screen.getByRole('heading', { name: '业务表单' }).closest('section');
    const scoped = within(businessFormSection as HTMLElement);

    expect(scoped.getByRole('button', { name: '填充摘要' })).toBeInTheDocument();
    expect(scoped.getByText('标题:合同审批')).toBeInTheDocument();
    expect(scoped.getByLabelText('适用场景')).toBeInTheDocument();
    expect(scoped.getByText('审批')).toBeInTheDocument();
    expect(scoped.getByText('归档')).toBeInTheDocument();

    fireEvent.change(scoped.getByLabelText('标题'), { target: { value: '新合同' } });
    expect(scoped.getByText('标题:新合同')).toBeInTheDocument();

    fireEvent.change(scoped.getByLabelText('展示模式'), { target: { value: 'show' } });
    expect(scoped.getByLabelText('可见说明')).toBeInTheDocument();

    fireEvent.change(scoped.getByLabelText('展示模式'), { target: { value: 'lock' } });
    expect(scoped.getByLabelText('锁定项')).toBeDisabled();

    fireEvent.change(scoped.getByLabelText('展示模式'), { target: { value: 'read' } });
    expect(scoped.queryByLabelText('只读项')).not.toBeInTheDocument();
    expect(scoped.getByText('只读内容')).toBeInTheDocument();

    fireEvent.click(scoped.getByRole('button', { name: '填充摘要' }));
    expect(scoped.getByText('摘要:新合同')).toBeInTheDocument();

    expect(scoped.getByText('封面图.png')).toBeInTheDocument();
    expect(scoped.getByText('支持图片、视频、GIF,推荐 1:1,最大 20MB')).toBeInTheDocument();
    expect(scoped.getByText('项目资料.pdf')).toBeInTheDocument();

    const mediaField = scoped.getByTestId('business-form-field-coverMedia');
    const fileField = scoped.getByTestId('business-form-field-attachment');
    const mediaUploadInput = mediaField.querySelector('input[type="file"]');
    const fileUploadInput = fileField.querySelector('input[type="file"]');

    if (!mediaUploadInput || !fileUploadInput) {
      throw new Error('Upload input was not rendered');
    }

    expect(mediaField.querySelector('.t-upload__card-mask')).toBeInTheDocument();
    expect(mediaField.querySelector('.t-icon-browse')).toBeInTheDocument();
    expect(mediaField.querySelector('.t-icon-delete')).toBeInTheDocument();
  });

  it('shows the upload files and global media wall states', () => {
    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();
  });
});