Skip to content

Commit

Permalink
[DTRA] Farhan/WEBREL-56/tTest coverage: app.tsx + init-store.ts in Tr…
Browse files Browse the repository at this point in the history
…ader package (deriv-com#14130)

* chore: test cases for app.tsx and init-store

* refactor: variable names

* refactor: update mock store
  • Loading branch information
farhan-nurzi-deriv authored Mar 21, 2024
1 parent 419d100 commit 11c06a1
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
81 changes: 81 additions & 0 deletions packages/trader/src/App/__tests__/app.spec.tsx
Original file line number Diff line number Diff line change
@@ -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(() => <div>Router</div>));
jest.mock('App/Containers/trade-footer-extensions', () => jest.fn(() => <div>TradeFooterExtensions</div>));

describe('App', () => {
it('should render the app component', () => {
const { container } = render(
<App
passthrough={{
root_store: rootStore,
WS: mockWs,
}}
/>
);
expect(container).toBeInTheDocument();
});

it('should call setPromptHandler on unmount', () => {
const setPromptHandler = jest.fn();
rootStore.ui.setPromptHandler = setPromptHandler;
const { unmount } = render(
<App
passthrough={{
root_store: rootStore,
WS: mockWs,
}}
/>
);
unmount();
expect(setPromptHandler).toHaveBeenCalledWith(false);
});
});
67 changes: 67 additions & 0 deletions packages/trader/src/App/__tests__/init-store.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 11c06a1

Please sign in to comment.