-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat-3598-non-permitted-chain-flow-small-imp…
…rovements
- Loading branch information
Showing
25 changed files
with
365 additions
and
421 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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
27 changes: 27 additions & 0 deletions
27
app/components/hooks/AssetPolling/AssetPollingProvider.test.tsx
This file contains 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,27 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react-native'; | ||
|
||
import { AssetPollingProvider } from './AssetPollingProvider'; | ||
|
||
jest.mock('./useCurrencyRatePolling', () => jest.fn()); | ||
jest.mock('./useTokenRatesPolling', () => jest.fn()); | ||
jest.mock('./useTokenDetectionPolling', () => jest.fn()); | ||
jest.mock('./useTokenListPolling', () => jest.fn()); | ||
jest.mock('./useTokenBalancesPolling', () => jest.fn()); | ||
|
||
describe('AssetPollingProvider', () => { | ||
it('should call all polling hooks', () => { | ||
|
||
render( | ||
<AssetPollingProvider> | ||
<div></div> | ||
</AssetPollingProvider> | ||
); | ||
|
||
expect(jest.requireMock('./useCurrencyRatePolling')).toHaveBeenCalled(); | ||
expect(jest.requireMock('./useTokenRatesPolling')).toHaveBeenCalled(); | ||
expect(jest.requireMock('./useTokenDetectionPolling')).toHaveBeenCalled(); | ||
expect(jest.requireMock('./useTokenListPolling')).toHaveBeenCalled(); | ||
expect(jest.requireMock('./useTokenBalancesPolling')).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains 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
58 changes: 58 additions & 0 deletions
58
app/components/hooks/AssetPolling/useTokenBalancesPolling.test.ts
This file contains 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,58 @@ | ||
import { renderHookWithProvider } from '../../../util/test/renderWithProvider'; | ||
import Engine from '../../../core/Engine'; | ||
import useTokenBalancesPolling from './useTokenBalancesPolling'; | ||
|
||
jest.mock('../../../core/Engine', () => ({ | ||
context: { | ||
TokenBalancesController: { | ||
startPolling: jest.fn(), | ||
stopPollingByPollingToken: jest.fn(), | ||
}, | ||
}, | ||
})); | ||
|
||
describe('useTokenBalancesPolling', () => { | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
const selectedChainId = '0x1' as const; | ||
const state = { | ||
engine: { | ||
backgroundState: { | ||
TokenBalancesController: { | ||
tokenBalances: {}, | ||
}, | ||
NetworkController: { | ||
selectedNetworkClientId: 'selectedNetworkClientId', | ||
networkConfigurationsByChainId: { | ||
[selectedChainId]: { | ||
chainId: selectedChainId, | ||
rpcEndpoints: [{ | ||
networkClientId: 'selectedNetworkClientId', | ||
}] | ||
}, | ||
'0x89': {}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
it('Should poll by selected chain id, and stop polling on dismount', async () => { | ||
|
||
const { unmount } = renderHookWithProvider(() => useTokenBalancesPolling(), {state}); | ||
|
||
const mockedTokenBalancesController = jest.mocked(Engine.context.TokenBalancesController); | ||
|
||
expect(mockedTokenBalancesController.startPolling).toHaveBeenCalledTimes(1); | ||
expect( | ||
mockedTokenBalancesController.startPolling | ||
).toHaveBeenCalledWith({chainId: selectedChainId}); | ||
|
||
expect(mockedTokenBalancesController.stopPollingByPollingToken).toHaveBeenCalledTimes(0); | ||
unmount(); | ||
expect(mockedTokenBalancesController.stopPollingByPollingToken).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
app/components/hooks/AssetPolling/useTokenBalancesPolling.ts
This file contains 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,37 @@ | ||
import { useSelector } from 'react-redux'; | ||
import usePolling from '../usePolling'; | ||
import Engine from '../../../core/Engine'; | ||
import { selectChainId, selectNetworkConfigurations } from '../../../selectors/networkController'; | ||
import { Hex } from '@metamask/utils'; | ||
import { isPortfolioViewEnabled } from '../../../util/networks'; | ||
import { selectAllTokenBalances } from '../../../selectors/tokenBalancesController'; | ||
|
||
const useTokenBalancesPolling = ({ chainIds }: { chainIds?: Hex[] } = {}) => { | ||
|
||
// Selectors to determine polling input | ||
const networkConfigurations = useSelector(selectNetworkConfigurations); | ||
const currentChainId = useSelector(selectChainId); | ||
|
||
// Selectors returning state updated by the polling | ||
const tokenBalances = useSelector(selectAllTokenBalances); | ||
|
||
const chainIdsToPoll = isPortfolioViewEnabled | ||
? (chainIds ?? Object.keys(networkConfigurations)) | ||
: [currentChainId]; | ||
|
||
const { TokenBalancesController } = Engine.context; | ||
|
||
usePolling({ | ||
startPolling: | ||
TokenBalancesController.startPolling.bind(TokenBalancesController), | ||
stopPollingByPollingToken: | ||
TokenBalancesController.stopPollingByPollingToken.bind(TokenBalancesController), | ||
input: chainIdsToPoll.map((chainId) => ({chainId: chainId as Hex})), | ||
}); | ||
|
||
return { | ||
tokenBalances | ||
}; | ||
}; | ||
|
||
export default useTokenBalancesPolling; |
This file contains 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 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.