ChannelCodeRoutes.test.tsx 1.54 KB
import { cleanup, render, screen } 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 routes and navigation', () => {
  beforeEach(() => {
    window.localStorage.clear();
  });

  it('renders the list route at /channel-codes', () => {
    renderChannelCodes();
    expect(screen.getByRole('heading', { name: '渠道活码管理' })).toBeInTheDocument();
  });

  it('renders the new route at /channel-codes/new', () => {
    renderChannelCodes('/channel-codes/new');
    expect(screen.getByRole('heading', { name: '新增员工渠道活码' })).toBeInTheDocument();
  });

  it('renders the edit route at /channel-codes/:codeId/edit', () => {
    renderChannelCodes('/channel-codes/1/edit');
    expect(screen.getByRole('heading', { name: '编辑员工渠道活码' })).toBeInTheDocument();
  });

  it('shows navigation entry under Agent群管理 group', () => {
    renderChannelCodes();
    expect(screen.getByRole('button', { name: '渠道活码' })).toBeInTheDocument();
  });

  it('marks navigation entry as active when on channel-codes routes', () => {
    renderChannelCodes();
    expect(screen.getByRole('button', { name: '渠道活码' })).toHaveAttribute('aria-current', 'page');
  });
});