ChannelCodeViewDrawer.test.tsx 4.28 KB
import { fireEvent, render, screen, within } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { beforeEach, describe, expect, it } from 'vitest';

import { App } from '../../App';

function renderChannelCodes(path = '/channel-codes') {
  window.localStorage.setItem('agent-admin-local-login', 'true');
  return render(
    <MemoryRouter initialEntries={[path]}>
      <App />
    </MemoryRouter>,
  );
}

describe('Channel code view drawer', () => {
  beforeEach(() => {
    window.localStorage.clear();
  });

  it('opens drawer when clicking view action', () => {
    renderChannelCodes();

    const firstRow = screen.getByRole('row', { name: /店销顾问渠道活码/ });
    fireEvent.click(screen.getByRole('button', { name: '查看店销顾问渠道活码' }));

    expect(screen.getByRole('dialog', { name: '店销顾问渠道活码' })).toBeInTheDocument();
  });

  it('shows all detail fields in drawer', () => {
    renderChannelCodes();

    fireEvent.click(screen.getByRole('button', { name: '查看店销顾问渠道活码' }));

    const drawer = screen.getByRole('dialog', { name: '店销顾问渠道活码' });
    expect(within(drawer).getByText('对应岗位')).toBeInTheDocument();
    expect(within(drawer).getByText('渠道编码')).toBeInTheDocument();
    expect(within(drawer).getByText('承接员工')).toBeInTheDocument();
    expect(within(drawer).getByText('关联 Agent')).toBeInTheDocument();
    expect(within(drawer).getByText('状态')).toBeInTheDocument();
    expect(within(drawer).getByText('活码描述')).toBeInTheDocument();
    expect(within(drawer).getByText('自动欢迎语')).toBeInTheDocument();
  });

  it('shows QR code image', () => {
    renderChannelCodes();

    fireEvent.click(screen.getByRole('button', { name: '查看店销顾问渠道活码' }));

    expect(screen.getByRole('img', { name: '渠道活码二维码' })).toBeInTheDocument();
  });

  it('closes drawer and preserves list context', () => {
    renderChannelCodes();

    fireEvent.click(screen.getByRole('button', { name: '查看店销顾问渠道活码' }));
    expect(screen.getByRole('dialog', { name: '店销顾问渠道活码' })).toBeInTheDocument();

    // Drawer close button is aria-label="关闭{title}"
    const closeButton = screen.getByRole('button', { name: '关闭店销顾问渠道活码' });
    fireEvent.click(closeButton);
    expect(screen.queryByRole('dialog', { name: '店销顾问渠道活码' })).not.toBeInTheDocument();

    // List should still be visible
    expect(screen.getByRole('heading', { name: '渠道活码管理' })).toBeInTheDocument();
    expect(screen.getByText('店销顾问渠道活码')).toBeInTheDocument();
  });

  it('closes drawer via bottom close button', () => {
    renderChannelCodes();

    fireEvent.click(screen.getByRole('button', { name: '查看店销顾问渠道活码' }));
    expect(screen.getByRole('dialog', { name: '店销顾问渠道活码' })).toBeInTheDocument();

    const drawer = screen.getByRole('dialog', { name: '店销顾问渠道活码' });
    // Footer close button uses "关闭" as text
    const footerCloseButton = within(drawer).getByRole('button', { name: '关闭' });
    fireEvent.click(footerCloseButton);
    expect(screen.queryByRole('dialog', { name: '店销顾问渠道活码' })).not.toBeInTheDocument();
  });

  it('shows agent names as tags with unified color', () => {
    renderChannelCodes();

    fireEvent.click(screen.getByRole('button', { name: '查看店销顾问渠道活码' }));

    const drawer = screen.getByRole('dialog', { name: '店销顾问渠道活码' });
    // Should display agent names as tags
    expect(within(drawer).getByText('店销助手Agent')).toBeInTheDocument();
    expect(within(drawer).getByText('管家Agent')).toBeInTheDocument();
  });

  it('shows QR code load error state', () => {
    renderChannelCodes('/channel-codes?scenario=qrcodeLoadError');

    fireEvent.click(screen.getByRole('button', { name: '查看店销顾问渠道活码' }));

    // Should show error state for QR code
    expect(screen.getByText('二维码加载失败')).toBeInTheDocument();
    // Other details should still be visible
    const drawer = screen.getByRole('dialog', { name: '店销顾问渠道活码' });
    expect(within(drawer).getByText('对应岗位')).toBeInTheDocument();
  });
});