2021-12-14 00:41:10 +01:00
import React from 'react' ;
import { fireEvent , screen } from '@testing-library/react' ;
import reactRouterDom from 'react-router-dom' ;
import configureStore from '../../../store/store' ;
import { renderWithProvider } from '../../../../test/jest/rendering' ;
import { EXPERIMENTAL _ROUTE } from '../../../helpers/constants/routes' ;
import { setBackgroundConnection } from '../../../../test/jest' ;
2023-01-25 17:33:06 +01:00
import { hexToDecimal } from '../../../../shared/modules/conversion.utils' ;
2023-02-16 20:23:29 +01:00
import NftsTab from '.' ;
2021-12-14 00:41:10 +01:00
2023-02-16 20:23:29 +01:00
const NFTS = [
2021-12-14 00:41:10 +01:00
{
address : '0x495f947276749Ce646f68AC8c248420045cb7b5e' ,
tokenId :
'58076532811975507823669075598676816378162417803895263482849101575514658701313' ,
name : 'Punk #4' ,
creator : {
user : {
username : null ,
} ,
profile _img _url : null ,
address : '0x806627172af48bd5b0765d3449a7def80d6576ff' ,
config : '' ,
} ,
description : 'Red Mohawk bam!' ,
image :
'https://lh3.googleusercontent.com/BdxvLseXcfl57BiuQcQYdJ64v-aI8din7WPk0Pgo3qQFhAUH-B6i-dCqqc_mCkRIzULmwzwecnohLhrcH8A9mpWIZqA7ygc52Sr81hE' ,
standard : 'ERC1155' ,
} ,
{
address : '0x495f947276749Ce646f68AC8c248420045cb7b5e' ,
tokenId :
'58076532811975507823669075598676816378162417803895263482849101574415147073537' ,
name : 'Punk #3' ,
creator : {
user : {
username : null ,
} ,
profile _img _url : null ,
address : '0x806627172af48bd5b0765d3449a7def80d6576ff' ,
config : '' ,
} ,
description : 'Clown PUNK!!!' ,
image :
'https://lh3.googleusercontent.com/H7VrxaalZv4PF1B8U7ADuc8AfuqTVyzmMEDQ5OXKlx0Tqu5XiwsKYj4j_pAF6wUJjLMQbSN_0n3fuj84lNyRhFW9hyrxqDfY1IiQEQ' ,
standard : 'ERC1155' ,
} ,
{
address : '0x495f947276749Ce646f68AC8c248420045cb7b5e' ,
tokenId :
'58076532811975507823669075598676816378162417803895263482849101573315635445761' ,
name : 'Punk #2' ,
creator : {
user : {
username : null ,
} ,
profile _img _url : null ,
address : '0x806627172af48bd5b0765d3449a7def80d6576ff' ,
config : '' ,
} ,
description : 'Got glasses and black hair!' ,
image :
'https://lh3.googleusercontent.com/CHNTSlKB_Gob-iwTq8jcag6XwBkTqBMLt_vEKeBv18Q4AoPFAEPceqK6mRzkad2s5djx6CT5zbGQwDy81WwtNzViK5dQbG60uAWv' ,
standard : 'ERC1155' ,
} ,
{
address : '0x495f947276749Ce646f68AC8c248420045cb7b5e' ,
tokenId :
'58076532811975507823669075598676816378162417803895263482849101572216123817985' ,
name : 'Punk #1' ,
creator : {
user : {
username : null ,
} ,
profile _img _url : null ,
address : '0x806627172af48bd5b0765d3449a7def80d6576ff' ,
config : '' ,
} ,
image :
'https://lh3.googleusercontent.com/4jfPi-nQNWCUXD5qVNVWX7LX2UufU_elEJcvICFlsTdcBXv70asnDEOlI8oKECZxlXq1wseeIXMwmP5tLyOUxMKk' ,
standard : 'ERC1155' ,
} ,
{
address : '0x495f947276749Ce646f68AC8c248420045cb7b5e' ,
tokenId :
'58076532811975507823669075598676816378162417803895263482849101571116612190209' ,
name : 'Punk #4651' ,
creator : {
user : {
username : null ,
} ,
profile _img _url : null ,
address : '0x806627172af48bd5b0765d3449a7def80d6576ff' ,
config : '' ,
} ,
image :
'https://lh3.googleusercontent.com/BdxvLseXcfl57BiuQcQYdJ64v-aI8din7WPk0Pgo3qQFhAUH-B6i-dCqqc_mCkRIzULmwzwecnohLhrcH8A9mpWIZqA7ygc52Sr81hE' ,
standard : 'ERC1155' ,
} ,
{
address : '0xDc7382Eb0Bc9C352A4CbA23c909bDA01e0206414' ,
tokenId : '1' ,
name : 'MUNK #1' ,
description : null ,
image : 'ipfs://QmTSZUNt8AKyDabkyXXXP4oHWDnaVXgNdXoJGEyaYzLbeL' ,
standard : 'ERC721' ,
} ,
{
address : '0xDc7382Eb0Bc9C352A4CbA23c909bDA01e0206414' ,
tokenId : '2' ,
name : 'MUNK #2' ,
description : null ,
image : 'ipfs://QmTSZUNt8AKyDabkyXXXP4oHWDnaVXgNdXoJGEyaYzLbeL' ,
standard : 'ERC721' ,
} ,
{
address : '0xDc7382Eb0Bc9C352A4CbA23c909bDA01e0206414' ,
tokenId : '3' ,
name : 'MUNK #3' ,
description : null ,
image : 'ipfs://QmTSZUNt8AKyDabkyXXXP4oHWDnaVXgNdXoJGEyaYzLbeL' ,
standard : 'ERC721' ,
} ,
] ;
2023-02-16 20:23:29 +01:00
const NFTS _CONTRACTS = [
2021-12-14 00:41:10 +01:00
{
address : '0x495f947276749Ce646f68AC8c248420045cb7b5e' ,
name : 'PUNKS' ,
symbol : 'PNKS' ,
schemaName : 'ERC1155' ,
} ,
{
address : '0xDc7382Eb0Bc9C352A4CbA23c909bDA01e0206414' ,
name : 'Munks' ,
symbol : 'MNKS' ,
} ,
] ;
2023-02-16 20:23:29 +01:00
const nftsDropdownState = {
2022-07-26 20:10:51 +02:00
'0x495f947276749ce646f68ac8c248420045cb7b5e' : true ,
'0xdc7382eb0bc9c352a4cba23c909bda01e0206414' : true ,
2022-01-27 18:26:33 +01:00
} ;
2021-12-14 00:41:10 +01:00
const ACCOUNT _1 = '0x123' ;
const ACCOUNT _2 = '0x456' ;
const render = ( {
2023-02-16 20:23:29 +01:00
nftContracts = [ ] ,
nfts = [ ] ,
2021-12-14 00:41:10 +01:00
selectedAddress ,
chainId = '0x1' ,
2022-11-15 19:49:42 +01:00
useNftDetection ,
2021-12-14 00:41:10 +01:00
onAddNFT = jest . fn ( ) ,
} ) => {
2023-01-25 17:33:06 +01:00
const chainIdAsDecimal = hexToDecimal ( chainId ) ;
2021-12-14 00:41:10 +01:00
const store = configureStore ( {
metamask : {
2022-11-15 19:49:42 +01:00
allNfts : {
2021-12-14 00:41:10 +01:00
[ ACCOUNT _1 ] : {
2023-02-16 20:23:29 +01:00
[ chainIdAsDecimal ] : nfts ,
2021-12-14 00:41:10 +01:00
} ,
} ,
2022-11-15 19:49:42 +01:00
allNftContracts : {
2021-12-14 00:41:10 +01:00
[ ACCOUNT _1 ] : {
2023-02-16 20:23:29 +01:00
[ chainIdAsDecimal ] : nftContracts ,
2021-12-14 00:41:10 +01:00
} ,
} ,
provider : { chainId } ,
selectedAddress ,
2022-11-15 19:49:42 +01:00
useNftDetection ,
2023-02-16 20:23:29 +01:00
nftsDropdownState ,
2021-12-14 00:41:10 +01:00
} ,
} ) ;
2023-02-16 20:23:29 +01:00
return renderWithProvider ( < NftsTab onAddNFT = { onAddNFT } / > , store ) ;
2021-12-14 00:41:10 +01:00
} ;
2023-02-16 20:23:29 +01:00
describe ( 'NFT Items' , ( ) => {
const detectNftsStub = jest . fn ( ) ;
2022-01-03 21:39:41 +01:00
const getStateStub = jest . fn ( ) ;
2023-02-16 20:23:29 +01:00
const checkAndUpdateAllNftsOwnershipStatusStub = jest . fn ( ) ;
const updateNftDropDownStateStub = jest . fn ( ) ;
2021-12-14 00:41:10 +01:00
setBackgroundConnection ( {
2023-02-16 20:23:29 +01:00
detectNfts : detectNftsStub ,
2022-01-03 21:39:41 +01:00
getState : getStateStub ,
2022-11-15 19:49:42 +01:00
checkAndUpdateAllNftsOwnershipStatus :
2023-02-16 20:23:29 +01:00
checkAndUpdateAllNftsOwnershipStatusStub ,
updateNftDropDownState : updateNftDropDownStateStub ,
2021-12-14 00:41:10 +01:00
} ) ;
const historyPushMock = jest . fn ( ) ;
jest
. spyOn ( reactRouterDom , 'useHistory' )
. mockImplementation ( )
. mockReturnValue ( { push : historyPushMock } ) ;
afterEach ( ( ) => {
jest . clearAllMocks ( ) ;
} ) ;
2023-02-16 20:23:29 +01:00
describe ( 'NFTs Detection Notice' , ( ) => {
it ( 'should render the NFTs Detection Notice when currently selected network is Mainnet and currently selected account has no nfts' , ( ) => {
2021-12-14 00:41:10 +01:00
render ( {
selectedAddress : ACCOUNT _2 ,
2023-02-16 20:23:29 +01:00
nfts : NFTS ,
2021-12-14 00:41:10 +01:00
} ) ;
expect ( screen . queryByText ( 'New! NFT detection' ) ) . toBeInTheDocument ( ) ;
} ) ;
2023-02-16 20:23:29 +01:00
it ( 'should not render the NFTs Detection Notice when currently selected network is Mainnet and currently selected account has NFTs' , ( ) => {
2021-12-14 00:41:10 +01:00
render ( {
selectedAddress : ACCOUNT _1 ,
2023-02-16 20:23:29 +01:00
nfts : NFTS ,
2021-12-14 00:41:10 +01:00
} ) ;
expect ( screen . queryByText ( 'New! NFT detection' ) ) . not . toBeInTheDocument ( ) ;
} ) ;
2022-07-18 16:43:30 +02:00
it ( 'should take user to the experimental settings tab in settings when user clicks "Turn on NFT detection in Settings"' , ( ) => {
2021-12-14 00:41:10 +01:00
render ( {
selectedAddress : ACCOUNT _2 ,
2023-02-16 20:23:29 +01:00
nfts : NFTS ,
2021-12-14 00:41:10 +01:00
} ) ;
fireEvent . click ( screen . queryByText ( 'Turn on NFT detection in Settings' ) ) ;
expect ( historyPushMock ) . toHaveBeenCalledTimes ( 1 ) ;
2022-07-18 16:43:30 +02:00
expect ( historyPushMock ) . toHaveBeenCalledWith (
` ${ EXPERIMENTAL _ROUTE } #autodetect-nfts ` ,
) ;
2021-12-14 00:41:10 +01:00
} ) ;
2023-02-16 20:23:29 +01:00
it ( 'should not render the NFTs Detection Notice when currently selected network is Mainnet and currently selected account has no NFTs but use NFT autodetection preference is set to true' , ( ) => {
2021-12-14 00:41:10 +01:00
render ( {
selectedAddress : ACCOUNT _1 ,
2023-02-16 20:23:29 +01:00
nfts : NFTS ,
2022-11-15 19:49:42 +01:00
useNftDetection : true ,
2021-12-14 00:41:10 +01:00
} ) ;
expect ( screen . queryByText ( 'New! NFT detection' ) ) . not . toBeInTheDocument ( ) ;
} ) ;
2023-02-16 20:23:29 +01:00
it ( 'should not render the NFTs Detection Notice when currently selected network is Mainnet and currently selected account has no NFTs but user has dismissed the notice before' , ( ) => {
2021-12-14 00:41:10 +01:00
render ( {
selectedAddress : ACCOUNT _1 ,
2023-02-16 20:23:29 +01:00
nfts : NFTS ,
2021-12-14 00:41:10 +01:00
} ) ;
expect ( screen . queryByText ( 'New! NFT detection' ) ) . not . toBeInTheDocument ( ) ;
} ) ;
} ) ;
describe ( 'Collections' , ( ) => {
2023-02-16 20:23:29 +01:00
it ( 'should render the name of the collections and number of NFTs in each collection if current account/chainId combination has NFTs' , ( ) => {
2021-12-14 00:41:10 +01:00
render ( {
selectedAddress : ACCOUNT _1 ,
2023-02-16 20:23:29 +01:00
nfts : NFTS ,
nftContracts : NFTS _CONTRACTS ,
2021-12-14 00:41:10 +01:00
} ) ;
expect ( screen . queryByText ( 'PUNKS (5)' ) ) . toBeInTheDocument ( ) ;
expect ( screen . queryByText ( 'Munks (3)' ) ) . toBeInTheDocument ( ) ;
} ) ;
2023-02-16 20:23:29 +01:00
it ( 'should not render collections if current account/chainId combination has NFTs' , ( ) => {
2021-12-14 00:41:10 +01:00
render ( {
selectedAddress : ACCOUNT _2 ,
2023-02-16 20:23:29 +01:00
nfts : NFTS ,
nftContracts : NFTS _CONTRACTS ,
2021-12-14 00:41:10 +01:00
} ) ;
expect ( screen . queryByText ( 'PUNKS (5)' ) ) . not . toBeInTheDocument ( ) ;
expect ( screen . queryByText ( 'Munks (3)' ) ) . not . toBeInTheDocument ( ) ;
} ) ;
} ) ;
2023-02-16 20:23:29 +01:00
describe ( 'NFTs options' , ( ) => {
it ( 'should render a link "Refresh list" when some NFTs are present on mainnet and NFT auto-detection preference is set to true, which, when clicked calls methods DetectNFTs and checkAndUpdateNftsOwnershipStatus' , ( ) => {
2021-12-14 00:41:10 +01:00
render ( {
selectedAddress : ACCOUNT _1 ,
2023-02-16 20:23:29 +01:00
nfts : NFTS ,
2022-11-15 19:49:42 +01:00
useNftDetection : true ,
2021-12-14 00:41:10 +01:00
} ) ;
2023-02-16 20:23:29 +01:00
expect ( detectNftsStub ) . not . toHaveBeenCalled ( ) ;
expect ( checkAndUpdateAllNftsOwnershipStatusStub ) . not . toHaveBeenCalled ( ) ;
2021-12-14 00:41:10 +01:00
fireEvent . click ( screen . queryByText ( 'Refresh list' ) ) ;
2023-02-16 20:23:29 +01:00
expect ( detectNftsStub ) . toHaveBeenCalled ( ) ;
expect ( checkAndUpdateAllNftsOwnershipStatusStub ) . toHaveBeenCalled ( ) ;
2022-01-03 21:39:41 +01:00
} ) ;
2023-02-16 20:23:29 +01:00
it ( 'should render a link "Refresh list" when some NFTs are present on a non-mainnet chain, which, when clicked calls a method checkAndUpdateNftsOwnershipStatus' , ( ) => {
2022-01-03 21:39:41 +01:00
render ( {
2022-09-29 05:26:01 +02:00
chainId : '0x5' ,
2022-01-03 21:39:41 +01:00
selectedAddress : ACCOUNT _1 ,
2023-02-16 20:23:29 +01:00
nfts : NFTS ,
2022-11-15 19:49:42 +01:00
useNftDetection : true ,
2022-01-03 21:39:41 +01:00
} ) ;
2023-02-16 20:23:29 +01:00
expect ( checkAndUpdateAllNftsOwnershipStatusStub ) . not . toHaveBeenCalled ( ) ;
2022-01-03 21:39:41 +01:00
fireEvent . click ( screen . queryByText ( 'Refresh list' ) ) ;
2023-02-16 20:23:29 +01:00
expect ( checkAndUpdateAllNftsOwnershipStatusStub ) . toHaveBeenCalled ( ) ;
2021-12-14 00:41:10 +01:00
} ) ;
2023-02-16 20:23:29 +01:00
it ( 'should render a link "Enable autodetect" when some NFTs are present and NFT auto-detection preference is set to false, which, when clicked sends user to the experimental tab of settings' , ( ) => {
2021-12-14 00:41:10 +01:00
render ( {
selectedAddress : ACCOUNT _1 ,
2023-02-16 20:23:29 +01:00
nfts : NFTS ,
2021-12-14 00:41:10 +01:00
} ) ;
expect ( historyPushMock ) . toHaveBeenCalledTimes ( 0 ) ;
2022-08-08 21:01:38 +02:00
fireEvent . click ( screen . queryByText ( 'Enable autodetect' ) ) ;
2021-12-14 00:41:10 +01:00
expect ( historyPushMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( historyPushMock ) . toHaveBeenCalledWith ( EXPERIMENTAL _ROUTE ) ;
} ) ;
2023-02-16 20:23:29 +01:00
it ( 'should render a link "Import NFTs" when some NFTs are present, which, when clicked calls the passed in onAddNFT method' , ( ) => {
2021-12-14 00:41:10 +01:00
const onAddNFTStub = jest . fn ( ) ;
render ( {
selectedAddress : ACCOUNT _1 ,
2023-02-16 20:23:29 +01:00
nfts : NFTS ,
2021-12-14 00:41:10 +01:00
onAddNFT : onAddNFTStub ,
} ) ;
expect ( onAddNFTStub ) . toHaveBeenCalledTimes ( 0 ) ;
fireEvent . click ( screen . queryByText ( 'Import NFTs' ) ) ;
expect ( onAddNFTStub ) . toHaveBeenCalledTimes ( 1 ) ;
} ) ;
} ) ;
} ) ;