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

import { App } from '../../App';
import { resetAgentDisplayConfigMockState } from '../../../packages/contracts/official-site-agent-display-config-mock';
import { resetOfficialSiteJobTabMockState } from '../../../packages/contracts/official-site-job-tab-mock';

function renderApp(path = '/official-site/job-tabs/1/agents') {
  window.localStorage.setItem('agent-admin-local-login', 'true');
  return render(
    <MemoryRouter initialEntries={[path]}>
      <App />
    </MemoryRouter>,
  );
}

describe('official site agent display config routes and navigation', () => {
  beforeEach(() => {
    window.localStorage.clear();
    resetOfficialSiteJobTabMockState();
    resetAgentDisplayConfigMockState();
  });

  it('renders the agent display config page at /official-site/job-tabs/:tabId/agents', () => {
    renderApp();
    expect(screen.getByRole('heading', { name: '店销顾问 - 店销顾问渠道活码 - Agent展示配置' })).toBeInTheDocument();
  });

  it('shows navigation entry under 官网配置 group', () => {
    renderApp();
    expect(screen.getByRole('button', { name: '岗位展示配置' })).toBeInTheDocument();
  });

  it('marks 岗位展示配置 navigation entry as active when on agents route', () => {
    renderApp();
    expect(screen.getByRole('button', { name: '岗位展示配置' })).toHaveAttribute('aria-current', 'page');
  });

  it('shows breadcrumb for agent display config page', () => {
    renderApp();
    expect(screen.getByText('官网配置 / 岗位展示配置')).toBeInTheDocument();
  });

  it('shows return to list button', () => {
    renderApp();
    expect(screen.getByRole('button', { name: '返回' })).toBeInTheDocument();
  });

  it('shows position name in page title', () => {
    renderApp();
    expect(screen.getByRole('heading', { name: /店销顾问.*Agent展示配置/ })).toBeInTheDocument();
  });

  it('shows tab context info (qrcode, channel code, employee)', () => {
    renderApp();
    expect(screen.getByText('店销顾问渠道活码')).toBeInTheDocument();
    expect(screen.getByText('DSC-QR-001')).toBeInTheDocument();
    expect(screen.getByText((content) => content.includes('张小萌'))).toBeInTheDocument();
  });

  it('returns to job tabs list when clicking return button', () => {
    renderApp();
    fireEvent.click(screen.getByRole('button', { name: '返回' }));
    expect(screen.getByRole('heading', { name: '岗位展示配置' })).toBeInTheDocument();
  });

  it('shows error state for non-existent tab', () => {
    renderApp('/official-site/job-tabs/99999/agents');
    expect(screen.getByText('岗位展示配置不存在')).toBeInTheDocument();
    expect(screen.getByRole('button', { name: '返回列表' })).toBeInTheDocument();
  });

  it('can navigate to agent display config from job tabs list', () => {
    renderApp('/official-site/job-tabs');
    fireEvent.click(screen.getByRole('button', { name: '配置Agent-店销顾问' }));
    expect(screen.getByRole('heading', { name: /店销顾问.*Agent展示配置/ })).toBeInTheDocument();
  });
});