Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DTRA] / Kate / WEBREL-43 / Test coverage: positions-drawer.tsx in Trader package #11353

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { mockStore, useStore } from '@deriv/stores';
import { TCoreStores } from '@deriv/stores/types';
import { VANILLALONG, TURBOS } from '@deriv/shared';
balakrishna-deriv marked this conversation as resolved.
Show resolved Hide resolved
import PositionsDrawer from '../positions-drawer';
import TraderProviders from '../../../../../trader-providers';

type TAllPositions = ReturnType<typeof useStore>['portfolio']['all_positions'];
const mocked_store = {
modules: {
trade: {
symbol: '1HZ100V',
contract_type: 'rise_fall' as string,
},
},
portfolio: { all_positions: [] as TAllPositions, error: '', onHoverPosition: jest.fn() },
};
const empty_portfolio_message = 'EmptyPortfolioMessage';
const position_drawer_card = 'PositionsDrawerCard';

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
NavLink: jest.fn(({ children }) => <div>{children}</div>),
}));
jest.mock('react-transition-group', () => ({
...jest.requireActual('react-transition-group'),
CSSTransition: jest.fn(({ children }) => <div>{children}</div>),
}));
jest.mock('@deriv/components', () => ({
...jest.requireActual('@deriv/components'),
DataList: jest.fn(props => <div>{props.data_source.map(() => props.rowRenderer())}</div>),
PositionsDrawerCard: jest.fn(props => (
<div>
<button onMouseEnter={props.onMouseEnter} onMouseLeave={props.onMouseLeave}>
{position_drawer_card}
</button>
</div>
)),
}));
jest.mock('../../EmptyPortfolioMessage', () => jest.fn(() => <div>{empty_portfolio_message}</div>));

describe('<PositionsDrawer />', () => {
const mockPositionsDrawer = (mocked_store: TCoreStores) => {
return (
<TraderProviders store={mocked_store}>
<PositionsDrawer />
</TraderProviders>
);
};

it('should render Recent positions with empty portfolio message if there is no open positions', () => {
render(mockPositionsDrawer(mockStore(mocked_store)));

expect(screen.getByText('Recent positions')).toBeInTheDocument();
expect(screen.getByText(empty_portfolio_message)).toBeInTheDocument();
expect(screen.getByText('Go to Reports')).toBeInTheDocument();
});
it('should render Recent positions with empty portfolio message if there is an error in portfolio even though there is match in open position', () => {
mocked_store.portfolio.error = 'Some error';
mocked_store.portfolio.all_positions = [
{
contract_info: {
underlying: '1HZ100V',
contract_type: 'CALL',
shortcode: 'CALL_1HZ100V_10.00_1699697112_1699697772_S0P_2.33136_1699697111',
},
},
] as TAllPositions;
render(mockPositionsDrawer(mockStore(mocked_store)));

expect(screen.getByText('Recent positions')).toBeInTheDocument();
expect(screen.getByText(empty_portfolio_message)).toBeInTheDocument();
expect(screen.getByText('Go to Reports')).toBeInTheDocument();
});
it('should render PositionsDrawerCard if portfolio is not empty and there is no error', () => {
mocked_store.portfolio.error = '';
render(mockPositionsDrawer(mockStore(mocked_store)));

expect(screen.queryByText(empty_portfolio_message)).not.toBeInTheDocument();
expect(screen.getByText(position_drawer_card)).toBeInTheDocument();
});
it('should render both PositionsDrawerCard for Turbos Long and Short', () => {
mocked_store.modules.trade.contract_type = TURBOS.LONG;
mocked_store.portfolio.all_positions = [
{
contract_info: {
underlying: '1HZ100V',
contract_type: TURBOS.LONG.toUpperCase(),
shortcode: 'TURBOSLONG_1HZ100V_10.00_1699697112_1699697772_S0P_2.33136_1699697111',
},
},
{
contract_info: {
underlying: '1HZ100V',
contract_type: TURBOS.SHORT.toUpperCase(),
shortcode: 'TURBOSSHORT_1HZ100V_10.00_1699697112_1699697772_S0P_2.33136_1699697111',
},
},
] as TAllPositions;
render(mockPositionsDrawer(mockStore(mocked_store)));

expect(screen.getAllByText(position_drawer_card)).toHaveLength(2);
});
it('should render both PositionsDrawerCard for Vanilla Call and Put', () => {
mocked_store.modules.trade.contract_type = VANILLALONG.CALL;
mocked_store.portfolio.all_positions = [
{
contract_info: {
underlying: '1HZ100V',
contract_type: VANILLALONG.CALL.toUpperCase(),
shortcode: 'VANILLALONGCALL_1HZ100V_10.00_1699697112_1699697772_S0P_2.33136_1699697111',
},
},
{
contract_info: {
underlying: '1HZ100V',
contract_type: VANILLALONG.PUT.toUpperCase(),
shortcode: 'VANILLALONGPUT_1HZ100V_10.00_1699697112_1699697772_S0P_2.33136_1699697111',
},
},
] as TAllPositions;
render(mockPositionsDrawer(mockStore(mocked_store)));

expect(screen.getAllByText(position_drawer_card)).toHaveLength(2);
});
it('should call onHoverPosition if user hover on position drawer card and should call it twice if he unhover it', () => {
render(mockPositionsDrawer(mockStore(mocked_store)));

userEvent.hover(screen.getAllByText(position_drawer_card)[0]);
userEvent.unhover(screen.getAllByText(position_drawer_card)[0]);

expect(mocked_store.portfolio.onHoverPosition).toBeCalledTimes(2);
});
});