-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
153 changed files
with
7,694 additions
and
3,310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
node_modules/ | ||
dist/ | ||
coverage/ | ||
.idea/ | ||
.DS_Store | ||
.vscode | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
packages/checkout/widgets-lib/src/lib/validations/widgetValidators.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { NATIVE } from '../constants'; | ||
import { isValidAddress, isValidAmount, isValidWalletProvider } from './widgetValidators'; | ||
|
||
describe('widget validators', () => { | ||
describe('Wallet Provider Validator', () => { | ||
it('should return true for "metamask"', () => { | ||
const result = isValidWalletProvider('metamask'); | ||
expect(result).toBeTruthy(); | ||
}); | ||
|
||
it('should return false for "METAMASK" (all uppercase)', () => { | ||
const result = isValidWalletProvider('METAMASK'); | ||
expect(result).toBeFalsy(); | ||
}); | ||
|
||
it('should return false for undefined', () => { | ||
const result = isValidWalletProvider(undefined); | ||
expect(result).toBeFalsy(); | ||
}); | ||
|
||
it('should return false for "metramask" ', () => { | ||
const result = isValidWalletProvider('metramask'); | ||
expect(result).toBeFalsy(); | ||
}); | ||
|
||
it('should return false for empty string', () => { | ||
const result = isValidWalletProvider(''); | ||
expect(result).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('Amount Validator', () => { | ||
const validCases = ['1', '1.0', '1.234567', '100000000', '']; // empty amount should pass as valid | ||
const invalidCases = ['acdas', '0.1234s', '1.2345678', undefined]; | ||
|
||
validCases.forEach((testCase) => { | ||
it(`should validate amount as a float with 6 decimal places for ${testCase}`, () => { | ||
const result = isValidAmount(testCase); | ||
expect(result).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
invalidCases.forEach((testCase) => { | ||
it(`should return false for any amount not a float with 6 decimal places for ${testCase}`, () => { | ||
const result = isValidAmount(testCase); | ||
expect(result).toBeFalsy(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Address Validator', () => { | ||
const IMX_L1_ADDRESS = '0xF57e7e7C23978C3cAEC3C3548E3D615c346e79fF'; | ||
const INVALID_ADDRESS = '0x1234567890'; | ||
|
||
it('should return true if address is valid', () => { | ||
const result = isValidAddress(IMX_L1_ADDRESS); | ||
expect(result).toBeTruthy(); | ||
}); | ||
|
||
it('should return true if address is valid and lowercase', () => { | ||
const result = isValidAddress(IMX_L1_ADDRESS.toLowerCase()); | ||
expect(result).toBeTruthy(); | ||
}); | ||
|
||
it('should return true if address is NATIVE string', () => { | ||
const result = isValidAddress(NATIVE); | ||
expect(result).toBeTruthy(); | ||
}); | ||
|
||
it('should return true if address is NATIVE string lowercased', () => { | ||
const result = isValidAddress(NATIVE.toLowerCase()); | ||
expect(result).toBeTruthy(); | ||
}); | ||
|
||
it('should return false if address is valid and uppercase', () => { | ||
const result = isValidAddress(IMX_L1_ADDRESS.toUpperCase()); | ||
expect(result).toBeFalsy(); | ||
}); | ||
|
||
it('should return false if address is not valid', () => { | ||
const result = isValidAddress(INVALID_ADDRESS); | ||
expect(result).toBeFalsy(); | ||
}); | ||
|
||
it('should return true if address empty', () => { | ||
const result = isValidAddress(''); | ||
expect(result).toBeTruthy(); | ||
}); | ||
|
||
it('should return false if address is undefined', () => { | ||
const result = isValidAddress(undefined); | ||
expect(result).toBeFalsy(); | ||
}); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
packages/checkout/widgets-lib/src/lib/validations/widgetValidators.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { WalletProviderName } from '@imtbl/checkout-sdk'; | ||
import { isAddress } from 'ethers/lib/utils'; | ||
import { amountInputValidation } from './amountInputValidations'; | ||
import { NATIVE } from '../constants'; | ||
|
||
export function isValidWalletProvider(walletProvider: string | undefined) { | ||
if (walletProvider === undefined) return false; | ||
if (walletProvider === '') return false; | ||
return Object.values(WalletProviderName).includes(walletProvider as WalletProviderName); | ||
} | ||
|
||
export function isValidAmount(amount: string | undefined) { | ||
if (amount === undefined) return false; | ||
if (amount === '') return true; // let empty string pass through | ||
if (!parseFloat(amount)) return false; | ||
if (Number.isNaN(parseFloat(amount))) return false; | ||
|
||
return amountInputValidation(amount); | ||
} | ||
|
||
export function isValidAddress(address: string | undefined) { | ||
if (address === undefined) return false; | ||
if (address === '') return true; | ||
if (address === NATIVE || address === NATIVE.toLowerCase()) return true; | ||
return isAddress(address); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.