Загрузка данных


import { render, screen } from '@testing-library/react';
import React from 'react';

import '@testing-library/jest-dom';
import { Contract } from '@modules/contracts';

import { MoexChartActions } from '../MoexChartActions';

describe('MoexChartActions', () => {
  const mockHandlers = {
    handlePercent: jest.fn(),
    handleNewScale: jest.fn(),
    handleNewPanel: jest.fn(),
  };

  const mockContract = {
    issKey: 'RU000A0JQ0Y0',
  };

  beforeEach(() => {
    jest.clearAllMocks();
  });

  it('should render correctly with no selected instruments', () => {
    render(
      <MoexChartActions
        selectedInstrumentRows={[]}
        customActionsFooterHandlers={mockHandlers}
      />,
    );

    expect(screen.getByText('%')).toBeInTheDocument();
    expect(screen.getByText('Новая шкала')).toBeInTheDocument();
    expect(screen.getByText('Новая панель')).toBeInTheDocument();
  });

  it('should render correctly with selected instruments', () => {
    const selectedInstruments = [mockContract];

    render(
      <MoexChartActions
        selectedInstrumentRows={selectedInstruments as Contract[]}
        customActionsFooterHandlers={mockHandlers}
      />,
    );

    expect(screen.getByText('%')).toBeInTheDocument();
    expect(screen.getByText('Новая шкала')).toBeInTheDocument();
    expect(screen.getByText('Новая панель')).toBeInTheDocument();

    // All buttons should be enabled
    const percentButton = screen.getByText('%');
    const newScaleButton = screen.getByText('Новая шкала');
    const newPanelButton = screen.getByText('Новая панель');

    expect(percentButton).not.toBeDisabled();
    expect(newScaleButton).not.toBeDisabled();
    expect(newPanelButton).not.toBeDisabled();
  });

  it('should call handlePercent when percent button is clicked and instrument is selected', () => {
    const selectedInstruments = [mockContract];

    render(
      <MoexChartActions
        selectedInstrumentRows={selectedInstruments as Contract[]}
        customActionsFooterHandlers={mockHandlers}
      />,
    );

    const percentButton = screen.getByText('%');
    expect(percentButton).not.toBeDisabled();

    percentButton.click();
    expect(mockHandlers.handlePercent).toHaveBeenCalledWith('RU000A0JQ0Y0');
  });

  it('should call handleNewScale when new scale button is clicked and instrument is selected', () => {
    const selectedInstruments = [mockContract];

    render(
      <MoexChartActions
        selectedInstrumentRows={selectedInstruments as Contract[]}
        customActionsFooterHandlers={mockHandlers}
      />,
    );

    const newScaleButton = screen.getByText('Новая шкала');
    expect(newScaleButton).not.toBeDisabled();

    newScaleButton.click();
    expect(mockHandlers.handleNewScale).toHaveBeenCalledWith('RU000A0JQ0Y0');
  });

  it('should call handleNewPanel when new panel button is clicked and instrument is selected', () => {
    const selectedInstruments = [mockContract];

    render(
      <MoexChartActions
        selectedInstrumentRows={selectedInstruments as Contract[]}
        customActionsFooterHandlers={mockHandlers}
      />,
    );

    const newPanelButton = screen.getByText('Новая панель');
    expect(newPanelButton).not.toBeDisabled();

    newPanelButton.click();
    expect(mockHandlers.handleNewPanel).toHaveBeenCalledWith('RU000A0JQ0Y0');
  });

  it('should handle multiple selected instruments (only first is used)', () => {
    const multipleContracts = [{ issKey: 'RU000A0JQ0Y0' }, { issKey: 'RU000A0JQ0Y1' }];

    render(
      <MoexChartActions
        selectedInstrumentRows={multipleContracts as Contract[]}
        customActionsFooterHandlers={mockHandlers}
      />,
    );

    const percentButton = screen.getByText('%');
    const newScaleButton = screen.getByText('Новая шкала');
    const newPanelButton = screen.getByText('Новая панель');

    expect(percentButton).not.toBeDisabled();
    expect(newScaleButton).not.toBeDisabled();
    expect(newPanelButton).not.toBeDisabled();

    // Click buttons and verify they call handler with first instrument's issKey
    percentButton.click();
    expect(mockHandlers.handlePercent).toHaveBeenCalledWith('RU000A0JQ0Y0');

    newScaleButton.click();
    expect(mockHandlers.handleNewScale).toHaveBeenCalledWith('RU000A0JQ0Y0');

    newPanelButton.click();
    expect(mockHandlers.handleNewPanel).toHaveBeenCalledWith('RU000A0JQ0Y0');
  });
});