Skip to content

Commit 5bb75ab

Browse files
authored
feat: Implement balance for NFTs in BIP-44 send (MetaMask#18711)
<!-- 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** <!-- 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? --> This PR aims to implement nft balances in send flow. ## **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: ## **Related issues** Fixes: MetaMask#18710 ## **Manual testing steps** ```gherkin Feature: my feature name Scenario: user [verb for user action] Given [describe expected initial app state] When user [verb for user action] Then [describe expected outcome] ``` ## **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** - [X] I’ve followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [X] I've completed the PR template to the best of my ability - [X] I’ve included tests if applicable - [X] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [X] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/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 b450a0b commit 5bb75ab

6 files changed

Lines changed: 1178 additions & 11 deletions

File tree

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import React from 'react';
2+
import { fireEvent } from '@testing-library/react-native';
3+
4+
import renderWithProvider from '../../../../../../util/test/renderWithProvider';
5+
import { Nft as NftType } from '../../../types/token';
6+
import { Nft } from './nft';
7+
8+
describe('Nft', () => {
9+
const createMockNft = (overrides: Partial<NftType> = {}): NftType => ({
10+
address: '0x1234567890123456789012345678901234567890',
11+
standard: 'ERC721',
12+
name: 'Cool NFT',
13+
collectionName: 'Awesome Collection',
14+
image: 'https://example.com/nft.png',
15+
chainId: '0x1',
16+
tokenId: '123',
17+
accountId: 'account1',
18+
networkBadgeSource: { uri: 'https://example.com/badge.png' },
19+
balance: '1',
20+
...overrides,
21+
});
22+
const mockOnPress = jest.fn();
23+
24+
beforeEach(() => {
25+
jest.clearAllMocks();
26+
});
27+
28+
it('displays NFT with collection name and name', () => {
29+
const mockNft = createMockNft({
30+
collectionName: 'Bored Apes',
31+
name: 'Ape #456',
32+
tokenId: '456',
33+
balance: '0',
34+
});
35+
36+
const { getByText } = renderWithProvider(
37+
<Nft asset={mockNft} onPress={mockOnPress} />,
38+
);
39+
40+
expect(getByText('Bored Apes')).toBeOnTheScreen();
41+
expect(getByText('Ape #456')).toBeOnTheScreen();
42+
});
43+
44+
it('displays tokenId when name is missing', () => {
45+
const mockNft = createMockNft({
46+
collectionName: 'CryptoPunks',
47+
name: undefined,
48+
tokenId: '789',
49+
balance: '0',
50+
});
51+
52+
const { getByText } = renderWithProvider(
53+
<Nft asset={mockNft} onPress={mockOnPress} />,
54+
);
55+
56+
expect(getByText('CryptoPunks')).toBeOnTheScreen();
57+
expect(getByText('#789')).toBeOnTheScreen();
58+
});
59+
60+
it('displays tokenId when collectionName is missing', () => {
61+
const mockNft = createMockNft({
62+
collectionName: undefined,
63+
name: 'Unique Art',
64+
tokenId: '101',
65+
balance: '0',
66+
});
67+
68+
const { getByText } = renderWithProvider(
69+
<Nft asset={mockNft} onPress={mockOnPress} />,
70+
);
71+
72+
expect(getByText('#101')).toBeOnTheScreen();
73+
expect(getByText('Unique Art')).toBeOnTheScreen();
74+
});
75+
76+
it('displays balance when provided and not zero', () => {
77+
const mockNft = createMockNft({
78+
collectionName: 'Multi Token',
79+
name: 'Token Item',
80+
balance: '5',
81+
tokenId: '202',
82+
});
83+
84+
const { getByText } = renderWithProvider(
85+
<Nft asset={mockNft} onPress={mockOnPress} />,
86+
);
87+
88+
expect(getByText('Multi Token')).toBeOnTheScreen();
89+
expect(getByText('(5) Token Item')).toBeOnTheScreen();
90+
});
91+
92+
it('does not display balance when zero', () => {
93+
const mockNft = createMockNft({
94+
collectionName: 'Zero Balance',
95+
name: 'Empty Item',
96+
balance: '0',
97+
tokenId: '303',
98+
});
99+
100+
const { getByText, queryByText } = renderWithProvider(
101+
<Nft asset={mockNft} onPress={mockOnPress} />,
102+
);
103+
104+
expect(getByText('Zero Balance')).toBeOnTheScreen();
105+
expect(getByText('Empty Item')).toBeOnTheScreen();
106+
expect(queryByText('(0)')).not.toBeOnTheScreen();
107+
});
108+
109+
it('does not display balance when undefined', () => {
110+
const mockNft = createMockNft({
111+
collectionName: 'No Balance',
112+
name: 'Simple Item',
113+
balance: undefined,
114+
tokenId: '404',
115+
});
116+
117+
const { getByText, queryByText } = renderWithProvider(
118+
<Nft asset={mockNft} onPress={mockOnPress} />,
119+
);
120+
121+
expect(getByText('No Balance')).toBeOnTheScreen();
122+
expect(getByText('Simple Item')).toBeOnTheScreen();
123+
expect(queryByText(/^\(/)).not.toBeOnTheScreen();
124+
});
125+
126+
it('calls onPress when pressed', () => {
127+
const mockNft = createMockNft();
128+
129+
const { getByText } = renderWithProvider(
130+
<Nft asset={mockNft} onPress={mockOnPress} />,
131+
);
132+
133+
fireEvent.press(getByText('Awesome Collection'));
134+
135+
expect(mockOnPress).toHaveBeenCalledWith(mockNft);
136+
});
137+
138+
it('renders without network badge when networkBadgeSource is undefined', () => {
139+
const mockNft = createMockNft({
140+
networkBadgeSource: undefined,
141+
collectionName: 'Simple Collection',
142+
name: 'Simple NFT',
143+
balance: '0',
144+
});
145+
146+
const { getByText } = renderWithProvider(
147+
<Nft asset={mockNft} onPress={mockOnPress} />,
148+
);
149+
150+
expect(getByText('Simple Collection')).toBeOnTheScreen();
151+
expect(getByText('Simple NFT')).toBeOnTheScreen();
152+
});
153+
});

app/components/Views/confirmations/components/UI/nft/nft.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ export function Nft({ asset, onPress }: NftProps) {
7676
color={TextColor.TextAlternative}
7777
numberOfLines={1}
7878
>
79+
{asset.balance !== '0' &&
80+
asset.balance !== undefined &&
81+
`(${asset.balance}) `}
7982
{asset.name ? asset.name : `#${asset.tokenId}`}
8083
</Text>
8184
)}

app/components/Views/confirmations/components/send/asset/asset.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export const Asset = () => {
135135
<>
136136
{filteredTokens.length > 0 && (
137137
<Text
138-
twClassName="m-4 mt-2"
138+
twClassName="m-4 mt-4"
139139
variant={TextVariant.BodyMd}
140140
color={TextColor.TextAlternative}
141141
fontWeight={FontWeight.Medium}

0 commit comments

Comments
 (0)