diff --git a/src/__tests__/utils/helpers.test.tsx b/src/__tests__/utils/helpers.test.tsx index 3f82652..127402d 100644 --- a/src/__tests__/utils/helpers.test.tsx +++ b/src/__tests__/utils/helpers.test.tsx @@ -1,6 +1,8 @@ import Transaction from '../../models/Transaction'; import { canProcessSloCode, + checkDomainValue, + checkEmailValue, extractParamsFromUri, logOutBody, prepareConfig, @@ -316,3 +318,80 @@ describe('helpers#canProcessSloCode/2', () => { ).toBeTruthy(); }); }); + +describe('helper#checkEmaiValue/1', () => { + it('returns false if empty string value', () => { + expect(() => checkEmailValue('')).toThrowError( + 'Please provide non blank string for email' + ); + }); + it('returns false if wrong string value', () => { + expect(() => checkEmailValue('azerty')).toThrowError( + 'Please provide valid email' + ); + }); + + it('returns false if @ string value', () => { + expect(() => checkEmailValue('@')).toThrowError( + 'Please provide valid email' + ); + }); + + it('returns true if simple string value', () => { + expect(checkEmailValue('john@example.com')).toEqual('john@example.com'); + }); + + it('returns true if dotted email string value', () => { + expect(checkEmailValue('john.doe@example.com')).toEqual( + 'john.doe@example.com' + ); + }); + + it('returns false if ending first part with dots email string value', () => { + expect(() => checkEmailValue('wrong..@example.com')).toThrowError( + 'Please provide valid email' + ); + }); + + it('returns false if alias email string value', () => { + expect(() => checkEmailValue('john+alias@example.com')).toThrowError( + 'Please provide valid email' + ); + }); + + it('returns truthy if tiret email string value', () => { + expect(checkEmailValue('john-doe@example.com')).toEqual( + 'john-doe@example.com' + ); + }); + + it('returns truthy if underscored email string value', () => { + expect(checkEmailValue('john-doe@example.com')).toEqual( + 'john-doe@example.com' + ); + }); +}); + +describe('helpers#checkDomainValue/1', () => { + it('should throw error if empy input', () => { + expect(() => checkDomainValue(' ')).toThrowError( + 'Please provide non blank string for domain' + ); + }); + + it('should throw error if underscored input', () => { + expect(() => checkDomainValue('_')).toThrowError( + 'Please provide valid domain (alphanumeric dashed separated)' + ); + }); + + it('should throw error if standard account complex name input', () => { + expect(() => checkDomainValue("My awesome Company's name")).toThrowError( + 'Please provide valid domain (alphanumeric dashed separated)' + ); + }); + + it('should return value if proper domain input', () => { + expect(checkDomainValue('communitiz-app')).toEqual('communitiz-app'); + }); +}); diff --git a/src/components/CryptrGatewayButton.tsx b/src/components/CryptrGatewayButton.tsx index d4fb07d..2990880 100644 --- a/src/components/CryptrGatewayButton.tsx +++ b/src/components/CryptrGatewayButton.tsx @@ -3,26 +3,7 @@ import { Locale, useCryptr } from '..'; import { Pressable, Text, View } from 'react-native'; import type { StyleProp, TextStyle, ViewStyle } from 'react-native'; import { defaultStyles } from '../utils/defaultStypes'; - -const checkEmailValue = (emailValue: string) => { - if (emailValue.trim() === '') { - throw new Error('Please provide non blank string for email'); - } - if (!/^[a-zA-Z0-9+]+@(?:[a-zA-Z0-9-]+\.)+[A-Za-z]+$/.test(emailValue)) { - throw new Error('Please provide valid email'); - } - return emailValue; -}; - -const checkDomainValue = (domainValue?: string) => { - if (domainValue !== undefined && domainValue?.trim() !== '') { - if (!/^[a-z0-9-]*$/.test(domainValue)) { - throw new Error( - 'Please provide valid domain (alphanumeric dashed separated)' - ); - } - } -}; +import { checkDomainValue, checkEmailValue } from '../utils/helpers'; type GatewayProps = { domain?: string; diff --git a/src/utils/helpers.tsx b/src/utils/helpers.tsx index 6fe1104..0ef9e8b 100644 --- a/src/utils/helpers.tsx +++ b/src/utils/helpers.tsx @@ -157,3 +157,27 @@ export const canProcessSloCode = ( // ips !== 'google' ); }; + +export const checkEmailValue = (emailValue: string) => { + if (emailValue.trim() === '') { + throw new Error('Please provide non blank string for email'); + } + if ( + !/^[a-z0-9]+[-_.]?[a-z0-9]+@(?:[a-zA-Z0-9-]+\.)+[A-Za-z]+$/.test(emailValue) + ) { + throw new Error('Please provide valid email'); + } + return emailValue; +}; + +export const checkDomainValue = (domainValue?: string) => { + if (domainValue === undefined || domainValue.trim() === '') { + throw new Error('Please provide non blank string for domain'); + } + if (!/^[a-z0-9-]*$/.test(domainValue)) { + throw new Error( + 'Please provide valid domain (alphanumeric dashed separated)' + ); + } + return domainValue; +};