Skip to content

Commit

Permalink
fixup! fix(coinmarket): send and verify modals
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive committed Sep 10, 2024
1 parent b85b79b commit c9742bd
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { selectAccounts, selectDevice } from '@suite-common/wallet-core';
import { Account, SelectedAccountLoaded } from '@suite-common/wallet-types';
import { isTestnet } from '@suite-common/wallet-utils';
import { useEffect, useState } from 'react';
import { setCoinmarketExchangeAccount } from 'src/actions/wallet/coinmarketExchangeActions';
import { setCoinmarketSellAccount } from 'src/actions/wallet/coinmarketSellActions';
import { useDispatch, useSelector } from 'src/hooks/suite';
import { useState } from 'react';
import { useSelector } from 'src/hooks/suite';
import {
coinmarketGetSortedAccounts,
mapTestnetSymbol,
Expand All @@ -21,7 +19,6 @@ export const useCoinmarketAccount = ({
selectedAccount,
isNotFormPage,
}: CoinmarketUseAccountProps): [Account, (state: Account) => void] => {
const dispatch = useDispatch();
const accounts = useSelector(selectAccounts);
const device = useSelector(selectDevice);

Expand Down Expand Up @@ -51,15 +48,5 @@ export const useCoinmarketAccount = ({
return selectedAccount.account;
});

// clean coinmarketAccounts if is form page
useEffect(() => {
if (!isNotFormPage) {
dispatch(setCoinmarketSellAccount(undefined));
dispatch(setCoinmarketExchangeAccount(undefined));
}
// only on mount
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return [account, setAccount];
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import routerReducer, { RouterState } from 'src/reducers/suite/routerReducer';
import modalReducer, { State as ModalState } from 'src/reducers/suite/modalReducer';
import { MODAL, ROUTER } from 'src/actions/suite/constants';
import { UI } from '@trezor/connect';
import { waitFor } from '@testing-library/react';

jest.mock('src/services/suite/invityAPI');
invityAPI.setInvityServersEnvironment = () => {};
Expand All @@ -26,20 +27,20 @@ export const ACCOUNT = {
descriptor: 'btc-descriptor',
};

const COINMARKET_BUY_ROUTE = {
const COINMARKET_EXCHANGE_ROUTE = {
anchor: undefined,
app: 'wallet',
hash: '/btc/0/normal',
loaded: true,
params: { symbol: 'btc', accountIndex: 0, accountType: 'normal' },
pathname: '/accounts/coinmarket/buy',
pathname: '/accounts/coinmarket/exchange',
route: {
name: 'wallet-coinmarket-buy',
pattern: '/accounts/coinmarket/buy',
name: 'wallet-coinmarket-exchange',
pattern: '/accounts/coinmarket/exchange',
app: 'wallet',
},
settingsBackRoute: { name: 'wallet-index', params: undefined },
url: '/accounts/coinmarket/buy#/btc/0/normal',
url: '/accounts/coinmarket/exchange#/btc/0/normal',
};

type CoinmarketState = ReturnType<typeof coinmarketReducer>;
Expand Down Expand Up @@ -242,7 +243,7 @@ describe('coinmarketMiddleware', () => {
// go to coinmarket
store.dispatch({
type: ROUTER.LOCATION_CHANGE,
payload: COINMARKET_BUY_ROUTE,
payload: COINMARKET_EXCHANGE_ROUTE,
});

// open modal
Expand All @@ -261,4 +262,35 @@ describe('coinmarketMiddleware', () => {

expect(store.getState().wallet.coinmarket.modalAccount).toEqual(undefined);
});

it('Test of cleaning coinmarketAccount property', async () => {
const store = initStore(
getInitialState({
coinmarket: {
...initialState,
exchange: {
...initialState.exchange,
coinmarketAccount: accounts[0],
},
sell: {
...initialState.sell,
coinmarketAccount: accounts[0],
},
},
}),
);

// go to coinmarket
store.dispatch({
type: ROUTER.LOCATION_CHANGE,
payload: COINMARKET_EXCHANGE_ROUTE,
});

await waitFor(() => {
expect(store.getState().wallet.coinmarket.sell.coinmarketAccount).toBe(accounts[0]);
expect(store.getState().wallet.coinmarket.exchange.coinmarketAccount).toEqual(
undefined,
);
});
});
});
42 changes: 32 additions & 10 deletions packages/suite/src/middlewares/wallet/coinmarketMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ import * as coinmarketBuyActions from 'src/actions/wallet/coinmarketBuyActions';
import * as coinmarketExchangeActions from 'src/actions/wallet/coinmarketExchangeActions';
import * as coinmarketSellActions from 'src/actions/wallet/coinmarketSellActions';
import { UI } from '@trezor/connect';
import { CONTEXT_DEVICE } from 'src/actions/suite/constants/modalConstants';
import { ROUTER, MODAL } from 'src/actions/suite/constants';

const coinmarketMiddleware =
(api: MiddlewareAPI<Dispatch, AppState>) =>
(next: Dispatch) =>
(action: Action): Action => {
const state = api.getState();
const { isLoading, lastLoadedTimestamp } = state.wallet.coinmarket;
const { exchangeInfo } = state.wallet.coinmarket.exchange;
const { sellInfo } = state.wallet.coinmarket.sell;
const { router, modal } = state;

if (action.type === COINMARKET_COMMON.LOAD_DATA) {
const { isLoading, lastLoadedTimestamp } = api.getState().wallet.coinmarket;
const { account, status } = api.getState().wallet.selectedAccount;
const { platforms, coins } = api.getState().wallet.coinmarket.info;
const { buyInfo } = api.getState().wallet.coinmarket.buy;
const { exchangeInfo } = api.getState().wallet.coinmarket.exchange;
const { sellInfo } = api.getState().wallet.coinmarket.sell;
const { account, status } = state.wallet.selectedAccount;
const { platforms, coins } = state.wallet.coinmarket.info;
const { buyInfo } = state.wallet.coinmarket.buy;

const currentAccountDescriptor = invityAPI.getCurrentAccountDescriptor();
const isDifferentAccount = currentAccountDescriptor !== account?.descriptor;
Expand All @@ -35,7 +38,7 @@ const coinmarketMiddleware =
) {
api.dispatch(coinmarketCommonActions.setLoading(true));

const { invityServerEnvironment } = api.getState().suite.settings.debug;
const { invityServerEnvironment } = state.suite.settings.debug;
if (invityServerEnvironment) {
invityAPI.setInvityServersEnvironment(invityServerEnvironment);
}
Expand Down Expand Up @@ -82,10 +85,9 @@ const coinmarketMiddleware =
}
}

const { router, modal } = api.getState(); // previous state
const isCoinmarketRoute = !!router.route?.name.includes('wallet-coinmarket');
const isCloseEvent = action.type === UI.CLOSE_UI_WINDOW;
const isDeviceContext = modal.context === CONTEXT_DEVICE;
const isDeviceContext = modal.context === MODAL.CONTEXT_DEVICE;
const isReceiveModal = isDeviceContext && modal.windowType === 'ButtonRequest_Address';
const isSendModal = isDeviceContext && modal.windowType === 'ButtonRequest_SignTx';

Expand All @@ -97,6 +99,26 @@ const coinmarketMiddleware =

next(action);

// get the new state after the action has been processed
const newState = api.getState();
const sellCoinmarketAccount = newState.wallet.coinmarket.sell.coinmarketAccount;
const exchangeCoinmarketAccount = newState.wallet.coinmarket.exchange.coinmarketAccount;

if (action.type === ROUTER.LOCATION_CHANGE) {
const isSell = newState.router.route?.name === 'wallet-coinmarket-sell';
const isExchange = newState.router.route?.name === 'wallet-coinmarket-sell';

// clean coinmarketAccount in sell
if (isSell && sellCoinmarketAccount) {
api.dispatch(coinmarketSellActions.setCoinmarketSellAccount(undefined));
}

// clean coinmarketAccount in exchange
if (isExchange && exchangeCoinmarketAccount) {
api.dispatch(coinmarketExchangeActions.setCoinmarketExchangeAccount(undefined));
}
}

return action;
};

Expand Down

0 comments on commit c9742bd

Please sign in to comment.