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


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) => {
      const leftPriceScale = {
        getMode: jest.fn(() => PriceScaleMode.Normal),
        setVisible: jest.fn(),
      };

      const rightPriceScale = {
        getMode: jest.fn(() => PriceScaleMode.Normal),
        setVisible: jest.fn(),
      };

      return {
        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((side: Direction) => (side === Direction.Left ? leftPriceScale : rightPriceScale)),
      } 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.each([
    [Direction.Left, true],
    [Direction.Left, false],
    [Direction.Right, true],
    [Direction.Right, false],
  ] as const)('должен устанавливать видимость шкалы %s в %s', (side, visible) => {
    const manager = createManager([
      {
        isMain: true,
        id: 0,
        indicators: [],
        drawings: [],
      },
      {
        isMain: false,
        id: 1,
        indicators: [],
        drawings: [],
      },
      {
        isMain: false,
        id: 2,
        indicators: [
          {
            indicatorType: IndicatorsIds.RSI,
          },
        ],
        drawings: [],
      },
    ]);

    const priceScale = manager.getMainPane().getPriceScale(side);

    manager.setPriceScaleSideVisible(side, !visible);
    jest.mocked(priceScale.setVisible).mockClear();

    manager.setPriceScaleSideVisible(side, visible);

    expect(priceScale.setVisible).toHaveBeenLastCalledWith(visible);

    manager.destroy();
  });

  it('должен сохранять видимость правой шкалы при изменении левой', () => {
    const manager = createManager([
      {
        isMain: true,
        id: 0,
        indicators: [],
        drawings: [],
      },
    ]);

    const pane = manager.getMainPane();
    const leftPriceScale = pane.getPriceScale(Direction.Left);
    const rightPriceScale = pane.getPriceScale(Direction.Right);

    manager.setPriceScaleSideVisible(Direction.Left, false);
    manager.setPriceScaleSideVisible(Direction.Right, false);

    expect(leftPriceScale.setVisible).toHaveBeenLastCalledWith(false);
    expect(rightPriceScale.setVisible).toHaveBeenLastCalledWith(false);

    manager.setPriceScaleSideVisible(Direction.Left, true);

    expect(leftPriceScale.setVisible).toHaveBeenLastCalledWith(true);
    expect(rightPriceScale.setVisible).toHaveBeenLastCalledWith(false);

    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();
  });
});