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


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', () => {
    // Arrange & Act
    render(
      <MoexChartActions
        selectedInstrumentRows={[]}
        customActionsFooterHandlers={mockHandlers}
      />,
    );

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

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

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

    // Assert
    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', () => {
    // Arrange
    const selectedInstruments = [mockContract];

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

    const percentButton = screen.getByText('%');

    expect(percentButton).not.toBeDisabled();

    // Act
    percentButton.click();

    // Assert
    expect(mockHandlers.handlePercent).toHaveBeenCalledWith(mockContract);
  });

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

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

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

    expect(newScaleButton).not.toBeDisabled();

    // Act
    newScaleButton.click();

    // Assert
    expect(mockHandlers.handleNewScale).toHaveBeenCalledWith(mockContract);
  });

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

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

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

    expect(newPanelButton).not.toBeDisabled();

    // Act
    newPanelButton.click();

    // Assert
    expect(mockHandlers.handleNewPanel).toHaveBeenCalledWith(mockContract);
  });

  it('should handle multiple selected instruments (only first is used)', () => {
    // Arrange
    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();

    // Act
    percentButton.click();
    newScaleButton.click();
    newPanelButton.click();

    // Assert
    // Verify that handlers are called with the first selected instrument
    expect(mockHandlers.handlePercent).toHaveBeenCalledWith(multipleContracts[0]);
    expect(mockHandlers.handleNewScale).toHaveBeenCalledWith(multipleContracts[0]);
    expect(mockHandlers.handleNewPanel).toHaveBeenCalledWith(multipleContracts[0]);
  });
});