|
| 1 | +import React from 'react'; |
| 2 | +import { render, fireEvent } from '@testing-library/react-native'; |
| 3 | +import { Alert } from 'react-native'; |
| 4 | +import NftGridItemBottomSheet from './NftGridItemBottomSheet'; |
| 5 | +import { Nft } from '@metamask/assets-controllers'; |
| 6 | +import Engine from '../../../core/Engine'; |
| 7 | + |
| 8 | +jest.mock('react-native', () => ({ |
| 9 | + ...jest.requireActual('react-native'), |
| 10 | + Modal: ({ |
| 11 | + children, |
| 12 | + visible, |
| 13 | + }: { |
| 14 | + children: React.ReactNode; |
| 15 | + visible: boolean; |
| 16 | + }) => { |
| 17 | + const { View } = jest.requireActual('react-native'); |
| 18 | + return visible ? <View>{children}</View> : null; |
| 19 | + }, |
| 20 | +})); |
| 21 | + |
| 22 | +jest.mock('../../../core/Engine', () => ({ |
| 23 | + context: { |
| 24 | + NftController: { |
| 25 | + removeAndIgnoreNft: jest.fn(), |
| 26 | + addNft: jest.fn(), |
| 27 | + }, |
| 28 | + NetworkController: { |
| 29 | + findNetworkClientIdByChainId: jest.fn().mockReturnValue('mainnet'), |
| 30 | + }, |
| 31 | + }, |
| 32 | +})); |
| 33 | + |
| 34 | +jest.mock('../../../../locales/i18n', () => ({ |
| 35 | + strings: (key: string) => key, |
| 36 | +})); |
| 37 | + |
| 38 | +jest.mock( |
| 39 | + '../../../component-library/components/BottomSheets/BottomSheet', |
| 40 | + () => { |
| 41 | + const { forwardRef } = jest.requireActual('react'); |
| 42 | + return { |
| 43 | + __esModule: true, |
| 44 | + default: forwardRef( |
| 45 | + ( |
| 46 | + { |
| 47 | + children, |
| 48 | + onClose, |
| 49 | + }: { children: React.ReactNode; onClose?: () => void }, |
| 50 | + ref: React.Ref<{ onCloseBottomSheet: () => void }>, |
| 51 | + ) => { |
| 52 | + const { View } = jest.requireActual('react-native'); |
| 53 | + const React = jest.requireActual('react'); |
| 54 | + React.useImperativeHandle(ref, () => ({ |
| 55 | + onCloseBottomSheet: () => onClose?.(), |
| 56 | + })); |
| 57 | + return <View>{children}</View>; |
| 58 | + }, |
| 59 | + ), |
| 60 | + }; |
| 61 | + }, |
| 62 | +); |
| 63 | + |
| 64 | +jest.mock('@metamask/design-system-react-native', () => { |
| 65 | + const { View, Text, TouchableOpacity } = jest.requireActual('react-native'); |
| 66 | + return { |
| 67 | + Box: ({ children }: { children: React.ReactNode }) => ( |
| 68 | + <View>{children}</View> |
| 69 | + ), |
| 70 | + BottomSheetHeader: ({ |
| 71 | + children, |
| 72 | + onClose, |
| 73 | + }: { |
| 74 | + children: React.ReactNode; |
| 75 | + onClose?: () => void; |
| 76 | + }) => ( |
| 77 | + <View> |
| 78 | + {children} |
| 79 | + <TouchableOpacity |
| 80 | + testID="bottom-sheet-header-close" |
| 81 | + onPress={onClose} |
| 82 | + /> |
| 83 | + </View> |
| 84 | + ), |
| 85 | + Button: ({ |
| 86 | + children, |
| 87 | + onPress, |
| 88 | + testID, |
| 89 | + }: { |
| 90 | + children: React.ReactNode; |
| 91 | + onPress: () => void; |
| 92 | + testID?: string; |
| 93 | + }) => ( |
| 94 | + <TouchableOpacity testID={testID} onPress={onPress}> |
| 95 | + <Text>{children}</Text> |
| 96 | + </TouchableOpacity> |
| 97 | + ), |
| 98 | + ButtonVariant: { Secondary: 'Secondary', Primary: 'Primary' }, |
| 99 | + Text: ({ children }: { children: React.ReactNode }) => ( |
| 100 | + <Text>{children}</Text> |
| 101 | + ), |
| 102 | + TextVariant: { HeadingMd: 'HeadingMd' }, |
| 103 | + }; |
| 104 | +}); |
| 105 | + |
| 106 | +jest.mock('@metamask/controller-utils', () => ({ |
| 107 | + toHex: jest.fn((val) => `0x${val.toString(16)}`), |
| 108 | +})); |
| 109 | + |
| 110 | +describe('NftGridItemBottomSheet', () => { |
| 111 | + const mockNft: Nft = { |
| 112 | + address: '0x123', |
| 113 | + tokenId: '456', |
| 114 | + name: 'Test NFT', |
| 115 | + image: 'https://example.com/nft.png', |
| 116 | + collection: { name: 'Test Collection' }, |
| 117 | + chainId: 1, |
| 118 | + isCurrentlyOwned: true, |
| 119 | + standard: 'ERC721', |
| 120 | + } as Nft; |
| 121 | + |
| 122 | + const onClose = jest.fn(); |
| 123 | + let alertSpy: jest.SpyInstance; |
| 124 | + |
| 125 | + beforeEach(() => { |
| 126 | + jest.clearAllMocks(); |
| 127 | + alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(jest.fn()); |
| 128 | + }); |
| 129 | + |
| 130 | + afterEach(() => { |
| 131 | + alertSpy.mockRestore(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('renders nothing when not visible', () => { |
| 135 | + const { queryByTestId } = render( |
| 136 | + <NftGridItemBottomSheet |
| 137 | + isVisible={false} |
| 138 | + onClose={onClose} |
| 139 | + nft={mockNft} |
| 140 | + />, |
| 141 | + ); |
| 142 | + |
| 143 | + expect(queryByTestId('nft-grid-item-bottom-sheet')).toBeNull(); |
| 144 | + }); |
| 145 | + |
| 146 | + it('renders bottom sheet with correct options when visible', () => { |
| 147 | + const { getByText, getByTestId } = render( |
| 148 | + <NftGridItemBottomSheet isVisible onClose={onClose} nft={mockNft} />, |
| 149 | + ); |
| 150 | + |
| 151 | + expect(getByTestId('nft-grid-item-bottom-sheet')).toBeDefined(); |
| 152 | + expect(getByText('wallet.collectible_action_title')).toBeDefined(); |
| 153 | + expect(getByText('wallet.refresh_metadata')).toBeDefined(); |
| 154 | + expect(getByText('wallet.remove')).toBeDefined(); |
| 155 | + expect(getByText('wallet.cancel')).toBeDefined(); |
| 156 | + }); |
| 157 | + |
| 158 | + it('calls NftController.addNft when refresh metadata is pressed', () => { |
| 159 | + const { getByTestId } = render( |
| 160 | + <NftGridItemBottomSheet isVisible onClose={onClose} nft={mockNft} />, |
| 161 | + ); |
| 162 | + |
| 163 | + fireEvent.press(getByTestId('nft-grid-item-bottom-sheet-refresh-button')); |
| 164 | + |
| 165 | + expect(Engine.context.NftController.addNft).toHaveBeenCalledWith( |
| 166 | + '0x123', |
| 167 | + '456', |
| 168 | + 'mainnet', |
| 169 | + ); |
| 170 | + }); |
| 171 | + |
| 172 | + it('calls NftController.removeAndIgnoreNft and shows alert when remove is pressed', () => { |
| 173 | + const { getByTestId } = render( |
| 174 | + <NftGridItemBottomSheet isVisible onClose={onClose} nft={mockNft} />, |
| 175 | + ); |
| 176 | + |
| 177 | + fireEvent.press(getByTestId('nft-grid-item-bottom-sheet-remove-button')); |
| 178 | + |
| 179 | + expect( |
| 180 | + Engine.context.NftController.removeAndIgnoreNft, |
| 181 | + ).toHaveBeenCalledWith('0x123', '456', 'mainnet'); |
| 182 | + expect(alertSpy).toHaveBeenCalledWith( |
| 183 | + 'wallet.collectible_removed_title', |
| 184 | + 'wallet.collectible_removed_desc', |
| 185 | + ); |
| 186 | + }); |
| 187 | + |
| 188 | + it('calls onClose when cancel is pressed', () => { |
| 189 | + const { getByText } = render( |
| 190 | + <NftGridItemBottomSheet isVisible onClose={onClose} nft={mockNft} />, |
| 191 | + ); |
| 192 | + |
| 193 | + fireEvent.press(getByText('wallet.cancel')); |
| 194 | + |
| 195 | + expect(onClose).toHaveBeenCalled(); |
| 196 | + }); |
| 197 | + |
| 198 | + it('calls onClose when header close button is pressed', () => { |
| 199 | + const { getByTestId } = render( |
| 200 | + <NftGridItemBottomSheet isVisible onClose={onClose} nft={mockNft} />, |
| 201 | + ); |
| 202 | + |
| 203 | + fireEvent.press(getByTestId('bottom-sheet-header-close')); |
| 204 | + |
| 205 | + expect(onClose).toHaveBeenCalled(); |
| 206 | + }); |
| 207 | + |
| 208 | + it('does nothing when nft is null', () => { |
| 209 | + const { getByTestId } = render( |
| 210 | + <NftGridItemBottomSheet isVisible onClose={onClose} nft={null} />, |
| 211 | + ); |
| 212 | + |
| 213 | + fireEvent.press(getByTestId('nft-grid-item-bottom-sheet-remove-button')); |
| 214 | + fireEvent.press(getByTestId('nft-grid-item-bottom-sheet-refresh-button')); |
| 215 | + |
| 216 | + expect( |
| 217 | + Engine.context.NftController.removeAndIgnoreNft, |
| 218 | + ).not.toHaveBeenCalled(); |
| 219 | + expect(Engine.context.NftController.addNft).not.toHaveBeenCalled(); |
| 220 | + }); |
| 221 | +}); |
0 commit comments