BusinessAdminShell.test.tsx 6.84 KB
import { fireEvent, render, screen, within } from '@testing-library/react';
import { RobotIcon, SettingIcon } from 'tdesign-icons-react';
import { describe, expect, it, vi } from 'vitest';

import { BusinessAdminShell, type BusinessAdminShellMenuItem } from '@gl/business-components';

const menuItems: BusinessAdminShellMenuItem[] = [
  {
    key: 'agent-management',
    label: 'Agent管理',
    icon: <RobotIcon data-testid="agent-menu-icon" />,
    children: [
      { key: 'agent-list', label: 'Agent列表' },
      { key: 'agent-tag-list', label: '岗位标签管理' },
    ],
  },
  {
    key: 'official-site-management',
    label: '官网配置',
    icon: <SettingIcon data-testid="official-site-menu-icon" />,
    children: [{ key: 'official-site-job-tabs', label: '岗位展示配置' }],
  },
];

describe('BusinessAdminShell', () => {
  it('renders the fixed post-login shell structure with system name, menu, header and 24px content padding', () => {
    render(
      <BusinessAdminShell
        systemName="岗位Agent管理后台"
        menuItems={menuItems}
        activeMenuKey="agent-list"
        expandedMenuKeys={['agent-management']}
        breadcrumbItems={[
          { key: 'agent-management', label: 'Agent管理' },
          { key: 'agent-list', label: 'Agent列表' },
        ]}
        user={{ name: '管理员' }}
        onMenuChange={vi.fn()}
        onLogout={vi.fn()}
      >
        <h2>页面内容</h2>
      </BusinessAdminShell>,
    );

    expect(screen.getByText('岗位Agent管理后台')).toBeInTheDocument();
    expect(screen.getAllByText('Agent管理').length).toBeGreaterThan(0);
    expect(screen.getByTestId('agent-menu-icon')).toBeInTheDocument();
    expect(screen.getByTestId('official-site-menu-icon')).toBeInTheDocument();
    const leftNavigation = screen.getByLabelText('左侧导航');
    expect(within(leftNavigation).getByText('Agent列表').closest('li')).toHaveClass('t-is-active');
    expect(within(leftNavigation).getByText('Agent管理')).toBeInTheDocument();
    expect(within(leftNavigation).getByText('Agent列表')).toBeInTheDocument();
    expect(screen.getByText('页面内容').closest('main')).toHaveStyle({ padding: '24px' });
    expect(screen.getByText('管理员')).toBeInTheDocument();
    expect(screen.getByLabelText('管理员头像')).toHaveTextContent('管');
    expect(screen.getByRole('button', { name: '退出登录' })).toBeInTheDocument();
  });

  it('collapses and expands the left menu while preserving configured menu icons', () => {
    render(
      <BusinessAdminShell
        systemName="岗位Agent管理后台"
        menuItems={menuItems}
        activeMenuKey="agent-list"
        expandedMenuKeys={['agent-management']}
        breadcrumbItems={[
          { key: 'agent-management', label: 'Agent管理' },
          { key: 'agent-list', label: 'Agent列表' },
        ]}
        user={{ name: '管理员' }}
        onMenuChange={vi.fn()}
        onLogout={vi.fn()}
      >
        <h2>页面内容</h2>
      </BusinessAdminShell>,
    );

    fireEvent.click(screen.getByRole('button', { name: '收起菜单' }));

    expect(screen.getByRole('button', { name: '展开菜单' })).toBeInTheDocument();
    expect(screen.getByTestId('agent-menu-icon')).toBeInTheDocument();
    expect(screen.getByLabelText('左侧导航')).toHaveStyle({ width: '64px' });

    fireEvent.click(screen.getByRole('button', { name: '展开菜单' }));

    expect(screen.getByRole('button', { name: '收起菜单' })).toBeInTheDocument();
    expect(screen.getByLabelText('左侧导航')).toHaveStyle({ width: '220px' });
    expect(screen.getByText('岗位Agent管理后台')).toBeInTheDocument();
  });

  it('toggles a first-level menu section without being locked by route expanded keys', () => {
    render(
      <BusinessAdminShell
        systemName="岗位Agent管理后台"
        menuItems={menuItems}
        activeMenuKey="agent-list"
        expandedMenuKeys={['agent-management']}
        breadcrumbItems={[
          { key: 'agent-management', label: 'Agent管理' },
          { key: 'agent-list', label: 'Agent列表' },
        ]}
        user={{ name: '管理员' }}
        onMenuChange={vi.fn()}
        onLogout={vi.fn()}
      >
        <h2>页面内容</h2>
      </BusinessAdminShell>,
    );

    const leftNavigation = screen.getByLabelText('左侧导航');
    const agentMenuSection = within(leftNavigation).getByText('Agent管理').closest('li');
    expect(agentMenuSection).toHaveClass('t-is-opened');

    fireEvent.click(within(leftNavigation).getByText('Agent管理'));

    expect(agentMenuSection).not.toHaveClass('t-is-opened');

    fireEvent.click(within(leftNavigation).getByText('Agent管理'));

    expect(agentMenuSection).toHaveClass('t-is-opened');
  });

  it('supports independent page breadcrumbs returning to the secondary menu page', () => {
    const handleBreadcrumbClick = vi.fn();

    render(
      <BusinessAdminShell
        systemName="岗位Agent管理后台"
        menuItems={menuItems}
        activeMenuKey="agent-list"
        expandedMenuKeys={['agent-management']}
        breadcrumbItems={[
          { key: 'agent-management', label: 'Agent管理' },
          { key: 'agent-list', label: 'Agent列表', onClick: handleBreadcrumbClick },
          { key: 'agent-create', label: '新增Agent' },
        ]}
        user={{ name: '管理员' }}
        onMenuChange={vi.fn()}
        onLogout={vi.fn()}
      >
        <h2>新增Agent</h2>
      </BusinessAdminShell>,
    );

    expect(screen.getByRole('heading', { name: '新增Agent' })).toBeInTheDocument();
    fireEvent.click(screen.getByRole('button', { name: 'Agent列表' }));

    expect(handleBreadcrumbClick).toHaveBeenCalledTimes(1);
  });

  it('shows profile and logout actions in avatar dropdown when profile is enabled', () => {
    const handleProfile = vi.fn();
    const handleLogout = vi.fn();

    render(
      <BusinessAdminShell
        systemName="岗位Agent管理后台"
        menuItems={menuItems}
        activeMenuKey="agent-list"
        expandedMenuKeys={['agent-management']}
        breadcrumbItems={[
          { key: 'agent-management', label: 'Agent管理' },
          { key: 'agent-list', label: 'Agent列表' },
        ]}
        user={{ name: '管理员' }}
        hasProfile
        onMenuChange={vi.fn()}
        onProfile={handleProfile}
        onLogout={handleLogout}
      >
        <h2>页面内容</h2>
      </BusinessAdminShell>,
    );

    expect(screen.queryByRole('button', { name: '退出登录' })).not.toBeInTheDocument();
    fireEvent.click(screen.getByRole('button', { name: '管理员 用户菜单' }));
    fireEvent.click(screen.getByText('个人信息'));
    fireEvent.click(screen.getByRole('button', { name: '管理员 用户菜单' }));
    fireEvent.click(screen.getByText('退出登录'));

    expect(handleProfile).toHaveBeenCalledTimes(1);
    expect(handleLogout).toHaveBeenCalledTimes(1);
  });
});