From 11c06a1349e2927d8c373190040bfd2ae4dc4634 Mon Sep 17 00:00:00 2001 From: Farhan Ahmad Nurzi <125247833+farhan-nurzi-deriv@users.noreply.github.com> Date: Thu, 21 Mar 2024 11:03:00 +0800 Subject: [PATCH] [DTRA] Farhan/WEBREL-56/tTest coverage: app.tsx + init-store.ts in Trader package (#14130) * chore: test cases for app.tsx and init-store * refactor: variable names * refactor: update mock store --- .../trader/src/App/__tests__/app.spec.tsx | 81 +++++++++++++++++++ .../src/App/__tests__/init-store.spec.ts | 67 +++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 packages/trader/src/App/__tests__/app.spec.tsx create mode 100644 packages/trader/src/App/__tests__/init-store.spec.ts diff --git a/packages/trader/src/App/__tests__/app.spec.tsx b/packages/trader/src/App/__tests__/app.spec.tsx new file mode 100644 index 000000000000..dbdedf494938 --- /dev/null +++ b/packages/trader/src/App/__tests__/app.spec.tsx @@ -0,0 +1,81 @@ +import React from 'react'; +import { render } from '@testing-library/react'; +import App from '../app'; +import { mockStore } from '@deriv/stores'; +import moment from 'moment'; + +const rootStore = mockStore({ + common: { + server_time: moment(new Date()).utc(), + }, + client: { + is_landing_company_loaded: true, + is_logged_in: false, + }, + modules: { + cashier: { + general_store: { + onMountCommon: jest.fn(), + setAccountSwitchListener: jest.fn(), + }, + }, + }, +}); + +const mockWs = { + activeSymbols: jest.fn(), + authorized: { + activeSymbols: jest.fn(), + subscribeProposalOpenContract: jest.fn(), + send: jest.fn(), + }, + buy: jest.fn(), + storage: { + contractsFor: jest.fn(), + send: jest.fn(), + }, + contractUpdate: jest.fn(), + contractUpdateHistory: jest.fn(), + subscribeTicksHistory: jest.fn(), + forgetStream: jest.fn(), + forget: jest.fn(), + forgetAll: jest.fn(), + send: jest.fn(), + subscribeProposal: jest.fn(), + subscribeTicks: jest.fn(), + time: jest.fn(), + tradingTimes: jest.fn(), + wait: jest.fn(), +}; + +jest.mock('App/Containers/Routes/routes', () => jest.fn(() =>
Router
)); +jest.mock('App/Containers/trade-footer-extensions', () => jest.fn(() =>
TradeFooterExtensions
)); + +describe('App', () => { + it('should render the app component', () => { + const { container } = render( + + ); + expect(container).toBeInTheDocument(); + }); + + it('should call setPromptHandler on unmount', () => { + const setPromptHandler = jest.fn(); + rootStore.ui.setPromptHandler = setPromptHandler; + const { unmount } = render( + + ); + unmount(); + expect(setPromptHandler).toHaveBeenCalledWith(false); + }); +}); diff --git a/packages/trader/src/App/__tests__/init-store.spec.ts b/packages/trader/src/App/__tests__/init-store.spec.ts new file mode 100644 index 000000000000..bc34ef701d0f --- /dev/null +++ b/packages/trader/src/App/__tests__/init-store.spec.ts @@ -0,0 +1,67 @@ +import { setWebsocket } from '@deriv/shared'; +import { mockStore } from '@deriv/stores'; +import initStore from 'App/init-store'; + +const mockWs = { + activeSymbols: jest.fn(), + authorized: { + activeSymbols: jest.fn(), + subscribeProposalOpenContract: jest.fn(), + send: jest.fn(), + }, + buy: jest.fn(), + storage: { + contractsFor: jest.fn(), + send: jest.fn(), + }, + contractUpdate: jest.fn(), + contractUpdateHistory: jest.fn(), + subscribeTicksHistory: jest.fn(), + forgetStream: jest.fn(), + forget: jest.fn(), + forgetAll: jest.fn(), + send: jest.fn(), + subscribeProposal: jest.fn(), + subscribeTicks: jest.fn(), + time: jest.fn(), + tradingTimes: jest.fn(), + wait: jest.fn(), +}; + +jest.mock('@deriv/shared', () => ({ + ...jest.requireActual('@deriv/shared'), + setWebsocket: jest.fn(), +})); + +describe('initStore', () => { + const rootStore = mockStore({}); + + it('should return the root store', () => { + const result = initStore(rootStore, mockWs); + expect(result).toBeDefined(); + expect(Object.keys(result)).toEqual([ + 'client', + 'common', + 'modules', + 'ui', + 'gtm', + 'notifications', + 'contract_replay', + 'contract_trade', + 'portfolio', + 'chart_barrier_store', + 'active_symbols', + ]); + }); + + it('should set the websocket', () => { + initStore(rootStore, mockWs); + expect(setWebsocket).toHaveBeenCalledWith(mockWs); + }); + + it('should return the same store if it already exists', () => { + const result1 = initStore(rootStore, mockWs); + const result2 = initStore(rootStore, mockWs); + expect(result1).toBe(result2); + }); +});