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


import { PriceScaleMode } from 'lightweight-charts';
import { BehaviorSubject, of } from 'rxjs';

import { DrawingsManagerCollection } from '@core/DrawingsManagerCollection';
import { Pane } from '@core/Pane';
import { PaneManager } from '@core/PaneManager';
import { PriceAxisLabels } from '@core/PriceAxisLabels';

import { IndicatorsIds } from '@src/constants';
import { Direction } from '@src/types';
import { PaneSnapshot } from '@src/types/snapshot';

jest.mock('@core/Pane');
jest.mock('@core/DrawingsManagerCollection');
jest.mock('@core/PriceAxisLabels');

describe('PaneManager', () => {
  const PaneMock = Pane as jest.MockedClass<typeof Pane>;

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

    PaneMock.mockImplementation(
      (params) =>
        ({
          getId: jest.fn(() => params.id),
          paneIndex: jest.fn(() => params.id),
          getMainSerie: jest.fn(() => new BehaviorSubject(null)),
          setDrawingsSnapshot: jest.fn(),
          getDrawingsSnapshot: jest.fn(() => []),
          schedulePaneContainerSync: jest.fn(),
          refreshPriceScaleControls: jest.fn(),
          resetPriceScalesAutoScale: jest.fn(),
          destroy: jest.fn(),
          getSnapshot: jest.fn(() => ({
            isMain: params.isMainPane,
            id: params.id,
            indicators: [],
            drawings: [],
          })),
          getPriceScale: jest.fn(() => ({
            getMode: jest.fn(() => PriceScaleMode.Normal),
            setVisible: jest.fn(),
          })),
        }) as unknown as Pane,
    );

    (DrawingsManagerCollection as jest.MockedClass<typeof DrawingsManagerCollection>).mockImplementation(
      () =>
        ({
          destroy: jest.fn(),
          handlePaneClick: jest.fn(),
          addDrawingManager: jest.fn(),
          removeDrawingManager: jest.fn(),
          setActiveTool: jest.fn(),
          getActiveToolValue: jest.fn(),
          getIsEndlessMode: jest.fn(),
          activateDrawingTool: jest.fn(),
        }) as unknown as DrawingsManagerCollection,
    );

    (PriceAxisLabels as jest.MockedClass<typeof PriceAxisLabels>).mockImplementation(
      () =>
        ({
          destroy: jest.fn(),
          invalidate: jest.fn(),
          setVisibleLogicalRange: jest.fn(),
        }) as unknown as PriceAxisLabels,
    );
  });

  function createManager(panesSnapshot: PaneSnapshot[]): PaneManager {
    const params = {
      panesSnapshot,
      lwcChart: {
        subscribeClick: jest.fn(),
        unsubscribeClick: jest.fn(),
        removePane: jest.fn(),
      },
      eventManager: {
        symbol: jest.fn(() => of('')),
      },
      dataSource: null,
      DOM: {},
      ohlcConfig: {},
      subscribeChartEvent: jest.fn(),
      tooltipConfig: {},
      chartContainer: document.createElement('div'),
      modalRenderer: {},
      hotkeys: {},
    } as unknown as ConstructorParameters<typeof PaneManager>[0];

    return new PaneManager(params);
  }

  it('не должен восстанавливать пустой secondary pane', () => {
    const manager = createManager([
      {
        isMain: true,
        id: 0,
        indicators: [],
        drawings: [],
      },
      {
        isMain: false,
        id: 1,
        indicators: [],
        drawings: [],
      },
      {
        isMain: false,
        id: 2,
        indicators: [
          {
            indicatorType: IndicatorsIds.RSI,
          },
        ],
        drawings: [],
      },
    ]);

    expect(manager.getPanes().has(0)).toBe(true);
    expect(manager.getPanes().has(1)).toBe(false);
    expect(manager.getPanes().has(2)).toBe(true);
    expect(manager.getPanes().size).toBe(2);

    manager.destroy();
  });

  it('Выставление шкал', () => {
    const manager = createManager([
      {
        isMain: true,
        id: 0,
        indicators: [],
        drawings: [],
      },
      {
        isMain: false,
        id: 1,
        indicators: [],
        drawings: [],
      },
      {
        isMain: false,
        id: 2,
        indicators: [
          {
            indicatorType: IndicatorsIds.RSI,
          },
        ],
        drawings: [],
      },
    ]);
    manager.setPriceScaleSideVisible(Direction.Left, true);

    expect(manager['leftPriceScaleVisible']).toBe(true);
    expect(manager['rightPriceScaleVisible']).toBe(true);
    expect(manager.getMainPane().getPriceScale(Direction.Left)).toBeDefined();

    manager.destroy();
  });

  it('геттер пейнов', () => {
    const manager = createManager([
      {
        isMain: true,
        id: 0,
        indicators: [],
        drawings: [],
      },
      {
        isMain: false,
        id: 1,
        indicators: [],
        drawings: [],
      },
      {
        isMain: false,
        id: 2,
        indicators: [
          {
            indicatorType: IndicatorsIds.RSI,
          },
        ],
        drawings: [],
      },
    ]);

    expect(manager.getPaneByIndex(0)).toMatchObject(manager.getMainPane());

    manager.destroy();
  });
});