ApprovalActionModal.test.tsx 3.29 KB
import { render, screen, within } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import type { ApprovalRow } from '@/api';
import { ApprovalActionModal, type ApprovalActionType } from './ApprovalActionModal';

function renderAction(
  levels: Array<Pick<ApprovalRow, 'currentLevel' | 'totalLevel'>>,
  type: ApprovalActionType = 'pass',
) {
  const records: ApprovalRow[] = levels.map((level, index) => ({
    listingId: String(index + 1),
    storeName: `门店${index + 1}`,
    merchantNo: `SQ00${index + 1}`,
    businessScene: '精品升级',
    status: 'PENDING',
    ...level,
  }));
  render(
    <ApprovalActionModal
      action={{ type, records }}
      pending={false}
      confirmed={false}
      reason=""
      onConfirmed={vi.fn()}
      onReason={vi.fn()}
      onCancel={vi.fn()}
      onConfirm={vi.fn()}
    />,
  );
  return within(screen.getByRole('dialog')).getByRole('alert');
}

describe('通过确认的审批结果提示', () => {
  it.each([
    { currentLevel: 2, totalLevel: 2 },
    { currentLevel: 1, totalLevel: 1 },
    { currentLevel: 3, totalLevel: 3 },
  ])('第 $currentLevel / $totalLevel 级终审通过后提示生效,不提示还有后续审批', (level) => {
    const alert = renderAction([level]);
    expect(alert).toHaveTextContent('通过当前审批节点后,记录将立即生效并进入门店列表。');
    expect(alert).not.toHaveTextContent('后续审批');
  });

  it('中间审批节点提示仍需后续审批', () => {
    const alert = renderAction([{ currentLevel: 1, totalLevel: 2 }]);
    expect(alert).toHaveTextContent('记录将继续后续审批');
    expect(alert).not.toHaveTextContent('立即生效');
  });

  it('批量全部终审时提示生效,不受终审总层级差异影响', () => {
    const alert = renderAction(
      [
        { currentLevel: 1, totalLevel: 1 },
        { currentLevel: 2, totalLevel: 2 },
      ],
      'batch',
    );
    expect(alert).toHaveTextContent('记录将立即生效并进入门店列表');
    expect(alert).not.toHaveTextContent('后续审批');
  });

  it('批量包含不同审批阶段时分别说明终审生效和继续后续审批', () => {
    const alert = renderAction(
      [
        { currentLevel: 2, totalLevel: 2 },
        { currentLevel: 1, totalLevel: 2 },
      ],
      'batch',
    );
    expect(alert).toHaveTextContent('终审记录通过后将立即生效并进入门店列表');
    expect(alert).toHaveTextContent('其余记录将继续后续审批');
  });

  it.each([
    {},
    { currentLevel: null, totalLevel: null },
    { currentLevel: 0, totalLevel: 0 },
    { currentLevel: 3, totalLevel: 2 },
  ])('缺少或非法审批层级不承诺立即生效,也不假定还有后续审批:%j', (level) => {
    const alert = renderAction([level]);
    expect(alert).toHaveTextContent('系统将按记录的审批进度处理');
    expect(alert).not.toHaveTextContent('立即生效');
    expect(alert).not.toHaveTextContent('继续后续审批');
  });

  it('批量存在未知层级时不会将已知终审结果套用到全部记录', () => {
    const alert = renderAction([{ currentLevel: 2, totalLevel: 2 }, {}], 'batch');
    expect(alert).toHaveTextContent('系统将按记录的审批进度处理');
    expect(alert).not.toHaveTextContent('立即生效');
  });
});