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' ;
2022-06-28 02:31:25 +02:00
import { renderWithProvider } from '../../../test/jest/rendering' ;
2023-02-07 19:06:37 +01:00
import mockState from '../../../test/data/mock-state.json' ;
import { DEFAULT _ROUTE } from '../../helpers/constants/routes' ;
import {
addNftVerifyOwnership ,
ignoreTokens ,
2023-02-16 20:23:29 +01:00
setNewNftAddedMessage ,
updateNftDropDownState ,
2023-02-07 19:06:37 +01:00
} from '../../store/actions' ;
2023-02-16 20:23:29 +01:00
import AddNft 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' ,
} ,
} ,
} ) ,
) ,
} ) ) ;
jest . mock ( '../../store/actions.ts' , ( ) => ( {
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-02-16 20:23:29 +01:00
describe ( 'AddNft' , ( ) => {
2023-02-07 19:06:37 +01:00
const store = configureMockStore ( [ thunk ] ) ( mockState ) ;
beforeEach ( ( ) => {
jest . restoreAllMocks ( ) ;
2022-06-28 02:31:25 +02:00
} ) ;
it ( 'should enable the "Add" button when valid entries are input into both Address and TokenId fields' , ( ) => {
2023-02-16 20:23:29 +01:00
const { getByTestId , getByText } = renderWithProvider ( < AddNft / > , store ) ;
2022-06-28 02:31:25 +02:00
expect ( getByText ( 'Add' ) ) . not . toBeEnabled ( ) ;
fireEvent . change ( getByTestId ( 'address' ) , {
target : { value : VALID _ADDRESS } ,
} ) ;
fireEvent . change ( getByTestId ( 'token-id' ) , {
target : { value : VALID _TOKENID } ,
} ) ;
expect ( getByText ( 'Add' ) ) . toBeEnabled ( ) ;
} ) ;
it ( 'should not enable the "Add" button when an invalid entry is input into one or both Address and TokenId fields' , ( ) => {
2023-02-16 20:23:29 +01:00
const { getByTestId , getByText } = renderWithProvider ( < AddNft / > , store ) ;
2022-06-28 02:31:25 +02:00
expect ( getByText ( 'Add' ) ) . not . toBeEnabled ( ) ;
fireEvent . change ( getByTestId ( 'address' ) , {
target : { value : INVALID _ADDRESS } ,
} ) ;
fireEvent . change ( getByTestId ( 'token-id' ) , {
target : { value : VALID _TOKENID } ,
} ) ;
expect ( getByText ( 'Add' ) ) . not . toBeEnabled ( ) ;
fireEvent . change ( getByTestId ( 'address' ) , {
target : { value : VALID _ADDRESS } ,
} ) ;
expect ( getByText ( 'Add' ) ) . toBeEnabled ( ) ;
fireEvent . change ( getByTestId ( 'token-id' ) , {
target : { value : INVALID _TOKENID } ,
} ) ;
expect ( getByText ( 'Add' ) ) . not . toBeEnabled ( ) ;
} ) ;
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 ( ) => {
const { getByTestId , getByText } = renderWithProvider ( < AddNft / > , store ) ;
2022-06-28 02:31:25 +02:00
fireEvent . change ( getByTestId ( 'address' ) , {
target : { value : VALID _ADDRESS } ,
} ) ;
const LARGE _TOKEN _ID = Number . MAX _SAFE _INTEGER + 1 ;
fireEvent . change ( getByTestId ( 'token-id' ) , {
target : { value : LARGE _TOKEN _ID } ,
} ) ;
fireEvent . click ( getByText ( 'Add' ) ) ;
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-02-27 17:42:02 +01:00
const { getByTestId , getByText } = renderWithProvider ( < AddNft / > , store ) ;
2023-02-07 19:06:37 +01:00
fireEvent . change ( getByTestId ( 'address' ) , {
target : { value : VALID _ADDRESS } ,
} ) ;
const LARGE _TOKEN _ID = Number . MAX _SAFE _INTEGER + 1 ;
fireEvent . change ( getByTestId ( 'token-id' ) , {
target : { value : LARGE _TOKEN _ID } ,
} ) ;
fireEvent . click ( getByText ( 'Add' ) ) ;
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-02-16 20:23:29 +01:00
const { queryByTestId } = renderWithProvider ( < AddNft / > , store ) ;
2023-02-07 19:06:37 +01:00
const cancelButton = queryByTestId ( 'page-container-footer-cancel' ) ;
fireEvent . click ( cancelButton ) ;
expect ( useHistory ( ) . push ) . toHaveBeenCalledWith ( DEFAULT _ROUTE ) ;
} ) ;
it ( 'should route to default route when close button is clicked' , ( ) => {
2023-02-16 20:23:29 +01:00
const { queryByLabelText } = renderWithProvider ( < AddNft / > , store ) ;
2023-02-07 19:06:37 +01:00
const closeButton = queryByLabelText ( 'close' ) ;
fireEvent . click ( closeButton ) ;
expect ( useHistory ( ) . push ) . toHaveBeenCalledWith ( DEFAULT _ROUTE ) ;
2022-06-28 02:31:25 +02:00
} ) ;
} ) ;