Skip to content

Commit ba511df

Browse files
authored
test: mock token hooks in bridge unit tests (#40686)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** Changes - mock debouncing and token hooks in bridge-input-group test to remove flakiness - wait for JWT token to be available before fetching token lists to remove fetch flakiness <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/40686?quickstart=1) ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: test: fix flaky bridge input tests ## **Related issues** Fixes: flaky unit tests ## **Manual testing steps** 1. Go to this page... 2. 3. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
1 parent bbc277f commit ba511df

File tree

5 files changed

+299
-99
lines changed

5 files changed

+299
-99
lines changed

ui/hooks/bridge/usePopularTokens.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ export const usePopularTokens = ({
4848

4949
const { value: tokenList, pending: isTokenListLoading } =
5050
useAsyncResult(async () => {
51+
if (!jwt) {
52+
return assetsToInclude;
53+
}
5154
abortControllerRef.current?.abort('Asset balances changed');
5255
abortControllerRef.current = new AbortController();
5356
const response = await fetchPopularTokens({

ui/hooks/bridge/useTokenSearchResults.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ export const useTokenSearchResults = ({
128128
}, [searchQuery, assetsToInclude]);
129129

130130
useEffect(() => {
131+
if (!jwt) {
132+
return;
133+
}
131134
// Reset state on search query change
132135
abortControllerRef.current.abort('Search query changed');
133136
setSearchResultsWithBalance([]);

ui/pages/bridge/index.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ jest.mock('../../contexts/hardware-wallets', () => ({
2626
useHardwareWalletState: () => mockUseHardwareWalletState(),
2727
}));
2828

29+
jest.mock('../../hooks/bridge/usePopularTokens', () => ({
30+
usePopularTokens: jest.fn().mockReturnValue({
31+
popularTokensList: [],
32+
isLoading: false,
33+
}),
34+
}));
35+
36+
jest.mock('../../hooks/bridge/useTokenSearchResults', () => ({
37+
useTokenSearchResults: jest.fn().mockReturnValue({
38+
searchResults: [],
39+
isSearchResultsLoading: false,
40+
}),
41+
}));
42+
2943
setBackgroundConnection({
3044
resetPostFetchState: jest.fn(),
3145
getStatePatches: jest.fn(),

ui/pages/bridge/prepare/__snapshots__/bridge-input-group.test.tsx.snap

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,55 @@ exports[`BridgeInputGroup should render destination networks 1`] = `
356356
</div>
357357
`;
358358

359+
exports[`BridgeInputGroup should render popular tokens 2`] = `
360+
[
361+
{
362+
"accountAddress": "0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc",
363+
"assetsToInclude": [
364+
{
365+
"accountType": undefined,
366+
"assetId": "eip155:1/slip44:60",
367+
"balance": "0.01",
368+
"chainId": "eip155:1",
369+
"decimals": 18,
370+
"iconUrl": "https://static.cx.metamask.io/api/v2/tokenIcons/assets/eip155/1/slip44/60.png",
371+
"name": "Ether",
372+
"rwaData": undefined,
373+
"symbol": "ETH",
374+
"tokenFiatAmount": 25.242128065034784,
375+
},
376+
{
377+
"accountType": undefined,
378+
"assetId": "eip155:1/erc20:0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984",
379+
"balance": "0.0000001848",
380+
"chainId": "eip155:1",
381+
"decimals": 10,
382+
"iconUrl": "https://static.cx.metamask.io/api/v2/tokenIcons/assets/eip155/1/erc20/0x1f9840a85d5af5bf1d1762f925bdaddc4201f984.png",
383+
"name": "Uniswap",
384+
"rwaData": undefined,
385+
"symbol": "UNI",
386+
"tokenFiatAmount": 0.0010728914112762384,
387+
},
388+
{
389+
"accountType": undefined,
390+
"assetId": "eip155:1/erc20:0x514910771AF9Ca656af840dff83E8264EcF986CA",
391+
"balance": "0.000000001",
392+
"chainId": "eip155:1",
393+
"decimals": 9,
394+
"iconUrl": "https://static.cx.metamask.io/api/v2/tokenIcons/assets/eip155/1/erc20/0x514910771af9ca656af840dff83e8264ecf986ca.png",
395+
"name": "Link",
396+
"rwaData": undefined,
397+
"symbol": "LINK",
398+
"tokenFiatAmount": 0.0000030290553678041743,
399+
},
400+
],
401+
"chainIds": Set {
402+
"eip155:1",
403+
},
404+
},
405+
]
406+
`;
407+
359408
exports[`BridgeInputGroup should render source networks 1`] = `
360409
<div
361410
class="mm-box mm-popover mm-popover--open mm-popover--reference-hidden bridge-network-list-popover mm-box--margin-inline-3 mm-box--padding-4 mm-box--background-color-background-subsection mm-box--rounded-xl mm-box--border-color-border-muted box--border-style-solid box--border-width-1"
@@ -884,7 +933,57 @@ exports[`BridgeInputGroup should search for tokens 1`] = `
884933
</div>
885934
`;
886935

887-
exports[`BridgeInputGroup should search for tokens 6`] = `
936+
exports[`BridgeInputGroup should search for tokens 4`] = `
937+
[
938+
{
939+
"accountAddress": "0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc",
940+
"assetsToInclude": [
941+
{
942+
"accountType": undefined,
943+
"assetId": "eip155:1/slip44:60",
944+
"balance": "0.01",
945+
"chainId": "eip155:1",
946+
"decimals": 18,
947+
"iconUrl": "https://static.cx.metamask.io/api/v2/tokenIcons/assets/eip155/1/slip44/60.png",
948+
"name": "Ether",
949+
"rwaData": undefined,
950+
"symbol": "ETH",
951+
"tokenFiatAmount": 25.242128065034784,
952+
},
953+
{
954+
"accountType": undefined,
955+
"assetId": "eip155:1/erc20:0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984",
956+
"balance": "0.0000001848",
957+
"chainId": "eip155:1",
958+
"decimals": 10,
959+
"iconUrl": "https://static.cx.metamask.io/api/v2/tokenIcons/assets/eip155/1/erc20/0x1f9840a85d5af5bf1d1762f925bdaddc4201f984.png",
960+
"name": "Uniswap",
961+
"rwaData": undefined,
962+
"symbol": "UNI",
963+
"tokenFiatAmount": 0.0010728914112762384,
964+
},
965+
{
966+
"accountType": undefined,
967+
"assetId": "eip155:1/erc20:0x514910771AF9Ca656af840dff83E8264EcF986CA",
968+
"balance": "0.000000001",
969+
"chainId": "eip155:1",
970+
"decimals": 9,
971+
"iconUrl": "https://static.cx.metamask.io/api/v2/tokenIcons/assets/eip155/1/erc20/0x514910771af9ca656af840dff83e8264ecf986ca.png",
972+
"name": "Link",
973+
"rwaData": undefined,
974+
"symbol": "LINK",
975+
"tokenFiatAmount": 0.0000030290553678041743,
976+
},
977+
],
978+
"chainIds": Set {
979+
"eip155:1",
980+
},
981+
"searchQuery": "USD",
982+
},
983+
]
984+
`;
985+
986+
exports[`BridgeInputGroup should search for tokens 5`] = `
888987
<div
889988
class="mm-modal"
890989
data-testid="bridge-asset-picker-modal"
@@ -1232,3 +1331,53 @@ exports[`BridgeInputGroup should search for tokens 6`] = `
12321331
/>
12331332
</div>
12341333
`;
1334+
1335+
exports[`BridgeInputGroup should search for tokens with hasNextPage 3`] = `
1336+
[
1337+
{
1338+
"accountAddress": "0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc",
1339+
"assetsToInclude": [
1340+
{
1341+
"accountType": undefined,
1342+
"assetId": "eip155:1/slip44:60",
1343+
"balance": "0.01",
1344+
"chainId": "eip155:1",
1345+
"decimals": 18,
1346+
"iconUrl": "https://static.cx.metamask.io/api/v2/tokenIcons/assets/eip155/1/slip44/60.png",
1347+
"name": "Ether",
1348+
"rwaData": undefined,
1349+
"symbol": "ETH",
1350+
"tokenFiatAmount": 25.242128065034784,
1351+
},
1352+
{
1353+
"accountType": undefined,
1354+
"assetId": "eip155:1/erc20:0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984",
1355+
"balance": "0.0000001848",
1356+
"chainId": "eip155:1",
1357+
"decimals": 10,
1358+
"iconUrl": "https://static.cx.metamask.io/api/v2/tokenIcons/assets/eip155/1/erc20/0x1f9840a85d5af5bf1d1762f925bdaddc4201f984.png",
1359+
"name": "Uniswap",
1360+
"rwaData": undefined,
1361+
"symbol": "UNI",
1362+
"tokenFiatAmount": 0.0010728914112762384,
1363+
},
1364+
{
1365+
"accountType": undefined,
1366+
"assetId": "eip155:1/erc20:0x514910771AF9Ca656af840dff83E8264EcF986CA",
1367+
"balance": "0.000000001",
1368+
"chainId": "eip155:1",
1369+
"decimals": 9,
1370+
"iconUrl": "https://static.cx.metamask.io/api/v2/tokenIcons/assets/eip155/1/erc20/0x514910771af9ca656af840dff83e8264ecf986ca.png",
1371+
"name": "Link",
1372+
"rwaData": undefined,
1373+
"symbol": "LINK",
1374+
"tokenFiatAmount": 0.0000030290553678041743,
1375+
},
1376+
],
1377+
"chainIds": Set {
1378+
"eip155:1",
1379+
},
1380+
"searchQuery": "USD",
1381+
},
1382+
]
1383+
`;

0 commit comments

Comments
 (0)