Skip to content

Commit

Permalink
chore: set src network on Bridge page load
Browse files Browse the repository at this point in the history
  • Loading branch information
micaelae committed Aug 22, 2024
1 parent 42aff78 commit 275a9d1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
1 change: 0 additions & 1 deletion ui/ducks/bridge/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export const setBridgeFeatureFlags = () => {
};

// User actions
// TODO call this in useBridging as well to preload src tokens
export const setFromChain = (network: ProviderConfig | RPCDefinition) => {
return async (dispatch: MetaMaskReduxDispatch) => {
const { chainId } = network;
Expand Down
22 changes: 15 additions & 7 deletions ui/hooks/bridge/useBridging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ jest.mock('react-router-dom', () => ({
}),
}));

const mockDispatch = jest.fn().mockReturnValue(() => jest.fn());
jest.mock('react-redux', () => ({
...jest.requireActual('react-redux'),
useDispatch: jest.fn().mockReturnValue(() => jest.fn()),
useDispatch: () => mockDispatch,
}));

const mockSetFromChain = jest.fn();
jest.mock('../../ducks/bridge/actions', () => ({
...jest.requireActual('../../ducks/bridge/actions'),
setFromChain: () => mockSetFromChain(),
}));

const MOCK_METAMETRICS_ID = '0xtestMetaMetricsId';
Expand Down Expand Up @@ -78,9 +85,8 @@ describe('useBridging', () => {
const { result } = renderUseBridging({
metamask: {
useExternalServices: true,
providerConfig: {
chainId: '0x1',
},
providerConfig: { chainId: '0x1', type: 'test-id' },
networkConfigurations: [{ chainId: '0x1', id: 'test-id' }],
metaMetricsId: MOCK_METAMETRICS_ID,
bridgeState: {
bridgeFeatureFlags: {
Expand All @@ -94,6 +100,8 @@ describe('useBridging', () => {
},
});

expect(mockDispatch.mock.calls).toHaveLength(1);

expect(nock(BRIDGE_API_BASE_URL).isDone()).toBe(true);
result.current.openBridgeExperience(location, token, urlSuffix);

Expand Down Expand Up @@ -149,9 +157,8 @@ describe('useBridging', () => {
const { result } = renderUseBridging({
metamask: {
useExternalServices: true,
providerConfig: {
chainId: '0x1',
},
providerConfig: { chainId: '0x1', type: 'test-id' },
networkConfigurations: [{ chainId: '0x1', id: 'test-id' }],
metaMetricsId: MOCK_METAMETRICS_ID,
bridgeState: {
bridgeFeatureFlags: {
Expand All @@ -167,6 +174,7 @@ describe('useBridging', () => {

result.current.openBridgeExperience(location, token, urlSuffix);

expect(mockDispatch.mock.calls).toHaveLength(3);
expect(mockHistoryPush.mock.calls).toHaveLength(1);
expect(mockHistoryPush).toHaveBeenCalledWith(expectedUrl);
expect(openTabSpy).not.toHaveBeenCalled();
Expand Down
23 changes: 16 additions & 7 deletions ui/hooks/bridge/useBridging.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useCallback, useContext, useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { setBridgeFeatureFlags } from '../../ducks/bridge/actions';
import {
getCurrentChainId,
setBridgeFeatureFlags,
setFromChain,
} from '../../ducks/bridge/actions';
import {
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta,build-flask)
getCurrentKeyring,
getDataCollectionForMarketing,
Expand Down Expand Up @@ -31,6 +33,7 @@ import { isHardwareKeyring } from '../../helpers/utils/hardware';
import { getPortfolioUrl } from '../../helpers/utils/portfolio';
import { setSwapsFromToken } from '../../ducks/swaps/swaps';
import { SwapsTokenObject } from '../../../shared/constants/swaps';
import { getProviderConfig } from '../../ducks/metamask/metamask';
///: END:ONLY_INCLUDE_IF

const useBridging = () => {
Expand All @@ -41,7 +44,7 @@ const useBridging = () => {
const metaMetricsId = useSelector(getMetaMetricsId);
const isMetaMetricsEnabled = useSelector(getParticipateInMetaMetrics);
const isMarketingEnabled = useSelector(getDataCollectionForMarketing);
const chainId = useSelector(getCurrentChainId);
const providerConfig = useSelector(getProviderConfig);
const keyring = useSelector(getCurrentKeyring);
const usingHardwareWallet = isHardwareKeyring(keyring.type);

Expand All @@ -52,13 +55,19 @@ const useBridging = () => {
dispatch(setBridgeFeatureFlags());
}, [dispatch, setBridgeFeatureFlags]);

useEffect(() => {
isBridgeChain &&
isBridgeSupported &&
dispatch(setFromChain(providerConfig));
}, []);

const openBridgeExperience = useCallback(
(
location: string,
token: SwapsTokenObject | SwapsEthToken,
portfolioUrlSuffix?: string,
) => {
if (!isBridgeChain) {
if (!isBridgeChain || !providerConfig) {
return;
}

Expand All @@ -70,7 +79,7 @@ const useBridging = () => {
token_symbol: token.symbol,
location,
text: 'Bridge',
chain_id: chainId,
chain_id: providerConfig.chainId,
},
});
dispatch(
Expand Down Expand Up @@ -105,7 +114,7 @@ const useBridging = () => {
location,
text: 'Bridge',
url: portfolioUrl,
chain_id: chainId,
chain_id: providerConfig.chainId,
token_symbol: token.symbol,
},
});
Expand All @@ -114,7 +123,6 @@ const useBridging = () => {
[
isBridgeSupported,
isBridgeChain,
chainId,
setSwapsFromToken,
dispatch,
usingHardwareWallet,
Expand All @@ -123,6 +131,7 @@ const useBridging = () => {
trackEvent,
isMetaMetricsEnabled,
isMarketingEnabled,
providerConfig,
],
);

Expand Down
2 changes: 2 additions & 0 deletions ui/pages/bridge/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ setBackgroundConnection({
getNetworkConfigurationByNetworkClientId: jest
.fn()
.mockResolvedValue({ chainId: '0x1' }),
setBridgeFeatureFlags: jest.fn(),
selectSrcNetwork: jest.fn(),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);

Expand Down
4 changes: 4 additions & 0 deletions ui/pages/bridge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ import {
BlockSize,
} from '../../helpers/constants/design-system';
import { getIsBridgeEnabled } from '../../selectors';
import useBridging from '../../hooks/bridge/useBridging';
import { PrepareBridgePage } from './prepare/prepare-bridge-page';

const CrossChainSwap = () => {
const t = useContext(I18nContext);

useBridging();

const history = useHistory();
const dispatch = useDispatch();

Expand Down

0 comments on commit 275a9d1

Please sign in to comment.