2022-06-28 02:31:25 +02:00
import React from 'react' ;
2023-02-07 19:06:37 +01:00
import { fireEvent , waitFor } from '@testing-library/react' ;
2022-06-28 02:31:25 +02:00
import configureMockStore from 'redux-mock-store' ;
2023-02-07 19:06:37 +01:00
import thunk from 'redux-thunk' ;
import { useHistory } from 'react-router-dom' ;
2023-07-14 18:18:41 +02:00
import { renderWithProvider } from '../../../../test/jest/rendering' ;
import mockState from '../../../../test/data/mock-state.json' ;
import { DEFAULT _ROUTE } from '../../../helpers/constants/routes' ;
2023-02-07 19:06:37 +01:00
import {
addNftVerifyOwnership ,
ignoreTokens ,
2023-02-16 20:23:29 +01:00
setNewNftAddedMessage ,
updateNftDropDownState ,
2023-07-14 18:18:41 +02:00
} from '../../../store/actions' ;
import { ImportNftsModal } from '.' ;
2022-06-28 02:31:25 +02:00
const VALID _ADDRESS = '0x312BE6a98441F9F6e3F6246B13CA19701e0AC3B9' ;
const INVALID _ADDRESS = 'aoinsafasdfa' ;
const VALID _TOKENID = '1201' ;
const INVALID _TOKENID = 'abcde' ;
2023-02-07 19:06:37 +01:00
jest . mock ( 'react-router-dom' , ( ) => ( {
... jest . requireActual ( 'react-router-dom' ) ,
useHistory : jest . fn (
jest . fn ( ) . mockReturnValue ( {
push : jest . fn ( ) ,
location : {
state : {
tokenAddress : '0xTokenAddress' ,
} ,
} ,
} ) ,
) ,
} ) ) ;
2023-07-14 18:18:41 +02:00
jest . mock ( '../../../store/actions.ts' , ( ) => ( {
2023-02-07 19:06:37 +01:00
addNftVerifyOwnership : jest
. fn ( )
. mockReturnValue ( jest . fn ( ) . mockResolvedValue ( ) ) ,
getTokenStandardAndDetails : jest . fn ( ) . mockResolvedValue ( ) ,
ignoreTokens : jest . fn ( ) . mockReturnValue ( jest . fn ( ) . mockResolvedValue ( ) ) ,
2023-02-16 20:23:29 +01:00
setNewNftAddedMessage : jest
2023-02-07 19:06:37 +01:00
. fn ( )
. mockReturnValue ( jest . fn ( ) . mockResolvedValue ( ) ) ,
2023-02-16 20:23:29 +01:00
updateNftDropDownState : jest
2023-02-07 19:06:37 +01:00
. fn ( )
. mockReturnValue ( jest . fn ( ) . mockResolvedValue ( ) ) ,
} ) ) ;
2023-07-14 18:18:41 +02:00
describe ( 'ImportNftsModal' , ( ) => {
2023-02-07 19:06:37 +01:00
const store = configureMockStore ( [ thunk ] ) ( mockState ) ;
beforeEach ( ( ) => {
jest . restoreAllMocks ( ) ;
2022-06-28 02:31:25 +02:00
} ) ;
2023-07-14 18:18:41 +02:00
it ( 'should enable the "Import" button when valid entries are input into both Address and TokenId fields' , ( ) => {
const { getByText , getByPlaceholderText } = renderWithProvider (
< ImportNftsModal / > ,
store ,
) ;
expect ( getByText ( 'Import' ) ) . not . toBeEnabled ( ) ;
const addressInput = getByPlaceholderText ( '0x...' ) ;
const tokenIdInput = getByPlaceholderText ( 'Enter the token id' ) ;
fireEvent . change ( addressInput , {
2022-06-28 02:31:25 +02:00
target : { value : VALID _ADDRESS } ,
} ) ;
2023-07-14 18:18:41 +02:00
fireEvent . change ( tokenIdInput , {
2022-06-28 02:31:25 +02:00
target : { value : VALID _TOKENID } ,
} ) ;
2023-07-14 18:18:41 +02:00
expect ( getByText ( 'Import' ) ) . toBeEnabled ( ) ;
2022-06-28 02:31:25 +02:00
} ) ;
2023-07-14 18:18:41 +02:00
it ( 'should not enable the "Import" button when an invalid entry is input into one or both Address and TokenId fields' , ( ) => {
const { getByText , getByPlaceholderText } = renderWithProvider (
< ImportNftsModal / > ,
store ,
) ;
expect ( getByText ( 'Import' ) ) . not . toBeEnabled ( ) ;
const addressInput = getByPlaceholderText ( '0x...' ) ;
const tokenIdInput = getByPlaceholderText ( 'Enter the token id' ) ;
fireEvent . change ( addressInput , {
2022-06-28 02:31:25 +02:00
target : { value : INVALID _ADDRESS } ,
} ) ;
2023-07-14 18:18:41 +02:00
fireEvent . change ( tokenIdInput , {
2022-06-28 02:31:25 +02:00
target : { value : VALID _TOKENID } ,
} ) ;
2023-07-14 18:18:41 +02:00
expect ( getByText ( 'Import' ) ) . not . toBeEnabled ( ) ;
fireEvent . change ( addressInput , {
2022-06-28 02:31:25 +02:00
target : { value : VALID _ADDRESS } ,
} ) ;
2023-07-14 18:18:41 +02:00
expect ( getByText ( 'Import' ) ) . toBeEnabled ( ) ;
fireEvent . change ( tokenIdInput , {
2022-06-28 02:31:25 +02:00
target : { value : INVALID _TOKENID } ,
} ) ;
2023-07-14 18:18:41 +02:00
expect ( getByText ( 'Import' ) ) . not . toBeEnabled ( ) ;
2022-06-28 02:31:25 +02:00
} ) ;
2023-02-16 20:23:29 +01:00
it ( 'should call addNftVerifyOwnership, updateNftDropDownState, setNewNftAddedMessage, and ignoreTokens action with correct values (tokenId should not be in scientific notation)' , async ( ) => {
2023-07-14 18:18:41 +02:00
const onClose = jest . fn ( ) ;
const { getByPlaceholderText , getByText } = renderWithProvider (
< ImportNftsModal onClose = { onClose } / > ,
store ,
) ;
const addressInput = getByPlaceholderText ( '0x...' ) ;
const tokenIdInput = getByPlaceholderText ( 'Enter the token id' ) ;
fireEvent . change ( addressInput , {
2022-06-28 02:31:25 +02:00
target : { value : VALID _ADDRESS } ,
} ) ;
const LARGE _TOKEN _ID = Number . MAX _SAFE _INTEGER + 1 ;
2023-07-14 18:18:41 +02:00
fireEvent . change ( tokenIdInput , {
2022-06-28 02:31:25 +02:00
target : { value : LARGE _TOKEN _ID } ,
} ) ;
2023-07-14 18:18:41 +02:00
fireEvent . click ( getByText ( 'Import' ) ) ;
2023-02-07 19:06:37 +01:00
await waitFor ( ( ) => {
expect ( addNftVerifyOwnership ) . toHaveBeenCalledWith (
'0x312BE6a98441F9F6e3F6246B13CA19701e0AC3B9' ,
'9007199254740992' ,
) ;
2023-02-16 20:23:29 +01:00
expect ( updateNftDropDownState ) . toHaveBeenCalledWith ( {
2023-02-07 19:06:37 +01:00
'0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc' : {
'0x5' : {
'0x312BE6a98441F9F6e3F6246B13CA19701e0AC3B9' : true ,
'0x495f947276749Ce646f68AC8c248420045cb7b5e' : false ,
} ,
} ,
} ) ;
2023-02-16 20:23:29 +01:00
expect ( setNewNftAddedMessage ) . toHaveBeenCalledWith ( 'success' ) ;
2023-02-07 19:06:37 +01:00
expect ( ignoreTokens ) . toHaveBeenCalledWith ( {
dontShowLoadingIndicator : true ,
tokensToIgnore : '0xTokenAddress' ,
} ) ;
} ) ;
} ) ;
it ( 'should throw error message and click close on failure message' , async ( ) => {
addNftVerifyOwnership . mockImplementation ( ( ) =>
jest . fn ( ) . mockRejectedValue ( new Error ( 'error' ) ) ,
2022-06-28 02:31:25 +02:00
) ;
2023-02-07 19:06:37 +01:00
2023-07-14 18:18:41 +02:00
const { getByTestId , getByText , getByPlaceholderText } = renderWithProvider (
< ImportNftsModal / > ,
store ,
) ;
const addressInput = getByPlaceholderText ( '0x...' ) ;
const tokenIdInput = getByPlaceholderText ( 'Enter the token id' ) ;
fireEvent . change ( addressInput , {
2023-02-07 19:06:37 +01:00
target : { value : VALID _ADDRESS } ,
} ) ;
const LARGE _TOKEN _ID = Number . MAX _SAFE _INTEGER + 1 ;
2023-07-14 18:18:41 +02:00
fireEvent . change ( tokenIdInput , {
2023-02-07 19:06:37 +01:00
target : { value : LARGE _TOKEN _ID } ,
} ) ;
2023-07-14 18:18:41 +02:00
fireEvent . click ( getByText ( 'Import' ) ) ;
2023-02-07 19:06:37 +01:00
await waitFor ( ( ) => {
2023-02-16 20:23:29 +01:00
expect ( setNewNftAddedMessage ) . toHaveBeenCalledWith ( 'error' ) ;
2023-02-07 19:06:37 +01:00
} ) ;
2023-02-27 17:42:02 +01:00
const addNftClose = getByTestId ( 'add-nft-error-close' ) ;
2023-02-07 19:06:37 +01:00
2023-02-16 20:23:29 +01:00
fireEvent . click ( addNftClose ) ;
2023-02-07 19:06:37 +01:00
} ) ;
it ( 'should route to default route when cancel button is clicked' , ( ) => {
2023-07-14 18:18:41 +02:00
const onClose = jest . fn ( ) ;
const { getByText } = renderWithProvider (
< ImportNftsModal onClose = { onClose } / > ,
store ,
) ;
2023-02-07 19:06:37 +01:00
2023-07-14 18:18:41 +02:00
const cancelButton = getByText ( 'Cancel' ) ;
2023-02-07 19:06:37 +01:00
fireEvent . click ( cancelButton ) ;
expect ( useHistory ( ) . push ) . toHaveBeenCalledWith ( DEFAULT _ROUTE ) ;
} ) ;
it ( 'should route to default route when close button is clicked' , ( ) => {
2023-07-14 18:18:41 +02:00
const onClose = jest . fn ( ) ;
renderWithProvider ( < ImportNftsModal onClose = { onClose } / > , store ) ;
2023-02-07 19:06:37 +01:00
2023-07-14 18:18:41 +02:00
fireEvent . click ( document . querySelector ( 'button[aria-label="Close"]' ) ) ;
2023-02-07 19:06:37 +01:00
expect ( useHistory ( ) . push ) . toHaveBeenCalledWith ( DEFAULT _ROUTE ) ;
2022-06-28 02:31:25 +02:00
} ) ;
} ) ;