Skip to content

Commit

Permalink
fix(trading): delete suite-analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive committed Mar 7, 2025
1 parent be67d22 commit 47d50cf
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 28 deletions.
1 change: 0 additions & 1 deletion suite-common/trading/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"@trezor/connect": "workspace:*",
"@trezor/env-utils": "workspace:*",
"@trezor/react-utils": "workspace:*",
"@trezor/suite-analytics": "workspace:*",
"@trezor/utils": "workspace:*",
"react": "18.2.0",
"uuid": "^11.0.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { BuyTradeResponse } from 'invity-api';

import { configureMockStore, extraDependenciesMock } from '@suite-common/test-utils';
import { Account } from '@suite-common/wallet-types';
import { analytics } from '@trezor/suite-analytics';

import { MIN_MAX_QUOTES_OK } from '../../../__fixtures__/buyUtils';
import { invityAPI } from '../../../invityAPI';
Expand Down Expand Up @@ -46,7 +45,7 @@ describe('confirmTradeThunk', () => {
});

const mockProcessResponseData = jest.fn();
const mockAnalyticsReport = jest.spyOn(analytics, 'report');
const mocktriggerAnalyticsTradeConfirmation = jest.fn();

const tradeForm = {
form: {
Expand All @@ -62,13 +61,13 @@ describe('confirmTradeThunk', () => {
return {
store,
mockProcessResponseData,
mockAnalyticsReport,
mocktriggerAnalyticsTradeConfirmation,
tradeForm,
};
};

it('should not trigger any action if selectedQuote is not set', async () => {
const { store, mockProcessResponseData, mockAnalyticsReport } = getMocks({
const { store, mockProcessResponseData, mocktriggerAnalyticsTradeConfirmation } = getMocks({
selectedQuote: undefined,
});

Expand All @@ -82,19 +81,21 @@ describe('confirmTradeThunk', () => {
descriptor: 'desc',
index: 1,
} as Account,
triggerAnalyticsTradeConfirmation: mocktriggerAnalyticsTradeConfirmation,
processResponseData: mockProcessResponseData,
}),
);

expect(store.getActions().length).toEqual(2);
expect(mockProcessResponseData).toHaveBeenCalledTimes(0);
expect(mockAnalyticsReport).toHaveBeenCalledTimes(0);
expect(mocktriggerAnalyticsTradeConfirmation).toHaveBeenCalledTimes(0);
expect(store.getState().wallet.trading.buy.isLoading).toBeFalsy();
});

describe('should show error toast', () => {
it('if there is no response', async () => {
const { store, mockProcessResponseData, mockAnalyticsReport } = getMocks();
const { store, mockProcessResponseData, mocktriggerAnalyticsTradeConfirmation } =
getMocks();

invityAPI.doBuyTrade = () => Promise.resolve(undefined as unknown as BuyTradeResponse);

Expand All @@ -108,6 +109,7 @@ describe('confirmTradeThunk', () => {
descriptor: 'desc',
index: 1,
} as Account,
triggerAnalyticsTradeConfirmation: mocktriggerAnalyticsTradeConfirmation,
processResponseData: mockProcessResponseData,
}),
);
Expand All @@ -116,15 +118,16 @@ describe('confirmTradeThunk', () => {
.getActions()
.find(action => action.type === '@common/in-app-notifications/addToast');

expect(mockAnalyticsReport).toHaveBeenCalledTimes(1);
expect(mocktriggerAnalyticsTradeConfirmation).toHaveBeenCalledTimes(1);
expect(toastAction?.payload.type).toEqual('error');
expect(toastAction?.payload.error).toEqual('No response from the server');
expect(mockProcessResponseData).toHaveBeenCalledTimes(0);
expect(store.getState().wallet.trading.buy.isLoading).toBeFalsy();
});

it('if there is no trade in response', async () => {
const { store, mockProcessResponseData, mockAnalyticsReport } = getMocks();
const { store, mockProcessResponseData, mocktriggerAnalyticsTradeConfirmation } =
getMocks();

invityAPI.doBuyTrade = () => Promise.resolve({} as BuyTradeResponse);

Expand All @@ -138,6 +141,7 @@ describe('confirmTradeThunk', () => {
descriptor: 'desc',
index: 1,
} as Account,
triggerAnalyticsTradeConfirmation: mocktriggerAnalyticsTradeConfirmation,
processResponseData: mockProcessResponseData,
}),
);
Expand All @@ -146,15 +150,16 @@ describe('confirmTradeThunk', () => {
.getActions()
.find(action => action.type === '@common/in-app-notifications/addToast');

expect(mockAnalyticsReport).toHaveBeenCalledTimes(1);
expect(mocktriggerAnalyticsTradeConfirmation).toHaveBeenCalledTimes(1);
expect(toastAction?.payload.type).toEqual('error');
expect(toastAction?.payload.error).toEqual('No response from the server');
expect(mockProcessResponseData).toHaveBeenCalledTimes(0);
expect(store.getState().wallet.trading.buy.isLoading).toBeFalsy();
});

it('if there is no response trade payment id', async () => {
const { store, mockProcessResponseData, mockAnalyticsReport } = getMocks();
const { store, mockProcessResponseData, mocktriggerAnalyticsTradeConfirmation } =
getMocks();

invityAPI.doBuyTrade = () =>
Promise.resolve({
Expand All @@ -174,6 +179,7 @@ describe('confirmTradeThunk', () => {
descriptor: 'desc',
index: 1,
} as Account,
triggerAnalyticsTradeConfirmation: mocktriggerAnalyticsTradeConfirmation,
processResponseData: mockProcessResponseData,
}),
);
Expand All @@ -182,15 +188,16 @@ describe('confirmTradeThunk', () => {
.getActions()
.find(action => action.type === '@common/in-app-notifications/addToast');

expect(mockAnalyticsReport).toHaveBeenCalledTimes(1);
expect(mocktriggerAnalyticsTradeConfirmation).toHaveBeenCalledTimes(1);
expect(toastAction?.payload.type).toEqual('error');
expect(toastAction?.payload.error).toEqual('No response from the server');
expect(mockProcessResponseData).toHaveBeenCalledTimes(0);
expect(store.getState().wallet.trading.buy.isLoading).toBeFalsy();
});

it('if there is trade error', async () => {
const { store, mockProcessResponseData, mockAnalyticsReport } = getMocks();
const { store, mockProcessResponseData, mocktriggerAnalyticsTradeConfirmation } =
getMocks();
const error = 'Error message from API';

invityAPI.doBuyTrade = () =>
Expand All @@ -211,14 +218,15 @@ describe('confirmTradeThunk', () => {
descriptor: 'desc',
index: 1,
} as Account,
triggerAnalyticsTradeConfirmation: mocktriggerAnalyticsTradeConfirmation,
processResponseData: mockProcessResponseData,
}),
);

const toastAction = store
.getActions()
.find(action => action.type === '@common/in-app-notifications/addToast');
expect(mockAnalyticsReport).toHaveBeenCalledTimes(1);
expect(mocktriggerAnalyticsTradeConfirmation).toHaveBeenCalledTimes(1);
expect(toastAction?.payload.type).toEqual('error');
expect(toastAction?.payload.error).toEqual(error);
expect(mockProcessResponseData).toHaveBeenCalledTimes(0);
Expand All @@ -227,7 +235,8 @@ describe('confirmTradeThunk', () => {
});

it('should call processResponseData with response and save trade', async () => {
const { store, mockProcessResponseData, mockAnalyticsReport } = getMocks();
const { store, mockProcessResponseData, mocktriggerAnalyticsTradeConfirmation } =
getMocks();

const dateString = new Date().toISOString();
jest.spyOn(Date.prototype, 'toISOString').mockImplementation(() => dateString);
Expand All @@ -249,13 +258,14 @@ describe('confirmTradeThunk', () => {
descriptor: 'desc',
index: 1,
} as Account,
triggerAnalyticsTradeConfirmation: mocktriggerAnalyticsTradeConfirmation,
processResponseData: mockProcessResponseData,
}),
);

const { trades } = store.getState().wallet.trading;

expect(mockAnalyticsReport).toHaveBeenCalledTimes(1);
expect(mocktriggerAnalyticsTradeConfirmation).toHaveBeenCalledTimes(1);
expect(mockProcessResponseData).toHaveBeenCalledTimes(1);
expect(trades.length).toEqual(1);
expect(trades[0]).toEqual({
Expand Down
17 changes: 9 additions & 8 deletions suite-common/trading/src/thunks/buy/confirmTradeThunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { BuyTradeResponse } from 'invity-api';
import { createThunk } from '@suite-common/redux-utils';
import { notificationsActions } from '@suite-common/toast-notifications';
import { Account } from '@suite-common/wallet-types';
import { EventType, analytics } from '@trezor/suite-analytics';

import { BUY_THUNK_COMMON_PREFIX } from './handleRequestThunk';
import { tradingActions } from '../../actions/tradingActions';
Expand All @@ -16,13 +15,20 @@ export type ConfirmTradeThunkProps = {
address: string;
account: Account;

triggerAnalyticsTradeConfirmation: () => void;
processResponseData: (response: BuyTradeResponse) => void;
};

export const confirmTradeThunk = createThunk(
`${BUY_THUNK_COMMON_PREFIX}/confirmTrade`,
async (
{ returnUrl, address, account, processResponseData }: ConfirmTradeThunkProps,
{
returnUrl,
address,
account,
triggerAnalyticsTradeConfirmation,
processResponseData,
}: ConfirmTradeThunkProps,
{ dispatch, getState },
) => {
const selectedQuote = selectTradingBuySelectedQuote(getState());
Expand All @@ -31,12 +37,7 @@ export const confirmTradeThunk = createThunk(

dispatch(tradingBuyActions.setIsLoading(true));

analytics.report({
type: EventType.TradingConfirmTrade,
payload: {
type: 'buy',
},
});
triggerAnalyticsTradeConfirmation();

const trade = {
...selectedQuote,
Expand Down
3 changes: 0 additions & 3 deletions suite-common/trading/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
{ "path": "../../packages/connect" },
{ "path": "../../packages/env-utils" },
{ "path": "../../packages/react-utils" },
{
"path": "../../packages/suite-analytics"
},
{ "path": "../../packages/utils" },
{ "path": "../test-utils" }
]
Expand Down
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9725,7 +9725,6 @@ __metadata:
"@trezor/connect": "workspace:*"
"@trezor/env-utils": "workspace:*"
"@trezor/react-utils": "workspace:*"
"@trezor/suite-analytics": "workspace:*"
"@trezor/utils": "workspace:*"
"@types/invity-api": "npm:^1.1.5"
react: "npm:18.2.0"
Expand Down

0 comments on commit 47d50cf

Please sign in to comment.