Skip to content

Commit

Permalink
[ DTRA] Maryia/DTRA-764/ fix: Console warning about invoking a transf…
Browse files Browse the repository at this point in the history
…ormer from outside a reactive context (#13090)

* fix: replace createTransformer outside a reactive context with computedFn

* test: portfolio-store getPositionById
  • Loading branch information
maryia-deriv authored Jan 29, 2024
1 parent 241411c commit c1dbce7
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 2 deletions.
99 changes: 99 additions & 0 deletions packages/core/src/Stores/__tests__/portfolio-store.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import PortfolioStore from '../portfolio-store.js';
import { configure } from 'mobx';

configure({ safeDescriptors: false });

let mockedPortfolioStore: PortfolioStore;

const symbol = '1HZ100V';
const contracts = [
{
app_id: 24075,
buy_price: 10,
contract_id: 229749680508,
contract_type: 'MULTUP',
currency: 'USD',
date_start: 1705570990,
expiry_time: 4859222399,
longcode:
"If you select 'Up', your total profit/loss will be the percentage increase in Volatility 100 (1s) Index, multiplied by 100, minus commissions.",
payout: 0,
purchase_time: 1705570990,
shortcode: 'MULTUP_1HZ100V_10.00_10_1705570990_4859222399_0_0.00',
symbol,
transaction_id: 458367398868,
},
{
app_id: 16929,
buy_price: 10,
contract_id: 230152813328,
contract_type: 'MULTDOWN',
currency: 'USD',
date_start: 1705921444,
expiry_time: 4859567999,
longcode:
"If you select 'Down', your total profit/loss will be the percentage decrease in AUD/JPY, multiplied by 300, minus commissions.",
payout: 0,
purchase_time: 1705921444,
shortcode: 'MULTDOWN_FRXAUDJPY_10.00_30_1705921444_4859567999_0_0.00',
symbol,
transaction_id: 459167693628,
},
];

beforeEach(() => {
mockedPortfolioStore = new PortfolioStore({
active_symbols: {
active_symbols: [
{
allow_forward_starting: 1,
display_name: 'Volatility 100 (1s) Index',
display_order: 3,
exchange_is_open: 1,
is_trading_suspended: 0,
market: 'synthetic_index',
market_display_name: 'Derived',
pip: 0.01,
subgroup: 'synthetics',
subgroup_display_name: 'Synthetics',
submarket: 'random_index',
submarket_display_name: 'Continuous Indices',
symbol,
symbol_type: 'stockindex',
},
],
},
});
mockedPortfolioStore.portfolioHandler({
echo_req: {
portfolio: 1,
req_id: 8,
},
msg_type: 'portfolio',
portfolio: {
contracts,
},
req_id: 8,
});
});

describe('PortfolioStore', () => {
it('getPositionById() should return a position by its id, or undefined when id is incorrect or not provided', () => {
expect(mockedPortfolioStore.getPositionById(230152813328)).toMatchObject({
contract_info: contracts[1],
contract_update: undefined,
details: contracts[1].longcode,
display_name: '',
id: contracts[1].contract_id,
indicative: 0,
is_unsupported: false,
payout: contracts[1].payout,
purchase: contracts[1].buy_price,
reference: contracts[1].transaction_id,
type: contracts[1].contract_type,
});
expect(mockedPortfolioStore.getPositionById('incorrect-id')).toEqual(undefined);
expect(mockedPortfolioStore.getPositionById(null)).toEqual(undefined);
expect(mockedPortfolioStore.getPositionById(undefined)).toEqual(undefined);
});
});
4 changes: 2 additions & 2 deletions packages/core/src/Stores/portfolio-store.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import throttle from 'lodash.throttle';
import { action, computed, observable, reaction, makeObservable, override } from 'mobx';
import { createTransformer } from 'mobx-utils';
import { computedFn } from 'mobx-utils';
import {
isAccumulatorContract,
isEmptyObject,
Expand Down Expand Up @@ -45,7 +45,7 @@ export default class PortfolioStore extends BaseStore {
main_barrier = null;
contract_type = '';

getPositionById = createTransformer(id => this.positions.find(position => +position.id === +id));
getPositionById = computedFn(id => this.positions.find(position => +position.id === +id));

responseQueue = [];

Expand Down

1 comment on commit c1dbce7

@vercel
Copy link

@vercel vercel bot commented on c1dbce7 Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

deriv-app – ./

deriv-app-git-master.binary.sx
deriv-app.binary.sx
binary.sx
deriv-app.vercel.app

Please sign in to comment.