Skip to content

Commit

Permalink
fix: add validation error on NFT import
Browse files Browse the repository at this point in the history
  • Loading branch information
sahar-fehri committed Sep 25, 2023
1 parent 6bddeba commit a638876
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export const ImportNftsModal = ({ onClose }) => {
const [disabled, setDisabled] = useState(true);
const [nftAddFailed, setNftAddFailed] = useState(false);
const trackEvent = useContext(MetaMetricsContext);
const [nftAddressValidationError, setNftAddressValidationError] =
useState(null);

const handleAddNft = async () => {
try {
Expand Down Expand Up @@ -129,6 +131,10 @@ export const ImportNftsModal = ({ onClose }) => {
};

const validateAndSetAddress = (val) => {
setNftAddressValidationError(null);
if (!isValidHexAddress(val)) {
setNftAddressValidationError(t('invalidAddress'));
}
setDisabled(!isValidHexAddress(val) || !tokenId);
setNftAddress(val);
};
Expand Down Expand Up @@ -210,6 +216,8 @@ export const ImportNftsModal = ({ onClose }) => {
validateAndSetAddress(e.target.value);
setNftAddFailed(false);
}}
helpText={nftAddressValidationError}
error={nftAddressValidationError}
/>
</Box>
<Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,20 @@ describe('ImportNftsModal', () => {

expect(useHistory().push).toHaveBeenCalledWith(DEFAULT_ROUTE);
});

it('should set error message when address invalid', () => {
const onClose = jest.fn();
const { getByText, getByPlaceholderText } = renderWithProvider(
<ImportNftsModal onClose={onClose} />,
store,
);

const addressInput = getByPlaceholderText('0x...');
fireEvent.change(addressInput, {
target: { value: INVALID_ADDRESS },
});

const errorMessage = getByText('Invalid address');
expect(errorMessage).toBeInTheDocument();
});
});

0 comments on commit a638876

Please sign in to comment.