diff --git a/web/cashtab/src/components/Configure/Configure.js b/web/cashtab/src/components/Configure/Configure.js --- a/web/cashtab/src/components/Configure/Configure.js +++ b/web/cashtab/src/components/Configure/Configure.js @@ -54,7 +54,10 @@ import ApiError from 'components/Common/ApiError'; import CopyToClipboard from 'components/Common/CopyToClipboard'; import { formatSavedBalance } from 'utils/formatting'; -import { isValidXecAddress } from 'utils/validation'; +import { + isValidXecAddress, + isValidNewWalletNameLength, +} from 'utils/validation'; import { convertToEcashPrefix } from 'utils/cashMethods'; import useWindowDimensions from 'hooks/useWindowDimensions'; import { isMobile, isIOS, isSafari } from 'react-device-detect'; @@ -751,10 +754,7 @@ }; const changeWalletName = async () => { - if ( - newWalletName === '' || - newWalletName.length > currency.localStorageMaxCharacters - ) { + if (!isValidNewWalletNameLength(newWalletName)) { setNewWalletNameIsValid(false); return; } @@ -818,11 +818,7 @@ const handleWalletNameInput = e => { const { value } = e.target; // validation - if ( - value && - value.length && - value.length <= currency.localStorageMaxCharacters - ) { + if (value && isValidNewWalletNameLength(value)) { setNewWalletNameIsValid(true); } else { setNewWalletNameIsValid(false); diff --git a/web/cashtab/src/utils/__tests__/validation.test.js b/web/cashtab/src/utils/__tests__/validation.test.js --- a/web/cashtab/src/utils/__tests__/validation.test.js +++ b/web/cashtab/src/utils/__tests__/validation.test.js @@ -9,6 +9,7 @@ isValidTokenStats, isValidCashtabSettings, isValidXecAddress, + isValidNewWalletNameLength, isValidEtokenAddress, isValidXecSendAmount, isValidSendToMany, @@ -695,4 +696,20 @@ sendModal: true, autoCameraOn: true, })); + it(`accepts a valid wallet name`, () => { + expect(isValidNewWalletNameLength('Apollo')).toBe(true); + }); + it(`rejects wallet name that is too long`, () => { + expect( + isValidNewWalletNameLength( + 'this string is far too long to be used as a wallet name...', + ), + ).toBe(false); + }); + it(`rejects blank string as new wallet name`, () => { + expect(isValidNewWalletNameLength('')).toBe(false); + }); + it(`rejects wallet name of the wrong type`, () => { + expect(isValidNewWalletNameLength(['newWalletName'])).toBe(false); + }); }); diff --git a/web/cashtab/src/utils/validation.js b/web/cashtab/src/utils/validation.js --- a/web/cashtab/src/utils/validation.js +++ b/web/cashtab/src/utils/validation.js @@ -394,6 +394,15 @@ ); }; +export const isValidNewWalletNameLength = newWalletName => { + return ( + typeof newWalletName === 'string' && + newWalletName.length > 0 && + newWalletName.length <= currency.localStorageMaxCharacters && + newWalletName.length !== '' + ); +}; + export const isValidXecAirdrop = xecAirdrop => { return ( typeof xecAirdrop === 'string' &&