|
| 1 | +import { ServerTime } from '@deriv/bot-skeleton'; |
| 2 | +import { LocalStore } from '@deriv/shared'; |
| 3 | +import { mockStore } from '@deriv/stores'; |
| 4 | +import { TStores } from '@deriv/stores/types'; |
| 5 | +import { mock_ws } from 'Utils/mock'; |
| 6 | +import { mockDBotStore } from 'Stores/useDBotStore'; |
| 7 | +import ChartStore, { g_subscribers_map } from '../chart-store'; |
| 8 | +import 'Utils/mock/mock-local-storage'; |
| 9 | + |
| 10 | +window.Blockly = { |
| 11 | + derivWorkspace: { |
| 12 | + getAllBlocks: jest.fn(() => ({ |
| 13 | + find: jest.fn(), |
| 14 | + })), |
| 15 | + }, |
| 16 | +}; |
| 17 | + |
| 18 | +jest.mock('@deriv/bot-skeleton/src/scratch/dbot', () => ({})); |
| 19 | + |
| 20 | +describe('ChartStore', () => { |
| 21 | + const mock_store: TStores = mockStore({ |
| 22 | + common: { |
| 23 | + server_time: { |
| 24 | + clone: jest.fn(() => ({ |
| 25 | + unix: jest.fn(() => '2024-03-11T10:56:02.239Z'), |
| 26 | + })), |
| 27 | + }, |
| 28 | + }, |
| 29 | + }); |
| 30 | + |
| 31 | + let chartStore: ChartStore; |
| 32 | + const mock_DBot_store = mockDBotStore(mock_store, mock_ws); |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + ServerTime.init(mock_store.common); |
| 36 | + chartStore = new ChartStore(mock_DBot_store); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should initialize ChartStore with default values', () => { |
| 40 | + expect(chartStore.symbol).toBeUndefined(); |
| 41 | + expect(chartStore.is_chart_loading).toBeUndefined(); |
| 42 | + expect(chartStore.chart_type).toBe('line'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should return true when contracts exist and the first contract is ended', () => { |
| 46 | + const mockContractStore = { |
| 47 | + contracts: [{ is_ended: true }, { is_ended: false }], |
| 48 | + }; |
| 49 | + mock_DBot_store.transactions = mockContractStore; |
| 50 | + const result = chartStore.is_contract_ended; |
| 51 | + |
| 52 | + expect(result).toBe(true); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should update symbol correctly', () => { |
| 56 | + const mockWorkspace = { |
| 57 | + getAllBlocks: jest.fn(() => [{ type: 'trade_definition_market', getFieldValue: jest.fn(() => 'EURUSD') }]), |
| 58 | + }; |
| 59 | + |
| 60 | + window.Blockly.derivWorkspace = mockWorkspace; |
| 61 | + chartStore.updateSymbol(); |
| 62 | + |
| 63 | + expect(chartStore.symbol).toEqual('EURUSD'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should update granularity correctly', () => { |
| 67 | + chartStore.updateGranularity(60); |
| 68 | + expect(chartStore.granularity).toEqual(60); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should update chart type correctly', () => { |
| 72 | + chartStore.updateChartType('candle'); |
| 73 | + expect(chartStore.chart_type).toEqual('candle'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should set chart status correctly', () => { |
| 77 | + chartStore.setChartStatus(true); |
| 78 | + expect(chartStore.is_chart_loading).toEqual(true); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should subscribe to ticks history', () => { |
| 82 | + const req = { subscribe: 1 }; |
| 83 | + const callback = jest.fn(); |
| 84 | + chartStore.wsSubscribe(req, callback); |
| 85 | + |
| 86 | + expect(mock_DBot_store.ws.subscribeTicksHistory).toHaveBeenCalledWith(req, callback); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should forget ticks history', () => { |
| 90 | + const subscribe_req = { subscribe: 1 }; |
| 91 | + const callback = jest.fn(); |
| 92 | + chartStore.wsSubscribe(subscribe_req, callback); |
| 93 | + const key = JSON.stringify(subscribe_req); |
| 94 | + |
| 95 | + expect(g_subscribers_map).toHaveProperty(key); |
| 96 | + chartStore.wsForget(subscribe_req); |
| 97 | + expect(g_subscribers_map).not.toHaveProperty(key); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should forget stream', () => { |
| 101 | + const stream_id = 'test_stream_id'; |
| 102 | + chartStore.wsForgetStream(stream_id); |
| 103 | + |
| 104 | + expect(mock_DBot_store.ws.forgetStream).toHaveBeenCalledWith(stream_id); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should send request and return server time', async () => { |
| 108 | + const req = { time: 1 }; |
| 109 | + const result = await chartStore.wsSendRequest(req); |
| 110 | + |
| 111 | + expect(result.msg_type).toEqual('time'); |
| 112 | + expect(result.time).toEqual('2024-03-11T10:56:02.239Z'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should send request and return active symbols', async () => { |
| 116 | + const req = { active_symbols: 1 }; |
| 117 | + await chartStore.wsSendRequest(req); |
| 118 | + |
| 119 | + expect(mock_DBot_store.ws.activeSymbols).toHaveBeenCalledWith(); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should send request using storage.send', async () => { |
| 123 | + const req = { storage: 'storage' }; |
| 124 | + await chartStore.wsSendRequest(req); |
| 125 | + |
| 126 | + expect(mock_DBot_store.ws.storage.send).toHaveBeenCalledWith(req); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should get markets order', () => { |
| 130 | + const active_symbols = [ |
| 131 | + { market: 'EURUSD', display_name: 'EUR/USD' }, |
| 132 | + { market: 'AUDUSD', display_name: 'AUD/USD' }, |
| 133 | + { market: 'CADUSD', display_name: 'CAD/USD' }, |
| 134 | + ]; |
| 135 | + const marketsOrder = chartStore.getMarketsOrder(active_symbols); |
| 136 | + |
| 137 | + expect(marketsOrder).toEqual(['AUDUSD', 'CADUSD', 'EURUSD']); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should get markets order with synthetic index', () => { |
| 141 | + const active_symbols = [{ market: 'synthetic_index', display_name: 'Synthetic Index' }]; |
| 142 | + const marketsOrder = chartStore.getMarketsOrder(active_symbols); |
| 143 | + |
| 144 | + expect(marketsOrder).toEqual(['synthetic_index']); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should update symbol and save to local storage', () => { |
| 148 | + const mockSymbol = 'USDJPY'; |
| 149 | + chartStore.onSymbolChange(mockSymbol); |
| 150 | + |
| 151 | + expect(chartStore.symbol).toEqual(mockSymbol); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should call restoreFromStorage ', () => { |
| 155 | + LocalStore.set('bot.chart_props', '{none}'); |
| 156 | + chartStore.restoreFromStorage(); |
| 157 | + }); |
| 158 | +}); |
0 commit comments