1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Fix tokenIds larger than MAX_SAFE_INTEGER converted to scientific notation and failing to import (#15016)

* fix tokenIds larger than MAX_SAFE_INTEGER breaking

* add tests
This commit is contained in:
Alex Donesky 2022-06-27 19:31:25 -05:00 committed by GitHub
parent bf9140aa57
commit 5290402181
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 8 deletions

View File

@ -4,7 +4,6 @@ import { useDispatch, useSelector } from 'react-redux';
import { util } from '@metamask/controllers'; import { util } from '@metamask/controllers';
import { useI18nContext } from '../../hooks/useI18nContext'; import { useI18nContext } from '../../hooks/useI18nContext';
import { DEFAULT_ROUTE } from '../../helpers/constants/routes'; import { DEFAULT_ROUTE } from '../../helpers/constants/routes';
import { import {
DISPLAY, DISPLAY,
FONT_WEIGHT, FONT_WEIGHT,
@ -55,9 +54,7 @@ export default function AddCollectible() {
const handleAddCollectible = async () => { const handleAddCollectible = async () => {
try { try {
await dispatch( await dispatch(addCollectibleVerifyOwnership(address, tokenId));
addCollectibleVerifyOwnership(address, tokenId.toString()),
);
} catch (error) { } catch (error) {
const { message } = error; const { message } = error;
dispatch(setNewCollectibleAddedMessage(message)); dispatch(setNewCollectibleAddedMessage(message));
@ -99,7 +96,7 @@ export default function AddCollectible() {
}; };
const validateAndSetTokenId = (val) => { const validateAndSetTokenId = (val) => {
setDisabled(!util.isValidHexAddress(address) || !val); setDisabled(!util.isValidHexAddress(address) || !val || isNaN(Number(val)));
setTokenId(val); setTokenId(val);
}; };
@ -149,7 +146,7 @@ export default function AddCollectible() {
)} )}
<Box margin={4}> <Box margin={4}>
<FormField <FormField
id="address" dataTestId="address"
titleText={t('address')} titleText={t('address')}
placeholder="0x..." placeholder="0x..."
value={address} value={address}
@ -161,7 +158,7 @@ export default function AddCollectible() {
autoFocus autoFocus
/> />
<FormField <FormField
id="token-id" dataTestId="token-id"
titleText={t('tokenId')} titleText={t('tokenId')}
placeholder={t('nftTokenIdPlaceholder')} placeholder={t('nftTokenIdPlaceholder')}
value={tokenId} value={tokenId}
@ -170,7 +167,6 @@ export default function AddCollectible() {
setCollectibleAddFailed(false); setCollectibleAddFailed(false);
}} }}
tooltipText={t('importNFTTokenIdToolTip')} tooltipText={t('importNFTTokenIdToolTip')}
numeric
/> />
</Box> </Box>
</Box> </Box>

View File

@ -0,0 +1,79 @@
import React from 'react';
import { fireEvent } from '@testing-library/react';
import configureMockStore from 'redux-mock-store';
import { renderWithProvider } from '../../../test/jest/rendering';
import * as Actions from '../../store/actions';
import AddCollectible from '.';
const VALID_ADDRESS = '0x312BE6a98441F9F6e3F6246B13CA19701e0AC3B9';
const INVALID_ADDRESS = 'aoinsafasdfa';
const VALID_TOKENID = '1201';
const INVALID_TOKENID = 'abcde';
describe('AddCollectible', () => {
const store = configureMockStore([])({
metamask: { provider: { chainId: '0x1' } },
});
it('should enable the "Add" button when valid entries are input into both Address and TokenId fields', () => {
const { getByTestId, getByText } = renderWithProvider(
<AddCollectible />,
store,
);
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', () => {
const { getByTestId, getByText } = renderWithProvider(
<AddCollectible />,
store,
);
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();
});
it('should call addCollectibleVerifyOwnership action with correct values (tokenId should not be in scientific notation)', () => {
const { getByTestId, getByText } = renderWithProvider(
<AddCollectible />,
store,
);
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 },
});
const addCollectibleVerifyOwnershipSpy = jest.spyOn(
Actions,
'addCollectibleVerifyOwnership',
);
fireEvent.click(getByText('Add'));
expect(addCollectibleVerifyOwnershipSpy).toHaveBeenCalledWith(
'0x312BE6a98441F9F6e3F6246B13CA19701e0AC3B9',
'9007199254740992',
);
});
});