Загрузка данных
import { act, render } from '@testing-library/react';
import { MoexChart, Timeframes } from 'moex-chart';
import React from 'react';
import { useChangeProperties, useSelectProperties } from '@modules/widgetProperties';
import { useMoexChart } from '../components/MoexChart/hooks';
import type { ChartIndicativeData } from '@widgets/Chart/types';
jest.mock('@modules/widgetProperties');
jest.mock('../components/MoexChart/dataSourceProvide', () => ({
DataSourceProvider: jest.fn(),
}));
jest.mock('moex-chart', () => ({
MoexChart: jest.fn(),
Timeframes: {
'10s': '10s',
'1m': '1m',
'5m': '5m',
},
DateFormat: {
DD_MM_YYYY_HH_mm_ss: 'DD_MM_YYYY_HH_mm_ss',
},
IndicatorsIds: {
Volume: 'Volume',
},
Locale: {
rus: 'ru-RU',
eng: 'en-US',
},
}));
jest.mock('@api/index', () => ({
updateMenuLocked: jest.fn(),
widgetsController: {
delete: jest.fn(),
},
widgetPropertiesController: {
update: jest.fn(),
},
workspaceController: {
update: jest.fn(),
},
}));
jest.mock('@api/controllers/workspace', () => ({
workspaceController: {
update: jest.fn(),
},
}));
const mockedDataSourceProvide = jest.requireMock('../components/MoexChart/dataSourceProvide') as {
DataSourceProvider: jest.Mock;
};
type TimeframeValue = (typeof Timeframes)[keyof typeof Timeframes];
interface MockCompareSeries {
name: string;
seriesOptions?: {
priceScaleId?: string;
color?: string;
};
}
interface MockIndicatorConfig {
symbol?: string;
label?: string;
newPane?: boolean;
series: MockCompareSeries[];
}
interface MockIndicatorSnapshot {
id?: string;
indicatorType?: string;
dataSource?: unknown;
config?: MockIndicatorConfig;
}
interface MockPaneSnapshot {
indicators: MockIndicatorSnapshot[];
}
interface MockChartSnapshot {
charts: {
symbol: string;
symbolName?: string;
timeframe: TimeframeValue;
chartSeriesType: string;
panes: MockPaneSnapshot[];
}[];
}
interface MockMoexChartConfig {
container: HTMLElement;
snapshot: MockChartSnapshot;
chartCollectionPreset: {
openCompareModal: () => void;
openSymbolSearchModal: () => void;
getDataSource: jest.Mock;
startRealtime: (
getSymbols: () => string[],
getTimeframe: () => TimeframeValue,
update: jest.Mock,
) => (() => void) | undefined;
};
}
interface MockMoexChartState {
timeframe: TimeframeValue;
initialInterval?: string;
savedData?: string;
}
interface MockPropertiesState {
moexChartState?: MockMoexChartState;
}
type UpdatePropertiesCallback = (state: MockPropertiesState) => void;
type TimeframeChangeCallback = (timeframe: TimeframeValue) => void;
interface TestComponentProps {
symbol?: string;
symbolName?: string;
symbolTicker?: string;
indicativeData?: ChartIndicativeData;
}
describe('useMoexChart', () => {
const mockUseSelectProperties = useSelectProperties as jest.Mock;
const mockUseChangeProperties = useChangeProperties as jest.Mock;
const mockMoexChart = MoexChart as jest.Mock;
const mockDataSourceProvider = mockedDataSourceProvide.DataSourceProvider;
const mockUpdateProperties = jest.fn();
const mockDestroy = jest.fn();
const mockGetSnapshot = jest.fn();
const mockSetSnapshot = jest.fn();
const mockSetSymbol = jest.fn();
const mockGetCompareManager = jest.fn();
const mockSetSettings = jest.fn();
const mockDataSource = jest.fn();
const mockStartRealtime = jest.fn();
const mockGetDataSource = jest.fn();
const mockRealtimeUnsubscribe = jest.fn();
const mockCompareManager = {
id: 'compare-manager',
};
const mockSnapshot: MockChartSnapshot = {
charts: [
{
symbol: 'OLD:SYMBOL',
symbolName: 'Old instrument',
timeframe: Timeframes['5m'],
chartSeriesType: 'Candlestick',
panes: [
{
indicators: [],
},
],
},
],
};
let hookResult: ReturnType<typeof useMoexChart> | null = null;
let lastMoexChartConfig: MockMoexChartConfig | null = null;
let mockMoexChartState: MockMoexChartState | undefined;
const TestComponent = ({
symbol = 'MOEX:SBER',
symbolName = 'Сбербанк',
symbolTicker = 'SBER',
indicativeData,
}: TestComponentProps): React.ReactElement => {
hookResult = useMoexChart({
symbol,
symbolName,
symbolTicker,
indicativeData,
});
return <div ref={hookResult.containerRef} />;
};
beforeEach(() => {
jest.clearAllMocks();
jest.useFakeTimers();
hookResult = null;
lastMoexChartConfig = null;
mockMoexChartState = {
timeframe: Timeframes['1m'],
savedData: undefined,
};
mockUseSelectProperties.mockImplementation((selector: (state: MockPropertiesState) => unknown) =>
selector({
moexChartState: mockMoexChartState,
}),
);
mockUseChangeProperties.mockReturnValue({
updateProperties: mockUpdateProperties,
});
mockGetDataSource.mockImplementation(
(_indicativeData?: ChartIndicativeData, callback?: (timeframe: TimeframeValue) => void) =>
(timeframe: TimeframeValue) => {
callback?.(timeframe);
return mockDataSource();
},
);
mockStartRealtime.mockReturnValue(mockRealtimeUnsubscribe);
mockDataSourceProvider.mockImplementation(() => ({
getDataSource: mockGetDataSource,
startRealtime: mockStartRealtime,
}));
mockGetSnapshot.mockReturnValue(mockSnapshot);
mockGetCompareManager.mockReturnValue(mockCompareManager);
mockMoexChart.mockImplementation((config: MockMoexChartConfig) => {
lastMoexChartConfig = config;
return {
destroy: mockDestroy,
getSnapshot: mockGetSnapshot,
setSnapshot: mockSetSnapshot,
setSymbol: mockSetSymbol,
setSettings: mockSetSettings,
getCompareManager: mockGetCompareManager,
};
});
});
afterEach(() => {
jest.clearAllTimers();
jest.useRealTimers();
});
it('should create moex chart with current symbol, symbol name and timeframe', () => {
// Arrange & Act
render(
<TestComponent
symbol="MOEX:SBER"
symbolName="Сбербанк"
/>,
);
// Assert
expect(mockMoexChart).toHaveBeenCalledTimes(1);
expect(mockDataSourceProvider).toHaveBeenCalledTimes(1);
expect(lastMoexChartConfig?.container).toBeInstanceOf(HTMLDivElement);
expect(lastMoexChartConfig?.snapshot.charts[0]?.symbol).toBe('MOEX:SBER');
expect(lastMoexChartConfig?.snapshot.charts[0]?.symbolName).toBe('Сбербанк');
expect(lastMoexChartConfig?.snapshot.charts[0]?.timeframe).toBe(Timeframes['1m']);
expect(hookResult?.compareManagerRef.current).toBe(mockCompareManager);
expect(hookResult?.hasSavedSnapshot).toBe(false);
});
it('should create chart with saved snapshot and current instrument data', () => {
// Arrange
mockMoexChartState = {
timeframe: Timeframes['5m'],
savedData: JSON.stringify(mockSnapshot),
};
// Act
render(
<TestComponent
symbol="MOEX:GAZP"
symbolName="Газпром"
/>,
);
// Assert
expect(lastMoexChartConfig?.snapshot.charts[0]?.symbol).toBe('MOEX:GAZP');
expect(lastMoexChartConfig?.snapshot.charts[0]?.symbolName).toBe('Газпром');
expect(lastMoexChartConfig?.snapshot.charts[0]?.timeframe).toBe(Timeframes['5m']);
expect(hookResult?.hasSavedSnapshot).toBe(true);
});
it('should save chart snapshot to widget properties', () => {
// Arrange
render(
<TestComponent
symbol="MOEX:SBER"
symbolName="Сбербанк"
/>,
);
// Act
act(() => {
hookResult?.saveSnapshot();
});
// Assert
expect(mockGetSnapshot).toHaveBeenCalledTimes(1);
expect(mockUpdateProperties).toHaveBeenCalledTimes(1);
const updateCallback = mockUpdateProperties.mock.calls[0]?.[0] as UpdatePropertiesCallback;
const mockState: MockPropertiesState = {
moexChartState: {
timeframe: Timeframes['1m'],
},
};
updateCallback(mockState);
expect(mockState.moexChartState).toEqual({
timeframe: Timeframes['1m'],
savedData: JSON.stringify(mockSnapshot),
});
});
it('should serialize compare symbol and display name in snapshot', () => {
// Arrange
const snapshotWithIndicators: MockChartSnapshot = {
charts: [
{
symbol: 'MOEX:SBER',
symbolName: 'Сбербанк',
timeframe: Timeframes['1m'],
chartSeriesType: 'Candlestick',
panes: [
{
indicators: [
{
id: 'compare-gazp',
indicatorType: undefined,
dataSource: {
subscription: {},
},
config: {
symbol: 'MOEX:GAZP',
label: 'Газпром',
newPane: false,
series: [
{
name: 'Line',
seriesOptions: {
priceScaleId: 'left',
color: '#FFFFFF',
},
},
],
},
},
{
id: 'rsi',
indicatorType: 'RSI',
dataSource: {
subscription: {},
},
config: {
label: 'RSI',
newPane: true,
series: [
{
name: 'Line',
seriesOptions: {
priceScaleId: 'right',
},
},
],
},
},
],
},
],
},
],
};
mockGetSnapshot.mockReturnValue(snapshotWithIndicators);
render(
<TestComponent
symbol="MOEX:SBER"
symbolName="Сбербанк"
/>,
);
// Act
act(() => {
hookResult?.saveSnapshot();
});
const updateCallback = mockUpdateProperties.mock.calls[0]?.[0] as UpdatePropertiesCallback;
const mockState: MockPropertiesState = {
moexChartState: {
timeframe: Timeframes['1m'],
},
};
updateCallback(mockState);
const savedSnapshot = JSON.parse(mockState.moexChartState?.savedData ?? '{}') as MockChartSnapshot;
const [compareIndicator, regularIndicator] = savedSnapshot.charts[0]?.panes[0]?.indicators ?? [];
// Assert
expect(compareIndicator).toEqual({
id: 'compare-gazp',
config: {
symbol: 'MOEX:GAZP',
label: 'Газпром',
newPane: false,
series: [
{
name: 'Line',
seriesOptions: {
priceScaleId: 'left',
},
},
],
},
});
expect(regularIndicator).toEqual({
id: 'rsi',
indicatorType: 'RSI',
});
});
it('should not save snapshot when chart does not return snapshot', () => {
// Arrange
mockGetSnapshot.mockReturnValue(undefined);
render(<TestComponent />);
// Act
act(() => {
hookResult?.saveSnapshot();
});
// Assert
expect(mockGetSnapshot).toHaveBeenCalledTimes(1);
expect(mockUpdateProperties).not.toHaveBeenCalled();
});
it('should apply saved snapshot with current symbol, symbol name and timeframe', () => {
// Arrange
mockMoexChartState = {
timeframe: Timeframes['5m'],
savedData: JSON.stringify(mockSnapshot),
};
render(
<TestComponent
symbol="MOEX:SBER"
symbolName="Сбербанк"
/>,
);
// Act
act(() => {
hookResult?.applySnapshot();
});
// Assert
expect(mockSetSnapshot).toHaveBeenCalledWith({
...mockSnapshot,
charts: [
{
...mockSnapshot.charts[0],
symbol: 'MOEX:SBER',
symbolName: 'Сбербанк',
timeframe: Timeframes['5m'],
},
],
});
expect(hookResult?.compareManagerRef.current).toBe(mockCompareManager);
});
it('should update compare modal state', () => {
// Arrange
render(<TestComponent />);
// Act
act(() => {
hookResult?.setIsCompareOpen(true);
});
// Assert
expect(hookResult?.isCompareOpen).toBe(true);
});
it('should open compare modal from chart preset callback', () => {
// Arrange
render(<TestComponent />);
// Act
act(() => {
lastMoexChartConfig?.chartCollectionPreset.openCompareModal();
});
// Assert
expect(hookResult?.isCompareOpen).toBe(true);
});
it('should update symbol search modal state', () => {
// Arrange
render(<TestComponent />);
// Act
act(() => {
hookResult?.setIsSymbolSearchOpen(true);
});
// Assert
expect(hookResult?.isSymbolSearchOpen).toBe(true);
});
it('should open symbol search modal from chart preset callback', () => {
// Arrange
render(<TestComponent />);
// Act
act(() => {
lastMoexChartConfig?.chartCollectionPreset.openSymbolSearchModal();
});
// Assert
expect(hookResult?.isSymbolSearchOpen).toBe(true);
});
it('should update chart when external instrument changes', () => {
// Arrange
const { rerender } = render(
<TestComponent
symbol="MOEX:SBER"
symbolName="Сбербанк"
/>,
);
// Act
rerender(
<TestComponent
symbol="MOEX:GAZP"
symbolName="Газпром"
/>,
);
// Assert
expect(mockSetSymbol).toHaveBeenCalledTimes(1);
expect(mockSetSymbol).toHaveBeenCalledWith('MOEX:GAZP', 'Газпром');
});
it('should update chart when only external symbol name changes', () => {
// Arrange
const { rerender } = render(
<TestComponent
symbol="MOEX:SBER"
symbolName="Сбербанк"
/>,
);
// Act
rerender(
<TestComponent
symbol="MOEX:SBER"
symbolName="Сбербанк ПАО"
/>,
);
// Assert
expect(mockSetSymbol).toHaveBeenCalledTimes(1);
expect(mockSetSymbol).toHaveBeenCalledWith('MOEX:SBER', 'Сбербанк ПАО');
});
it('should use symbol as display name when symbol name is empty', () => {
// Arrange
const { rerender } = render(
<TestComponent
symbol="MOEX:SBER"
symbolName="Сбербанк"
/>,
);
// Act
rerender(
<TestComponent
symbol="MOEX:SBER"
symbolName=""
/>,
);
// Assert
expect(mockSetSymbol).toHaveBeenCalledTimes(1);
expect(mockSetSymbol).toHaveBeenCalledWith('MOEX:SBER', 'MOEX:SBER');
});
it('should not update chart when symbol and symbol name are unchanged', () => {
// Arrange
const { rerender } = render(
<TestComponent
symbol="MOEX:SBER"
symbolName="Сбербанк"
/>,
);
// Act
rerender(
<TestComponent
symbol="MOEX:SBER"
symbolName="Сбербанк"
/>,
);
// Assert
expect(mockSetSymbol).not.toHaveBeenCalled();
});
it('should not update chart when external symbol is empty', () => {
// Arrange
const { rerender } = render(
<TestComponent
symbol="MOEX:SBER"
symbolName="Сбербанк"
/>,
);
// Act
rerender(
<TestComponent
symbol=""
symbolName="Пустой инструмент"
/>,
);
// Assert
expect(mockSetSymbol).not.toHaveBeenCalled();
});
it('should not recreate chart when external instrument changes', () => {
// Arrange
const { rerender } = render(
<TestComponent
symbol="MOEX:SBER"
symbolName="Сбербанк"
/>,
);
// Act
rerender(
<TestComponent
symbol="MOEX:GAZP"
symbolName="Газпром"
/>,
);
// Assert
expect(mockMoexChart).toHaveBeenCalledTimes(1);
});
it('should apply saved snapshot with externally selected instrument', () => {
// Arrange
mockMoexChartState = {
timeframe: Timeframes['5m'],
savedData: JSON.stringify(mockSnapshot),
};
const { rerender } = render(
<TestComponent
symbol="MOEX:SBER"
symbolName="Сбербанк"
/>,
);
rerender(
<TestComponent
symbol="MOEX:GAZP"
symbolName="Газпром"
/>,
);
// Act
act(() => {
hookResult?.applySnapshot();
});
// Assert
expect(mockSetSnapshot).toHaveBeenCalledWith({
...mockSnapshot,
charts: [
{
...mockSnapshot.charts[0],
symbol: 'MOEX:GAZP',
symbolName: 'Газпром',
timeframe: Timeframes['5m'],
},
],
});
});
it('should initialize indicative instrument with id and display name', () => {
// Arrange
const indicativeData: ChartIndicativeData = {
id: 1,
title: 'Indicative instrument',
secId: 'INAV',
instrumentName: 'Индикатив',
settlement: 'Расчётный',
firmName: 'Тестовая фирма',
key: '2xOFZ:INAV',
};
// Act
render(
<TestComponent
symbol="2xOFZ:INAV"
symbolName="Индикатив Расчётный"
indicativeData={indicativeData}
/>,
);
// Assert
expect(mockGetDataSource).toHaveBeenCalledWith(indicativeData, expect.any(Function));
expect(mockSetSymbol).toHaveBeenCalledWith('2xOFZ:INAV', 'Индикатив Расчётный');
});
it('should pass realtime params to data source provider', () => {
// Arrange
render(<TestComponent />);
const getSymbols = jest.fn(() => ['MOEX:SBER']);
const getTimeframe = jest.fn(() => Timeframes['1m']);
const update = jest.fn();
// Act
const unsubscribe = lastMoexChartConfig?.chartCollectionPreset.startRealtime(getSymbols, getTimeframe, update);
// Assert
expect(mockStartRealtime).toHaveBeenCalledWith({
getSymbols,
getTimeframe,
update,
});
expect(unsubscribe).toBe(mockRealtimeUnsubscribe);
});
it('should update timeframe from data source callback', () => {
// Arrange
render(<TestComponent />);
const timeframeChangeCallback = mockGetDataSource.mock.calls[0]?.[1] as TimeframeChangeCallback;
// Act
act(() => {
timeframeChangeCallback(Timeframes['5m']);
});
// Assert
expect(mockUpdateProperties).toHaveBeenCalledTimes(1);
const updateCallback = mockUpdateProperties.mock.calls[0]?.[0] as UpdatePropertiesCallback;
const mockState: MockPropertiesState = {
moexChartState: {
timeframe: Timeframes['1m'],
},
};
updateCallback(mockState);
expect(mockState.moexChartState?.timeframe).toBe(Timeframes['5m']);
});
it('should not update timeframe when it is the same as current timeframe', () => {
// Arrange
render(<TestComponent />);
const timeframeChangeCallback = mockGetDataSource.mock.calls[0]?.[1] as TimeframeChangeCallback;
// Act
act(() => {
timeframeChangeCallback(Timeframes['1m']);
});
// Assert
expect(mockUpdateProperties).not.toHaveBeenCalled();
});
it('should destroy chart on unmount', () => {
// Arrange
const { unmount } = render(<TestComponent />);
// Act
unmount();
// Assert
expect(mockDestroy).toHaveBeenCalledTimes(1);
});
});
import { renderHook } from '@testing-library/react';
import { useAppSelect } from '@hooks/useAppSelector';
import { useContracts } from '@modules/contracts';
import { filterByUniqIssKey } from '@utils/filterByUniqIssKey';
import { DEFAULT_SYMBOL } from '@widgets/Chart/const';
import { useChartPublicContext } from '../hooks/useChartPublicContext';
import type { Contract } from '@modules/contracts';
// Mock the dependencies
jest.mock('@hooks/useAppSelector');
jest.mock('@modules/contracts');
jest.mock('@utils/filterByUniqIssKey');
jest.mock('@api/index', () => ({
updateMenuLocked: jest.fn(),
widgetsController: {
delete: jest.fn(),
},
widgetPropertiesController: {
update: jest.fn(),
},
workspaceController: {
update: jest.fn(),
},
}));
jest.mock('@api/controllers/workspace', () => ({
workspaceController: {
update: jest.fn(),
},
}));
describe('useChartPublicContext', () => {
const mockUseAppSelect = useAppSelect as jest.Mock;
const mockUseContracts = useContracts as jest.Mock;
const mockFilterByUniqIssKey = filterByUniqIssKey as jest.Mock;
const mockContracts = [
{
issKey: DEFAULT_SYMBOL,
displayName: 'Инструмент по умолчанию',
symbol: 'DEFAULT',
// ... other contract properties
},
{
issKey: 'MOEX:TEST1',
displayName: 'Test Instrument 1',
symbol: 'TEST1',
// ... other contract properties
},
{
issKey: 'MOEX:TEST2',
displayName: 'Test Instrument 2',
symbol: 'TEST2',
// ... other contract properties
},
{
issKey: 'MOEX:TEST3',
displayName: 'Test Instrument 3',
symbol: 'TEST3',
// ... other contract properties
},
] as Contract[];
const mockWidget = {
id: 1,
name: 'Test Widget',
type: 'graphic',
master: 123,
externalProperties: [
{
key: 'instrument',
value: 'MOEX:TEST1',
},
],
// ... other widget properties
};
const mockPublicContext = {
instrument: 'MOEX:TEST1',
};
beforeEach(() => {
jest.clearAllMocks();
// Mock the useAppSelect to return our test data
mockUseAppSelect.mockImplementation((selector) => {
if (selector.toString().includes('publicContext')) {
return mockPublicContext;
}
if (selector.toString().includes('widgets')) {
return mockWidget;
}
return {};
});
// Mock useContracts to return our test contracts
mockUseContracts.mockReturnValue({
contracts: mockContracts,
});
// Mock filterByUniqIssKey to return the same contracts
mockFilterByUniqIssKey.mockImplementation((contracts: Contract[]) => contracts);
});
it('should return the correct issKey when a matching instrument is found', () => {
// Arrange
const mockSetCurrInstrument = jest.fn();
const mockGetMasterInstrumentFromPublicContext = jest.fn().mockReturnValue('MOEX:TEST1');
// Act
const { result } = renderHook(() =>
useChartPublicContext({
widgetId: 1,
setCurrInstrument: mockSetCurrInstrument,
getMasterInstrumentFromPublicContext: mockGetMasterInstrumentFromPublicContext,
}),
);
// Assert
expect(result.current.issKey).toBe('MOEX:TEST1');
});
it('should pass instrument id and display name when a matching instrument is found', () => {
// Arrange
const mockSetCurrInstrument = jest.fn();
const mockGetMasterInstrumentFromPublicContext = jest.fn().mockReturnValue('MOEX:TEST2');
// Act
renderHook(() =>
useChartPublicContext({
widgetId: 1,
setCurrInstrument: mockSetCurrInstrument,
getMasterInstrumentFromPublicContext: mockGetMasterInstrumentFromPublicContext,
}),
);
// Assert
expect(mockSetCurrInstrument).toHaveBeenCalledTimes(1);
expect(mockSetCurrInstrument).toHaveBeenCalledWith('MOEX:TEST2', 'Test Instrument 2');
});
it('should return undefined when no matching instrument is found', () => {
// Arrange
const mockSetCurrInstrument = jest.fn();
const mockGetMasterInstrumentFromPublicContext = jest.fn().mockReturnValue('MOEX:NONEXISTENT');
// Act
const { result } = renderHook(() =>
useChartPublicContext({
widgetId: 1,
setCurrInstrument: mockSetCurrInstrument,
getMasterInstrumentFromPublicContext: mockGetMasterInstrumentFromPublicContext,
}),
);
// Assert
expect(result.current.issKey).toBeUndefined();
expect(mockSetCurrInstrument).not.toHaveBeenCalled();
});
it('should return undefined when getMasterInstrumentFromPublicContext returns null', () => {
// Arrange
const mockSetCurrInstrument = jest.fn();
const mockGetMasterInstrumentFromPublicContext = jest.fn().mockReturnValue(null);
// Act
const { result } = renderHook(() =>
useChartPublicContext({
widgetId: 1,
setCurrInstrument: mockSetCurrInstrument,
getMasterInstrumentFromPublicContext: mockGetMasterInstrumentFromPublicContext,
}),
);
// Assert
expect(result.current.issKey).toBeUndefined();
});
it('should return undefined when getMasterInstrumentFromPublicContext returns undefined', () => {
// Arrange
const mockSetCurrInstrument = jest.fn();
const mockGetMasterInstrumentFromPublicContext = jest.fn().mockReturnValue(undefined);
// Act
const { result } = renderHook(() =>
useChartPublicContext({
widgetId: 1,
setCurrInstrument: mockSetCurrInstrument,
getMasterInstrumentFromPublicContext: mockGetMasterInstrumentFromPublicContext,
}),
);
// Assert
expect(result.current.issKey).toBeUndefined();
});
it('should set current instrument to DEFAULT_SYMBOL with display name when no field value and widget has master', () => {
// Arrange
const mockSetCurrInstrument = jest.fn();
const mockGetMasterInstrumentFromPublicContext = jest.fn().mockReturnValue(null);
// Act
renderHook(() =>
useChartPublicContext({
widgetId: 1,
setCurrInstrument: mockSetCurrInstrument,
getMasterInstrumentFromPublicContext: mockGetMasterInstrumentFromPublicContext,
}),
);
// Assert
expect(mockSetCurrInstrument).toHaveBeenCalledTimes(1);
expect(mockSetCurrInstrument).toHaveBeenCalledWith(DEFAULT_SYMBOL, 'Инструмент по умолчанию');
});
it('should handle case when widget is not found in widgets array', () => {
// Arrange
const mockSetCurrInstrument = jest.fn();
const mockGetMasterInstrumentFromPublicContext = jest.fn().mockReturnValue('MOEX:TEST1');
// Mock useAppSelect to return null for widget
mockUseAppSelect.mockImplementation((selector) => {
if (selector.toString().includes('publicContext')) {
return mockPublicContext;
}
if (selector.toString().includes('widgets')) {
return null; // Widget not found
}
return {};
});
// Act
const { result } = renderHook(() =>
useChartPublicContext({
widgetId: 999, // Non-existent widget ID
setCurrInstrument: mockSetCurrInstrument,
getMasterInstrumentFromPublicContext: mockGetMasterInstrumentFromPublicContext,
}),
);
// Assert
expect(result.current.issKey).toBe('MOEX:TEST1');
expect(mockSetCurrInstrument).toHaveBeenCalledWith('MOEX:TEST1', 'Test Instrument 1');
});
it('should handle case when contracts array is empty', () => {
// Arrange
const mockSetCurrInstrument = jest.fn();
const mockGetMasterInstrumentFromPublicContext = jest.fn().mockReturnValue('MOEX:TEST1');
// Mock useContracts to return empty contracts
mockUseContracts.mockReturnValue({
contracts: [],
});
// Act
const { result } = renderHook(() =>
useChartPublicContext({
widgetId: 1,
setCurrInstrument: mockSetCurrInstrument,
getMasterInstrumentFromPublicContext: mockGetMasterInstrumentFromPublicContext,
}),
);
// Assert
expect(result.current.issKey).toBeUndefined();
expect(mockSetCurrInstrument).not.toHaveBeenCalled();
});
});
import { render } from '@testing-library/react';
import React from 'react';
import { InstrumentSearch } from '@components/InstrumentSearch';
import { SymbolSearchModal } from '../components/MoexChart/components/SymbolSearchModal';
import type { Contract } from '@modules/contracts';
jest.mock('@components/InstrumentSearch', () => ({
InstrumentSearch: jest.fn(() => null),
}));
interface InstrumentSearchMockProps {
widgetId: number;
variant: string;
isOpen: boolean;
setOpen: (isOpen: boolean) => void;
addInstruments: (instruments: Contract[]) => void;
}
describe('SymbolSearchModal', () => {
const mockInstrumentSearch = InstrumentSearch as jest.Mock;
const mockSetOpen = jest.fn();
const mockOnSymbolChange = jest.fn();
const renderComponent = (): InstrumentSearchMockProps => {
render(
<SymbolSearchModal
widgetId={42}
isOpen
setOpen={mockSetOpen}
onSymbolChange={mockOnSymbolChange}
/>,
);
return mockInstrumentSearch.mock.calls[0]?.[0] as InstrumentSearchMockProps;
};
beforeEach(() => {
jest.clearAllMocks();
});
it('should pass modal properties to instrument search', () => {
// Arrange & Act
const instrumentSearchProps = renderComponent();
// Assert
expect(instrumentSearchProps.widgetId).toBe(42);
expect(instrumentSearchProps.variant).toBe('single');
expect(instrumentSearchProps.isOpen).toBe(true);
expect(instrumentSearchProps.setOpen).toBe(mockSetOpen);
expect(instrumentSearchProps.addInstruments).toEqual(expect.any(Function));
});
it('should pass selected instrument after instrument selection', () => {
// Arrange
const instrumentSearchProps = renderComponent();
const instrument = {
issKey: 'MOEX:SBER',
displayName: 'Сбербанк',
} as Contract;
// Act
instrumentSearchProps.addInstruments([instrument]);
// Assert
expect(mockOnSymbolChange).toHaveBeenCalledTimes(1);
expect(mockOnSymbolChange).toHaveBeenCalledWith(instrument);
});
it('should close modal after instrument selection', () => {
// Arrange
const instrumentSearchProps = renderComponent();
const instrument = {
issKey: 'MOEX:SBER',
displayName: 'Сбербанк',
} as Contract;
// Act
instrumentSearchProps.addInstruments([instrument]);
// Assert
expect(mockSetOpen).toHaveBeenCalledTimes(1);
expect(mockSetOpen).toHaveBeenCalledWith(false);
});
it('should not change instrument when instruments list is empty', () => {
// Arrange
const instrumentSearchProps = renderComponent();
// Act
instrumentSearchProps.addInstruments([]);
// Assert
expect(mockOnSymbolChange).not.toHaveBeenCalled();
expect(mockSetOpen).not.toHaveBeenCalled();
});
it('should not change instrument when selected instrument has no issKey', () => {
// Arrange
const instrumentSearchProps = renderComponent();
// Act
instrumentSearchProps.addInstruments([{} as Contract]);
// Assert
expect(mockOnSymbolChange).not.toHaveBeenCalled();
expect(mockSetOpen).not.toHaveBeenCalled();
});
it('should not change instrument when selected instrument has empty issKey', () => {
// Arrange
const instrumentSearchProps = renderComponent();
const instrument = {
issKey: '',
displayName: 'Сбербанк',
} as Contract;
// Act
instrumentSearchProps.addInstruments([instrument]);
// Assert
expect(mockOnSymbolChange).not.toHaveBeenCalled();
expect(mockSetOpen).not.toHaveBeenCalled();
});
});
import { indicativeQuotesController } from '@api/controllers/indicativeQuotesController';
import api from '@api/index';
import { candleToBar } from '@utils/candleToBar';
import { requestBars, requestRealtimeBars } from '../requestBars';
import { isIndicativeTicker } from '../utils/isIndicativeTicker';
// Mock the dependencies
jest.mock('../utils/isIndicativeTicker');
jest.mock('@utils/candleToBar');
// Mock the api module
jest.mock('@api/index', () => ({
__esModule: true,
default: {
getBars: jest.fn(),
},
}));
// Mock the indicativeQuotesController module
jest.mock('@api/controllers/indicativeQuotesController', () => ({
indicativeQuotesController: {
getCandles: jest.fn(),
},
}));
describe('requestBars', () => {
const mockPeriodParams = {
countBack: 100,
from: 1640195200,
to: 1640995200, // 2022-01-01T00:00:00Z
firstDataRequest: true,
};
const mockCandle = {
open: 100,
close: 110,
high: 120,
low: 90,
ticker: 'TEST',
volume: 1000,
end: '2022-01-01T10:00:00Z',
interval: '1',
};
const mockBar = {
open: 100,
close: 110,
high: 120,
low: 90,
volume: 1000,
time: 1640995200000,
};
beforeEach(() => {
jest.clearAllMocks();
});
it('should handle indicative instrument correctly when indicativeData matches ticker', async () => {
// Arrange
(isIndicativeTicker as jest.Mock).mockReturnValue(false);
(candleToBar as jest.Mock).mockReturnValue(mockBar);
const mockIndicativeData = {
key: 'test-ticker',
secId: 'string',
instrumentName: 'string',
settlement: 'string',
firmName: 'string',
};
const mockResponse = {
data: {
indicativeCandles: [mockCandle],
},
};
(
indicativeQuotesController.getCandles as unknown as { mockResolvedValue: (mockResponse: object) => void }
).mockResolvedValue(mockResponse);
// Act
const result = await requestBars({
currencyPair: 'USD/RUB',
interval: '1',
periodParams: mockPeriodParams,
ticker: 'test-ticker',
indicativeData: mockIndicativeData,
});
// Assert
expect(indicativeQuotesController.getCandles).toHaveBeenCalledWith({
count: mockPeriodParams.countBack,
key: 'USD/RUB',
date: '2022-01-01T00:00:00',
interval: '1',
});
expect(result).toEqual([mockBar]);
});
it('should handle indicative instrument correctly when ticker contains indicative board', async () => {
// Arrange
(isIndicativeTicker as jest.Mock).mockReturnValue(true);
(candleToBar as jest.Mock).mockReturnValue(mockBar);
const mockResponse = {
data: {
indicativeCandles: [mockCandle],
},
};
(
indicativeQuotesController.getCandles as unknown as { mockResolvedValue: (mockResponse: object) => void }
).mockResolvedValue(mockResponse);
// Act
const result = await requestBars({
currencyPair: 'USD/RUB',
interval: '1',
periodParams: mockPeriodParams,
ticker: 'TEST.indicative_spot',
});
// Assert
expect(indicativeQuotesController.getCandles).toHaveBeenCalledWith({
count: mockPeriodParams.countBack,
key: 'USD/RUB',
date: '2022-01-01T00:00:00',
interval: '1',
});
expect(result).toEqual([mockBar]);
});
it('should handle non-indicative instrument correctly', async () => {
// Arrange
(isIndicativeTicker as jest.Mock).mockReturnValue(false);
(candleToBar as jest.Mock).mockReturnValue(mockBar);
const mockResponse = {
data: [mockCandle],
};
(api.getBars as unknown as { mockResolvedValue: (mockResponse: object) => void }).mockResolvedValue(mockResponse);
// Act
const result = await requestBars({
currencyPair: 'USD/RUB',
interval: '1',
periodParams: mockPeriodParams,
ticker: 'TEST',
});
// Assert
expect(api.getBars).toHaveBeenCalledWith({
currencyPair: 'USD/RUB',
date: '2022-01-01%2000:00:00',
interval: '1',
count: mockPeriodParams.countBack,
ticker: 'TEST',
});
expect(result).toEqual([mockBar]);
});
it('should call onHistoryCallback with data when indicative instrument succeeds', async () => {
// Arrange
(isIndicativeTicker as jest.Mock).mockReturnValue(true);
(candleToBar as jest.Mock).mockReturnValue(mockBar);
const mockResponse = {
data: {
indicativeCandles: [mockCandle],
},
};
(
indicativeQuotesController.getCandles as unknown as { mockResolvedValue: (mockResponse: object) => void }
).mockResolvedValue(mockResponse);
const mockHistoryCallback = jest.fn();
// Act
await requestBars({
currencyPair: 'USD/RUB',
interval: '1',
periodParams: mockPeriodParams,
onHistoryCallback: mockHistoryCallback,
ticker: 'TEST.indicative_spot',
});
// Assert
expect(mockHistoryCallback).toHaveBeenCalledWith([mockBar], { noData: false });
});
it('should call onHistoryCallback with empty array and noData flag when indicative instrument fails', async () => {
// Arrange
(isIndicativeTicker as jest.Mock).mockReturnValue(true);
(
indicativeQuotesController.getCandles as unknown as { mockRejectedValue: (mockResponse: object) => void }
).mockRejectedValue(new Error('API Error'));
const mockHistoryCallback = jest.fn();
// Act
const result = await requestBars({
currencyPair: 'USD/RUB',
interval: '1',
periodParams: mockPeriodParams,
onHistoryCallback: mockHistoryCallback,
ticker: 'TEST.indicative_spot',
});
// Assert
expect(mockHistoryCallback).toHaveBeenCalledWith([], { noData: true });
expect(result).toEqual([]);
});
it('should call onHistoryCallback with empty array and noData flag when non-indicative instrument fails', async () => {
// Arrange
(isIndicativeTicker as jest.Mock).mockReturnValue(false);
(api.getBars as unknown as { mockRejectedValue: (mockResponse: object) => void }).mockRejectedValue(
new Error('API Error'),
);
const mockHistoryCallback = jest.fn();
// Act
const result = await requestBars({
currencyPair: 'USD/RUB',
interval: '1',
periodParams: mockPeriodParams,
onHistoryCallback: mockHistoryCallback,
ticker: 'TEST',
});
// Assert
expect(mockHistoryCallback).toHaveBeenCalledWith([], { noData: true });
expect(result).toEqual([]);
});
it('should handle multiple candles for indicative instrument', async () => {
// Arrange
(isIndicativeTicker as jest.Mock).mockReturnValue(true);
(candleToBar as jest.Mock).mockReturnValue(mockBar);
const mockCandles = [
{ ...mockCandle, end: '2022-01-01T10:00:00Z' },
{ ...mockCandle, end: '2022-01-01T11:00:00Z' },
{ ...mockCandle, end: '2022-01-01T12:00:00Z' },
];
const mockResponse = {
data: {
indicativeCandles: mockCandles,
},
};
(
indicativeQuotesController.getCandles as unknown as { mockResolvedValue: (mockResponse: object) => void }
).mockResolvedValue(mockResponse);
// Act
const result = await requestBars({
currencyPair: 'USD/RUB',
interval: '1',
periodParams: mockPeriodParams,
ticker: 'TEST.indicative_spot',
});
// Assert
expect(indicativeQuotesController.getCandles).toHaveBeenCalledWith({
count: mockPeriodParams.countBack,
key: 'USD/RUB',
date: '2022-01-01T00:00:00',
interval: '1',
});
expect(candleToBar).toHaveBeenCalledTimes(3);
expect(result).toEqual([mockBar, mockBar, mockBar]);
});
it('should handle multiple candles for non-indicative instrument', async () => {
// Arrange
(isIndicativeTicker as jest.Mock).mockReturnValue(false);
(candleToBar as jest.Mock).mockReturnValue(mockBar);
const mockCandles = [
{ ...mockCandle, end: '2022-01-01T10:00:00Z' },
{ ...mockCandle, end: '2022-01-01T11:00:00Z' },
{ ...mockCandle, end: '2022-01-01T12:00:00Z' },
];
const mockResponse = {
data: mockCandles,
};
(api.getBars as unknown as { mockResolvedValue: (mockResponse: object) => void }).mockResolvedValue(mockResponse);
// Act
const result = await requestBars({
currencyPair: 'USD/RUB',
interval: '1',
periodParams: mockPeriodParams,
ticker: 'TEST',
});
// Assert
expect(api.getBars).toHaveBeenCalledWith({
currencyPair: 'USD/RUB',
date: '2022-01-01%2000:00:00',
interval: '1',
count: mockPeriodParams.countBack,
ticker: 'TEST',
});
expect(candleToBar).toHaveBeenCalledTimes(3);
expect(result).toEqual([mockBar, mockBar, mockBar]);
});
it('should handle empty indicativeCandles array', async () => {
// Arrange
(isIndicativeTicker as jest.Mock).mockReturnValue(true);
const mockResponse = {
data: {
indicativeCandles: [],
},
};
(
indicativeQuotesController.getCandles as unknown as { mockResolvedValue: (mockResponse: object) => void }
).mockResolvedValue(mockResponse);
const mockHistoryCallback = jest.fn();
// Act
const result = await requestBars({
currencyPair: 'USD/RUB',
interval: '1',
periodParams: mockPeriodParams,
onHistoryCallback: mockHistoryCallback,
ticker: 'TEST.indicative_spot',
});
// Assert
expect(mockHistoryCallback).toHaveBeenCalledWith([], { noData: true });
expect(result).toEqual([]);
});
it('should handle empty bars array for non-indicative instrument', async () => {
// Arrange
(isIndicativeTicker as jest.Mock).mockReturnValue(false);
const mockResponse = {
data: [],
};
(api.getBars as unknown as { mockResolvedValue: (mockResponse: object) => void }).mockResolvedValue(mockResponse);
const mockHistoryCallback = jest.fn();
// Act
const result = await requestBars({
currencyPair: 'USD/RUB',
interval: '1',
periodParams: mockPeriodParams,
onHistoryCallback: mockHistoryCallback,
ticker: 'TEST',
});
// Assert
expect(mockHistoryCallback).toHaveBeenCalledWith([], { noData: true });
expect(result).toEqual([]);
});
});
describe('requestRealtimeBars', () => {
const mockCandle = {
open: 100,
close: 110,
high: 120,
low: 90,
ticker: 'TEST',
volume: 1000,
end: '2022-01-01T10:00:00Z',
interval: '1',
};
const mockBar = {
open: 100,
close: 110,
high: 120,
low: 90,
volume: 1000,
time: 1640995200000,
};
beforeEach(() => {
jest.clearAllMocks();
});
it('should handle indicative instrument correctly', async () => {
// Arrange
(isIndicativeTicker as jest.Mock).mockReturnValue(true);
(candleToBar as jest.Mock).mockReturnValue(mockBar);
const mockResponse = {
data: {
indicativeCandles: [mockCandle],
},
};
(
indicativeQuotesController.getCandles as unknown as { mockResolvedValue: (mockResponse: object) => void }
).mockResolvedValue(mockResponse);
// Act
const result = await requestRealtimeBars({
currencyPair: 'USD/RUB',
interval: '1',
ticker: 'TEST.indicative_spot',
});
// Assert
expect(indicativeQuotesController.getCandles).toHaveBeenCalledWith({
count: 1,
key: 'USD/RUB',
date: expect.any(String), // We can't predict the exact date string
interval: '1',
});
expect(candleToBar).toHaveBeenCalledWith(mockCandle);
expect(result).toEqual(mockBar);
});
it('should handle non-indicative instrument correctly', async () => {
// Arrange
(isIndicativeTicker as jest.Mock).mockReturnValue(false);
(candleToBar as jest.Mock).mockReturnValue(mockBar);
const mockResponse = {
data: [mockCandle],
};
(api.getBars as unknown as { mockResolvedValue: (mockResponse: object) => void }).mockResolvedValue(mockResponse);
// Act
const result = await requestRealtimeBars({
currencyPair: 'USD/RUB',
interval: '1',
ticker: 'TEST',
});
// Assert
expect(api.getBars).toHaveBeenCalledWith({
currencyPair: 'USD/RUB',
date: expect.any(String), // We can't predict the exact date string
interval: '1',
count: 1,
ticker: 'TEST',
});
expect(candleToBar).toHaveBeenCalledWith(mockCandle);
expect(result).toEqual(mockBar);
});
it('should handle empty indicativeCandles array in realtime request', async () => {
// Arrange
(isIndicativeTicker as jest.Mock).mockReturnValue(true);
const mockResponse = {
data: {
indicativeCandles: [],
},
};
(
indicativeQuotesController.getCandles as unknown as { mockResolvedValue: (mockResponse: object) => void }
).mockResolvedValue(mockResponse);
// Act
const result = await requestRealtimeBars({
currencyPair: 'USD/RUB',
interval: '1',
ticker: 'TEST.indicative_spot',
});
// Assert
expect(result).toBeUndefined();
});
it('should handle empty bars array in realtime request', async () => {
// Arrange
(isIndicativeTicker as jest.Mock).mockReturnValue(false);
const mockResponse = {
data: [],
};
(api.getBars as unknown as { mockResolvedValue: (mockResponse: object) => void }).mockResolvedValue(mockResponse);
// Act
const result = await requestRealtimeBars({
currencyPair: 'USD/RUB',
interval: '1',
ticker: 'TEST',
});
// Assert
expect(result).toBeUndefined();
});
it('should call onRealtimeCallback with bar when indicative instrument succeeds', async () => {
// Arrange
(isIndicativeTicker as jest.Mock).mockReturnValue(true);
(candleToBar as jest.Mock).mockReturnValue(mockBar);
const mockResponse = {
data: {
indicativeCandles: [mockCandle],
},
};
(
indicativeQuotesController.getCandles as unknown as { mockResolvedValue: (mockResponse: object) => void }
).mockResolvedValue(mockResponse);
const mockRealtimeCallback = jest.fn();
// Act
await requestRealtimeBars({
currencyPair: 'USD/RUB',
interval: '1',
ticker: 'TEST.indicative_spot',
onRealtimeCallback: mockRealtimeCallback,
});
// Assert
expect(mockRealtimeCallback).toHaveBeenCalledWith(mockBar);
});
it('should call onRealtimeCallback with bar when non-indicative instrument succeeds', async () => {
// Arrange
(isIndicativeTicker as jest.Mock).mockReturnValue(false);
(candleToBar as jest.Mock).mockReturnValue(mockBar);
const mockResponse = {
data: [mockCandle],
};
(api.getBars as unknown as { mockResolvedValue: (mockResponse: object) => void }).mockResolvedValue(mockResponse);
const mockRealtimeCallback = jest.fn();
// Act
await requestRealtimeBars({
currencyPair: 'USD/RUB',
interval: '1',
ticker: 'TEST',
onRealtimeCallback: mockRealtimeCallback,
});
// Assert
expect(mockRealtimeCallback).toHaveBeenCalledWith(mockBar);
});
it('should handle error in indicative instrument request', async () => {
// Arrange
(isIndicativeTicker as jest.Mock).mockReturnValue(true);
(
indicativeQuotesController.getCandles as unknown as { mockRejectedValue: (mockResponse: object) => void }
).mockRejectedValue(new Error('API Error'));
const mockRealtimeCallback = jest.fn();
// Act & Assert
await expect(
requestRealtimeBars({
currencyPair: 'USD/RUB',
interval: '1',
ticker: 'TEST.indicative_spot',
onRealtimeCallback: mockRealtimeCallback,
}),
).resolves.toBeUndefined();
});
it('should handle error in non-indicative instrument request', async () => {
// Arrange
(isIndicativeTicker as jest.Mock).mockReturnValue(false);
(api.getBars as unknown as { mockRejectedValue: (mockResponse: object) => void }).mockRejectedValue(
new Error('API Error'),
);
const mockRealtimeCallback = jest.fn();
// Act & Assert
await expect(
requestRealtimeBars({
currencyPair: 'USD/RUB',
interval: '1',
ticker: 'TEST',
onRealtimeCallback: mockRealtimeCallback,
}),
).resolves.toBeUndefined();
});
});
import type { Timeframes as TimeframesType } from 'moex-chart';
type DataSourceProvideModule = typeof import('@widgets/Chart/components/MoexChart/dataSourceProvide');
const mockRequestBars = jest.fn();
const mockRequestRealtimeBars = jest.fn();
const mockMoexChartTimeConverter = jest.fn();
const mockParseTimeframe = jest.fn();
const mockGetStartTime = jest.fn();
const mockMoexChartToIssTimeframe = jest.fn();
const mockGetISSOddTimeframePart = jest.fn();
// Mock the dependencies
jest.mock('moex-chart', () => ({
Timeframes: {
'10s': '10s',
'1m': '1m',
'5m': '5m',
},
parseTimeframe: mockParseTimeframe,
getStartTime: mockGetStartTime,
}));
jest.mock('@utils/chartToReqTimeConverter', () => ({
moexChartTimeConverter: mockMoexChartTimeConverter,
moexChartToIssTimeframe: mockMoexChartToIssTimeframe,
getISSOddTimeframePart: mockGetISSOddTimeframePart,
}));
jest.mock('../requestBars', () => ({
requestBars: mockRequestBars,
requestRealtimeBars: mockRequestRealtimeBars,
}));
jest.mock('@widgets/Chart/requestBars', () => ({
requestBars: mockRequestBars,
requestRealtimeBars: mockRequestRealtimeBars,
}));
const { DataSourceProvider } = jest.requireActual(
'@widgets/Chart/components/MoexChart/dataSourceProvide',
) as DataSourceProvideModule;
const Timeframes = {
'10s': '10s' as TimeframesType,
'1m': '1m' as TimeframesType,
'5m': '5m' as TimeframesType,
};
const flushPromises = async (): Promise<void> => {
await Promise.resolve();
await Promise.resolve();
};
describe('DataSourceProvider', () => {
const mockBar = {
time: 1640995200,
open: 100,
close: 110,
high: 120,
low: 90,
volume: 1000,
};
const baseTime = Date.parse('2026-05-19T10:00:00Z');
const createBar = (minute: number, overrides: Partial<typeof mockBar> = {}) => ({
time: baseTime + minute * 60_000,
open: 100 + minute,
close: 101 + minute,
high: 102 + minute,
low: 99 - minute,
volume: minute + 1,
...overrides,
});
const configureFiveMinuteConvolution = (): void => {
mockMoexChartToIssTimeframe.mockReturnValue(Timeframes['1m']);
mockParseTimeframe.mockImplementation((timeframe: TimeframesType) =>
timeframe === Timeframes['5m']
? {
candleWidth: 5,
dayjsUnit: 'minute',
}
: {
candleWidth: 1,
dayjsUnit: 'minute',
},
);
mockGetStartTime.mockImplementation((timeframe: TimeframesType, time: number) => {
const intervalMs = timeframe === Timeframes['5m'] ? 5 * 60_000 : 60_000;
return (Math.floor(time / intervalMs) * intervalMs) / 1000;
});
};
beforeEach(() => {
jest.clearAllMocks();
jest.useFakeTimers();
jest.setSystemTime(new Date('2026-05-19T10:00:00Z'));
mockMoexChartTimeConverter.mockReturnValue('1');
mockMoexChartToIssTimeframe.mockImplementation((timeframe: TimeframesType) => timeframe);
mockGetISSOddTimeframePart.mockReturnValue({
value: 0,
unit: 'minute',
});
mockParseTimeframe.mockReturnValue({
candleWidth: 1,
dayjsUnit: 'minute',
});
mockGetStartTime.mockImplementation((_timeframe: TimeframesType, time: number) => Math.floor(time / 1000));
});
afterEach(() => {
jest.useRealTimers();
});
it('should request chart history data with converted timeframe', async () => {
// Arrange
const mockTimeframeCallback = jest.fn();
// mockMoexChartTimeConverter.mockReturnValue('1');
mockRequestBars.mockResolvedValue([mockBar]);
const provider = new DataSourceProvider();
const dataSource = provider.getDataSource(undefined, mockTimeframeCallback);
// Act
const result = await dataSource(Timeframes['1m'], 'MOEX:SBER');
// Assert
expect(mockTimeframeCallback).toHaveBeenCalledWith(Timeframes['1m']);
expect(mockMoexChartTimeConverter).toHaveBeenCalledWith(Timeframes['1m']);
expect(mockRequestBars).toHaveBeenCalledWith({
currencyPair: 'MOEX.SBER',
interval: '1',
periodParams: {
firstDataRequest: true,
to: Math.round(Date.now() / 1000),
from: Date.now(),
countBack: 2000,
},
ticker: 'MOEX:SBER',
indicativeData: undefined,
});
expect(result).toEqual([mockBar]);
});
it('should request chart history data with indicative data', async () => {
// Arrange
const indicativeData = {
id: 1,
title: 'Test instrument',
secId: 'SBER',
instrumentName: 'SBER',
settlement: 'TQBR',
firmName: 'Test firm',
key: 'SBER_TBQR',
};
// mockMoexChartTimeConverter.mockReturnValue('1');
mockRequestBars.mockResolvedValue([mockBar]);
const provider = new DataSourceProvider();
const dataSource = provider.getDataSource(indicativeData);
// Act
await dataSource(Timeframes['1m'], 'MOEX:SBER');
// Assert
expect(mockRequestBars).toHaveBeenCalledWith(
expect.objectContaining({
indicativeData,
}),
);
});
it('should use until time when it is provided', async () => {
// Arrange
mockMoexChartTimeConverter.mockReturnValue('5');
mockRequestBars.mockResolvedValue([mockBar]);
const provider = new DataSourceProvider();
const dataSource = provider.getDataSource();
const until = { time: 111 } as NonNullable<Parameters<typeof dataSource>[2]>;
// Act
await dataSource(Timeframes['5m'], 'MOEX:GAZP', until);
// Assert
expect(mockRequestBars).toHaveBeenCalledWith(
expect.objectContaining({
periodParams: expect.objectContaining({
to: 111,
}),
}),
);
});
it('should return null when history data is empty', async () => {
// Arrange
// mockMoexChartTimeConverter.mockReturnValue('1');
mockRequestBars.mockResolvedValue([]);
const provider = new DataSourceProvider();
const dataSource = provider.getDataSource();
// Act
const result = await dataSource(Timeframes['1m'], 'MOEX:SBER');
// Assert
expect(result).toBeNull();
});
it('should request realtime data and update normalized symbol', async () => {
// Arrange
const provider = new DataSourceProvider();
const mockUpdate = jest.fn();
// mockMoexChartTimeConverter.mockReturnValue('1');
const localMockedBar = {
time: 1640995200,
open: 100,
close: 110,
high: 120,
low: 90,
volume: Math.floor(Math.random() * 1000),
};
mockRequestRealtimeBars.mockImplementation(() => localMockedBar);
provider.startRealtime({
getSymbols: () => [' moex:sber '],
getTimeframe: () => Timeframes['1m'],
update: mockUpdate,
periodMs: 1000,
});
// Act
jest.advanceTimersByTime(1000);
await flushPromises();
// Assert
expect(mockRequestRealtimeBars).toHaveBeenCalledWith({
currencyPair: 'MOEX.SBER',
interval: '1',
ticker: 'MOEX:SBER',
indicativeData: undefined,
});
expect(mockUpdate).toHaveBeenCalledWith('MOEX:SBER', localMockedBar);
});
it('should request realtime data with indicative data', async () => {
// Arrange
const provider = new DataSourceProvider();
const mockUpdate = jest.fn();
const indicativeData = {
id: 1,
title: 'Test instrument',
secId: 'SBER',
instrumentName: 'SBER',
settlement: 'TQBR',
firmName: 'Test firm',
key: 'SBER_TBQR',
};
// mockMoexChartTimeConverter.mockReturnValue('1');
mockRequestRealtimeBars.mockResolvedValue(mockBar);
provider.startRealtime({
getSymbols: () => ['MOEX:SBER'],
getTimeframe: () => Timeframes['1m'],
update: mockUpdate,
periodMs: 1000,
indicativeData,
});
// Act
jest.advanceTimersByTime(1000);
await flushPromises();
// Assert
expect(mockRequestRealtimeBars).toHaveBeenCalledWith(
expect.objectContaining({
indicativeData,
}),
);
});
it('should not call update when realtime data is empty', async () => {
// Arrange
const provider = new DataSourceProvider();
const mockUpdate = jest.fn();
// mockMoexChartTimeConverter.mockReturnValue('1');
mockRequestRealtimeBars.mockResolvedValue(undefined);
provider.startRealtime({
getSymbols: () => ['MOEX:SBER'],
getTimeframe: () => Timeframes['1m'],
update: mockUpdate,
periodMs: 1000,
});
// Act
jest.advanceTimersByTime(1000);
await flushPromises();
// Assert
expect(mockUpdate).not.toHaveBeenCalled();
});
it('should not request realtime data when symbols list is empty', async () => {
// Arrange
const provider = new DataSourceProvider();
// mockMoexChartTimeConverter.mockReturnValue('1');
mockRequestRealtimeBars.mockResolvedValue(mockBar);
provider.startRealtime({
getSymbols: () => [],
getTimeframe: () => Timeframes['1m'],
update: jest.fn(),
periodMs: 1000,
});
// Act
jest.advanceTimersByTime(1000);
await flushPromises();
// Assert
expect(mockRequestRealtimeBars).not.toHaveBeenCalled();
});
it('should clear realtime timer on unsubscribe', async () => {
// Arrange
const provider = new DataSourceProvider();
// mockMoexChartTimeConverter.mockReturnValue('1');
mockRequestRealtimeBars.mockResolvedValue(mockBar);
const unsubscribe = provider.startRealtime({
getSymbols: () => ['MOEX:SBER'],
getTimeframe: () => Timeframes['1m'],
update: jest.fn(),
periodMs: 1000,
});
// Act
unsubscribe();
jest.advanceTimersByTime(1000);
await flushPromises();
// Assert
expect(mockRequestRealtimeBars).not.toHaveBeenCalled();
});
it('should convolve history data when ISS timeframe differs', async () => {
// Arrange
configureFiveMinuteConvolution();
const historyData = Array.from({ length: 11 }, (_, minute) => createBar(minute));
mockRequestBars.mockResolvedValue(historyData);
const provider = new DataSourceProvider();
const dataSource = provider.getDataSource();
// Act
const result = await dataSource(Timeframes['5m'], 'MOEX:SBER');
// Assert
expect(result).toEqual([
{
time: baseTime + 5 * 60_000,
open: 105,
close: 110,
high: 111,
low: 90,
volume: 40,
},
]);
});
it('should convolve incomplete history candle group', async () => {
// Arrange
configureFiveMinuteConvolution();
const historyData = Array.from({ length: 8 }, (_, minute) => createBar(minute));
mockRequestBars.mockResolvedValue(historyData);
const provider = new DataSourceProvider();
const dataSource = provider.getDataSource();
// Act
const result = await dataSource(Timeframes['5m'], 'MOEX:SBER');
// Assert
expect(result).toEqual([
{
time: baseTime + 5 * 60_000,
open: 105,
close: 108,
high: 109,
low: 92,
volume: 21,
},
]);
});
it('should stop candle group when candle is outside current interval', async () => {
// Arrange
configureFiveMinuteConvolution();
const historyData = [
createBar(0),
createBar(1),
createBar(2),
createBar(3),
createBar(4),
createBar(5),
createBar(11),
createBar(12),
];
mockRequestBars.mockResolvedValue(historyData);
const provider = new DataSourceProvider();
const dataSource = provider.getDataSource();
// Act
const result = await dataSource(Timeframes['5m'], 'MOEX:SBER');
// Assert
expect(result).toEqual([
{
time: baseTime + 5 * 60_000,
open: 105,
close: 112,
high: 113,
low: 88,
volume: 18,
},
]);
});
it('should return empty data when convolution start index is not found', async () => {
// Arrange
configureFiveMinuteConvolution();
mockGetStartTime.mockReturnValue(1);
mockRequestBars.mockResolvedValue([createBar(0), createBar(1)]);
const provider = new DataSourceProvider();
const dataSource = provider.getDataSource();
// Act
const result = await dataSource(Timeframes['5m'], 'MOEX:SBER');
// Assert
expect(result).toEqual([]);
});
it('should append, replace and reset realtime candles during convolution', async () => {
// Arrange
configureFiveMinuteConvolution();
mockRequestBars.mockResolvedValue(Array.from({ length: 11 }, (_, minute) => createBar(minute)));
const provider = new DataSourceProvider();
await provider.getDataSource()(Timeframes['5m'], 'MOEX:SBER');
const firstCandle = {
time: baseTime + 5 * 60_000,
open: 100,
high: 103,
low: 99,
close: 102,
volume: 10,
};
const secondCandle = {
time: baseTime + 6 * 60_000,
open: 102,
high: 106,
low: 98,
close: 105,
volume: 20,
};
const updatedSecondCandle = {
...secondCandle,
high: 107,
low: 97,
close: 106,
volume: 25,
};
const nextTimeframeCandle = {
time: baseTime + 10 * 60_000,
open: 106,
high: 108,
low: 105,
close: 107,
volume: 30,
};
mockRequestRealtimeBars
.mockResolvedValueOnce(firstCandle)
.mockResolvedValueOnce(secondCandle)
.mockResolvedValueOnce(updatedSecondCandle)
.mockResolvedValueOnce(nextTimeframeCandle);
const mockUpdate = jest.fn();
const unsubscribe = provider.startRealtime({
getSymbols: () => ['MOEX:SBER'],
getTimeframe: () => Timeframes['5m'],
update: mockUpdate,
periodMs: 1000,
});
// Act
jest.advanceTimersByTime(1000);
await flushPromises();
jest.advanceTimersByTime(1000);
await flushPromises();
jest.advanceTimersByTime(1000);
await flushPromises();
jest.advanceTimersByTime(1000);
await flushPromises();
unsubscribe();
// Assert
expect(mockUpdate).toHaveBeenNthCalledWith(1, 'MOEX:SBER', firstCandle);
expect(mockUpdate).toHaveBeenNthCalledWith(2, 'MOEX:SBER', {
time: firstCandle.time,
open: firstCandle.open,
high: 106,
low: 98,
close: 105,
volume: 30,
});
expect(mockUpdate).toHaveBeenNthCalledWith(3, 'MOEX:SBER', {
time: firstCandle.time,
open: firstCandle.open,
high: 107,
low: 97,
close: 106,
volume: 35,
});
expect(mockUpdate).toHaveBeenNthCalledWith(4, 'MOEX:SBER', nextTimeframeCandle);
});
it('should update distinct realtime candles without convolution', async () => {
// Arrange
const provider = new DataSourceProvider();
const mockUpdate = jest.fn();
const firstCandle = {
...mockBar,
close: 110,
};
const secondCandle = {
...mockBar,
close: 111,
};
mockRequestRealtimeBars.mockResolvedValueOnce(firstCandle).mockResolvedValueOnce(secondCandle);
const unsubscribe = provider.startRealtime({
getSymbols: () => ['MOEX:SBER'],
getTimeframe: () => Timeframes['1m'],
update: mockUpdate,
periodMs: 1000,
});
// Act
jest.advanceTimersByTime(1000);
await flushPromises();
jest.advanceTimersByTime(1000);
await flushPromises();
unsubscribe();
// Assert
expect(mockUpdate).toHaveBeenNthCalledWith(1, 'MOEX:SBER', firstCandle);
expect(mockUpdate).toHaveBeenNthCalledWith(2, 'MOEX:SBER', secondCandle);
});
it('should not update chart for duplicated realtime candle', async () => {
// Arrange
const provider = new DataSourceProvider();
const mockUpdate = jest.fn();
const realtimeCandle = {
time: 1640995200,
open: 100,
close: 110,
high: 120,
low: 90,
volume: 1000,
};
mockRequestRealtimeBars.mockResolvedValue(realtimeCandle);
const unsubscribe = provider.startRealtime({
getSymbols: () => ['MOEX:SBER'],
getTimeframe: () => Timeframes['1m'],
update: mockUpdate,
periodMs: 1000,
});
// Act
jest.advanceTimersByTime(1000);
await flushPromises();
jest.advanceTimersByTime(1000);
await flushPromises();
unsubscribe();
// Assert
expect(mockRequestRealtimeBars).toHaveBeenCalledTimes(2);
expect(mockUpdate).toHaveBeenCalledTimes(1);
expect(mockUpdate).toHaveBeenCalledWith('MOEX:SBER', realtimeCandle);
});
it('should not request realtime data for empty normalized symbol', async () => {
// Arrange
const provider = new DataSourceProvider();
const unsubscribe = provider.startRealtime({
getSymbols: () => [' '],
getTimeframe: () => Timeframes['1m'],
update: jest.fn(),
periodMs: 1000,
});
// Act
jest.advanceTimersByTime(1000);
await flushPromises();
unsubscribe();
// Assert
expect(mockRequestRealtimeBars).not.toHaveBeenCalled();
});
it('should replace existing realtime timer when startRealtime is called again', async () => {
// Arrange
const provider = new DataSourceProvider();
const mockUpdate = jest.fn();
mockRequestRealtimeBars.mockResolvedValue(mockBar);
provider.startRealtime({
getSymbols: () => ['MOEX:SBER'],
getTimeframe: () => Timeframes['1m'],
update: mockUpdate,
periodMs: 1000,
});
const unsubscribe = provider.startRealtime({
getSymbols: () => ['MOEX:SBER'],
getTimeframe: () => Timeframes['1m'],
update: mockUpdate,
periodMs: 1000,
});
// Act
jest.advanceTimersByTime(1000);
await flushPromises();
unsubscribe();
// Assert
expect(mockRequestRealtimeBars).toHaveBeenCalledTimes(1);
expect(mockUpdate).toHaveBeenCalledTimes(1);
});
});