Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

2.1 KB
Raw Blame
/**
 * @jest-environment jsdom
 */
import { render, screen, fireEvent } from '@testing-library/react';
import StopButton from '../StopButton';

jest.mock('@librechat/client', () => ({
  ...jest.requireActual('@librechat/client'),
  useLocalize: () => (key: string) => key,
}));

describe('StopButton', () => {
  it('uses contrasted theme roles for its fill and inherited glyph ink', () => {
    render(<StopButton stop={jest.fn()} setShowStopButton={jest.fn()} />);

    const button = screen.getByTestId('stop-generation-button');
    expect(button).toHaveClass('bg-surface-inverted', 'text-text-inverted');
    expect(button.querySelector('rect')).toHaveAttribute('fill', 'currentColor');
  });

  it('hides itself and aborts the run on click', () => {
    const stop = jest.fn();
    const setShowStopButton = jest.fn();
    render(<StopButton stop={stop} setShowStopButton={setShowStopButton} />);

    fireEvent.click(screen.getByTestId('stop-generation-button'));

    expect(setShowStopButton).toHaveBeenCalledWith(false);
    expect(stop).toHaveBeenCalledTimes(1);
  });

  it('stays visible but inert while the abort would be a no-op', () => {
    const stop = jest.fn();
    const setShowStopButton = jest.fn();
    render(<StopButton stop={stop} setShowStopButton={setShowStopButton} canStop={false} />);

    const button = screen.getByTestId('stop-generation-button');
    expect(button).toBeDisabled();

    fireEvent.click(button);

    expect(setShowStopButton).not.toHaveBeenCalled();
    expect(stop).not.toHaveBeenCalled();
  });

  it('keeps an out-of-layout control the stop shortcut can still reach', () => {
    const stop = jest.fn();
    const setShowStopButton = jest.fn();
    render(<StopButton stop={stop} setShowStopButton={setShowStopButton} hidden />);

    const button = screen.getByTestId('stop-generation-button');
    /* The preflight hides `[hidden]` with `!important`, over the slot's `flex`. */
    expect(button).toHaveAttribute('hidden');

    button.click();

    expect(setShowStopButton).toHaveBeenCalledWith(false);
    expect(stop).toHaveBeenCalledTimes(1);
  });
});