ChannelCodeStatusOperations.test.tsx 4.09 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 enable/disable operations', () => {
  beforeEach(() => {
    window.localStorage.clear();
  });

  it('shows enable confirmation dialog', () => {
    renderChannelCodes();

    // Find a disabled row (live_ops has status 'disable')
    const disabledRow = screen.getByRole('row', { name: /直播运营渠道活码/ });
    fireEvent.click(within(disabledRow).getByRole('button', { name: '启用直播运营渠道活码' }));

    expect(screen.getByRole('dialog', { name: '确认启用"直播运营渠道活码"?' })).toBeInTheDocument();
    expect(screen.getByText('启用后渠道活码将恢复可用,用户可以继续扫码添加。')).toBeInTheDocument();
  });

  it('shows disable confirmation dialog', () => {
    renderChannelCodes();

    // Find an enabled row (store_sales has status 'enable')
    const enabledRow = screen.getByRole('row', { name: /店销顾问渠道活码/ });
    fireEvent.click(within(enabledRow).getByRole('button', { name: '停用店销顾问渠道活码' }));

    expect(screen.getByRole('dialog', { name: '确认停用"店销顾问渠道活码"?' })).toBeInTheDocument();
    expect(screen.getByText('停用后渠道活码不可继续扫码使用,请确认是否继续。')).toBeInTheDocument();
  });

  it('cancels enable operation without changing status', () => {
    renderChannelCodes();

    const disabledRow = screen.getByRole('row', { name: /直播运营渠道活码/ });
    fireEvent.click(within(disabledRow).getByRole('button', { name: '启用直播运营渠道活码' }));

    fireEvent.click(screen.getByRole('button', { name: '取消' }));
    expect(screen.queryByRole('dialog', { name: '确认启用' })).not.toBeInTheDocument();

    // Status should remain unchanged
    expect(within(disabledRow).getByText('停用')).toBeInTheDocument();
  });

  it('enables successfully and shows success message', () => {
    renderChannelCodes();

    const disabledRow = screen.getByRole('row', { name: /直播运营渠道活码/ });
    fireEvent.click(within(disabledRow).getByRole('button', { name: '启用直播运营渠道活码' }));

    fireEvent.click(screen.getByRole('button', { name: '确定' }));
    expect(screen.queryByRole('dialog', { name: '确认启用' })).not.toBeInTheDocument();

    expect(screen.getByRole('status', { name: '启用成功' })).toBeInTheDocument();
    expect(within(disabledRow).getByText('启用')).toBeInTheDocument();
  });

  it('disables successfully and shows success message', () => {
    renderChannelCodes('/channel-codes?scenario=normal');

    const enabledRow = screen.getByRole('row', { name: /私域增长渠道活码/ });
    fireEvent.click(within(enabledRow).getByRole('button', { name: '停用私域增长渠道活码' }));

    fireEvent.click(screen.getByRole('button', { name: '确定' }));
    expect(screen.queryByRole('dialog', { name: '确认停用' })).not.toBeInTheDocument();

    expect(screen.getByRole('status', { name: '停用成功' })).toBeInTheDocument();
    expect(within(enabledRow).getByText('停用')).toBeInTheDocument();
  });

  it('blocks disable with business error message', () => {
    renderChannelCodes('/channel-codes?scenario=statusBlockError');

    const enabledRow = screen.getByRole('row', { name: /店销顾问渠道活码/ });
    fireEvent.click(within(enabledRow).getByRole('button', { name: '停用店销顾问渠道活码' }));

    fireEvent.click(screen.getByRole('button', { name: '确定' }));

    // The error message should be shown
    const alert = screen.getByRole('alert');
    expect(alert).toHaveTextContent('官网展示中');
    // Status should remain enabled
    expect(within(enabledRow).getByText('启用')).toBeInTheDocument();
  });
});