-
Notifications
You must be signed in to change notification settings - Fork 5.4k
refactor: extract useEnableMissingNetwork into a separate hook #38412
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
Merged
+128
−122
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { renderHookWithProvider } from '../../../../test/lib/render-helpers-navigate'; | ||
| import { createBridgeMockStore } from '../../../../test/data/bridge/mock-bridge-store'; | ||
| import * as ActionsModule from '../../../store/actions'; | ||
| import { useEnableMissingNetwork } from './useEnableMissingNetwork'; | ||
|
|
||
| describe('useEnableMissingNetwork', () => { | ||
| const arrange = () => { | ||
| const mockEnableAllPopularNetworks = jest.spyOn( | ||
| ActionsModule, | ||
| 'setEnabledAllPopularNetworks', | ||
| ); | ||
|
|
||
| return { | ||
| mockEnableAllPopularNetworks, | ||
| }; | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('enables popular network when not already enabled', () => { | ||
| const mocks = arrange(); | ||
| const hook = renderHookWithProvider( | ||
| () => useEnableMissingNetwork(), | ||
| createBridgeMockStore({ | ||
| metamaskStateOverrides: { | ||
| enabledNetworkMap: { | ||
| eip155: { | ||
| '0xe708': true, | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| // Act - enable 0x1 | ||
| hook.result.current('0x1'); | ||
|
|
||
| // Assert - Adds 0x1 to enabled networks | ||
| expect(mocks.mockEnableAllPopularNetworks).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('does not enable popular network if already enabled', () => { | ||
| const mocks = arrange(); | ||
| const hook = renderHookWithProvider( | ||
| () => useEnableMissingNetwork(), | ||
| createBridgeMockStore(), | ||
| ); | ||
|
|
||
| // Act - enable 0x1 (already enabled) | ||
| hook.result.current('0x1'); | ||
| expect(mocks.mockEnableAllPopularNetworks).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not enable non-popular network', () => { | ||
| const mocks = arrange(); | ||
| const hook = renderHookWithProvider( | ||
| () => useEnableMissingNetwork(), | ||
| createBridgeMockStore({ | ||
| metamaskStateOverrides: { | ||
| enabledNetworkMap: { '0x1': true }, | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| hook.result.current('0x1111'); // not popular network | ||
| expect(mocks.mockEnableAllPopularNetworks).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { useCallback } from 'react'; | ||
| import { | ||
| formatChainIdToCaip, | ||
| formatChainIdToHex, | ||
| isNonEvmChainId, | ||
| } from '@metamask/bridge-controller'; | ||
| import { type CaipChainId, type Hex, parseCaipChainId } from '@metamask/utils'; | ||
| import { useSelector, useDispatch } from 'react-redux'; | ||
| import { getEnabledNetworksByNamespace } from '../../../selectors'; | ||
| import { FEATURED_NETWORK_CHAIN_IDS } from '../../../../shared/constants/network'; | ||
| import { setEnabledAllPopularNetworks } from '../../../store/actions'; | ||
|
|
||
| /** | ||
| * Ensures that any missing network gets added to the NetworkEnabledMap (which handles network polling) | ||
| * | ||
| * @returns callback to enable a network config. | ||
| */ | ||
| export const useEnableMissingNetwork = () => { | ||
| const enabledNetworksByNamespace = useSelector(getEnabledNetworksByNamespace); | ||
| const dispatch = useDispatch(); | ||
|
|
||
| const enableMissingNetwork = useCallback( | ||
| (chainId: Hex | CaipChainId) => { | ||
| if (isNonEvmChainId(chainId)) { | ||
| return; | ||
| } | ||
|
|
||
| const enabledNetworkKeys = Object.keys(enabledNetworksByNamespace ?? {}); | ||
|
|
||
| const caipChainId = formatChainIdToCaip(chainId); | ||
| const { namespace } = parseCaipChainId(caipChainId); | ||
| const hexChainId = formatChainIdToHex(chainId); | ||
|
|
||
| if (namespace) { | ||
| const isPopularNetwork = | ||
| FEATURED_NETWORK_CHAIN_IDS.includes(hexChainId); | ||
|
|
||
| if (isPopularNetwork) { | ||
| const isNetworkEnabled = enabledNetworkKeys.includes(hexChainId); | ||
| if (!isNetworkEnabled) { | ||
| // Bridging between popular networks indicates we want the 'select all' enabled | ||
| // This way users can see their full bridging tx activity | ||
| dispatch(setEnabledAllPopularNetworks()); | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| [dispatch, enabledNetworksByNamespace], | ||
| ); | ||
|
|
||
| return enableMissingNetwork; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.