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

import {
  Button,
  Checkbox,
  Input,
  Radio,
  SearchInput,
  Textarea,
  Switch,
} from '../index';

describe('shared UI basic controls', () => {
  it('exports the first task baseline from the shared UI boundary', () => {
    expect(Button).toBeTypeOf('function');
    expect(Input).toBeTypeOf('object');
    expect(SearchInput).toBeTypeOf('object');
    expect(Checkbox).toBeTypeOf('object');
    expect(Radio).toBeTypeOf('object');
    expect(Switch).toBeTypeOf('object');
  });

  it('renders Button variants and blocks clicks while loading or disabled', () => {
    const onClick = vi.fn();

    render(
      <>
        <Button variant="primary" size="large" onClick={onClick}>
          保存
        </Button>
        <Button loading onClick={onClick}>
          提交中
        </Button>
        <Button disabled onClick={onClick}>
          不可用
        </Button>
      </>,
    );

    fireEvent.click(screen.getByRole('button', { name: '保存' }));
    fireEvent.click(screen.getByRole('button', { name: /提交中/ }));
    fireEvent.click(screen.getByRole('button', { name: '不可用' }));

    expect(onClick).toHaveBeenCalledTimes(1);
    expect(screen.getByRole('button', { name: /提交中/ })).toBeDisabled();
    expect(screen.getByRole('button', { name: /提交中/ })).toHaveAttribute('aria-busy', 'true');
  });

  it('supports controlled and uncontrolled Input values with clear and status states', () => {
    const onControlledChange = vi.fn();
    const onClear = vi.fn();

    render(
      <>
        <Input
          aria-label="受控输入"
          value="已填写"
          allowClear
          status="error"
          onChange={onControlledChange}
          onClear={onClear}
        />
        <Input aria-label="非受控输入" defaultValue="初始值" prefix="¥" suffix="元" />
      </>,
    );

    fireEvent.click(screen.getByRole('button', { name: '清空受控输入' }));
    fireEvent.change(screen.getByLabelText('非受控输入'), { target: { value: '99' } });

    expect(onControlledChange).toHaveBeenCalledWith('');
    expect(onClear).toHaveBeenCalledTimes(1);
    expect(screen.getByLabelText('非受控输入')).toHaveValue('99');
    expect(screen.getByLabelText('受控输入')).toHaveAttribute('aria-invalid', 'true');
    expect(screen.getByText('¥')).toBeInTheDocument();
    expect(screen.getByText('元')).toBeInTheDocument();
  });

  it('renders concise Input and Textarea count display when showCount is enabled', () => {
    const onTextareaChange = vi.fn();

    render(
      <>
        <Input aria-label="字数输入" defaultValue="合同" maxLength={10} showCount />
        <Textarea aria-label="备注" defaultValue="已填写" maxLength={20} showCount onChange={onTextareaChange} />
      </>,
    );

    fireEvent.change(screen.getByLabelText('备注'), { target: { value: '已填写更多内容' } });

    expect(screen.getByText('2/10')).toBeInTheDocument();
    expect(screen.getByText('7/20')).toBeInTheDocument();
    expect(onTextareaChange).toHaveBeenCalledWith('已填写更多内容');
  });

  it('triggers SearchInput search from enter, button click, and clear', () => {
    const onSearch = vi.fn();
    const onClear = vi.fn();

    render(
      <SearchInput
        aria-label="搜索关键字"
        defaultValue="合同"
        allowClear
        onSearch={onSearch}
        onClear={onClear}
      />,
    );

    fireEvent.keyDown(screen.getByLabelText('搜索关键字'), { key: 'Enter' });
    fireEvent.click(screen.getByRole('button', { name: '搜索' }));
    fireEvent.click(screen.getByRole('button', { name: '清空搜索关键字' }));

    expect(onSearch).toHaveBeenNthCalledWith(1, '合同');
    expect(onSearch).toHaveBeenNthCalledWith(2, '合同');
    expect(onClear).toHaveBeenCalledTimes(1);
  });

  it('renders SearchInput with a design-sized search icon asset', () => {
    render(<SearchInput aria-label="搜索关键字" defaultValue="合同" />);

    const icon = screen.getByLabelText('搜索图标');

    expect(icon).toHaveAttribute('data-icon', 'search');
    expect(icon).toHaveAttribute('width', '18');
    expect(icon).toHaveAttribute('height', '18');
  });

  it('renders Checkbox checked, indeterminate, disabled states and change callback', () => {
    const onChange = vi.fn();

    render(
      <>
        <Checkbox label="启用" checked onChange={onChange} />
        <Checkbox label="部分选择" indeterminate />
        <Checkbox label="禁用" disabled />
      </>,
    );

    fireEvent.click(screen.getByLabelText('启用'));
    fireEvent.click(screen.getByLabelText('禁用'));

    expect(onChange).toHaveBeenCalledWith(false);
    expect(screen.getByLabelText('部分选择')).toHaveAttribute('aria-checked', 'mixed');
    expect(screen.getByLabelText('禁用')).toBeDisabled();
  });

  it('renders Radio as a controlled single option and respects disabled state', () => {
    const onChange = vi.fn();

    render(
      <>
        <Radio label="按项目" name="range" value="project" checked onChange={onChange} />
        <Radio label="按组织" name="range" value="org" disabled onChange={onChange} />
      </>,
    );

    fireEvent.click(screen.getByLabelText('按项目'));
    fireEvent.click(screen.getByLabelText('按组织'));

    expect(onChange).toHaveBeenCalledWith('project');
    expect(screen.getByLabelText('按组织')).toBeDisabled();
  });

  it('toggles Switch and ignores interaction while loading or disabled', () => {
    const onChange = vi.fn();

    render(
      <>
        <Switch aria-label="启用审批" checked={false} onChange={onChange} />
        <Switch aria-label="同步中" checked loading onChange={onChange} />
        <Switch aria-label="禁止切换" checked disabled onChange={onChange} />
      </>,
    );

    fireEvent.click(screen.getByRole('switch', { name: '启用审批' }));
    fireEvent.click(screen.getByRole('switch', { name: '同步中' }));
    fireEvent.click(screen.getByRole('switch', { name: '禁止切换' }));

    expect(onChange).toHaveBeenCalledTimes(1);
    expect(onChange).toHaveBeenCalledWith(true);
    expect(screen.getByRole('switch', { name: '同步中' })).toHaveAttribute('aria-busy', 'true');
    expect(screen.getByRole('switch', { name: '禁止切换' })).toBeDisabled();
  });
});