Skip to content

Commit

Permalink
test: add tests for wallets-react
Browse files Browse the repository at this point in the history
  • Loading branch information
yeager-eren committed Oct 5, 2024
1 parent 7d51184 commit 88173e0
Show file tree
Hide file tree
Showing 8 changed files with 644 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
dist
*.js
scripts
scripts
vitest.config.ts
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@
"@typescript-eslint/parser": "^5.59.7",
"@vercel/routing-utils": "^3.1.0",
"@vitest/coverage-v8": "^2.0.5",
"@testing-library/react": "^16.0.0",
"@testing-library/dom": "^10.1.0",
"@testing-library/react-hooks": "^8.0.1",
"happy-dom": "^14.12.0",
"react-test-renderer": "^18.3.1",
"buffer": "^5.5.0",
"chalk": "^5.2.0",
"command-line-args": "^5.2.1",
Expand Down
4 changes: 2 additions & 2 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/// <reference types="vitest" />
import { defineConfig } from 'vite';
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
environment: 'happy-dom',
watch: false,
},
});
16 changes: 12 additions & 4 deletions wallets/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
"source": "./src/index.ts",
"main": "./dist/index.js",
"exports": {
".": "./dist/index.js"
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"typings": "dist/index.d.ts",
"files": [
"dist",
"src"
Expand All @@ -18,7 +20,9 @@
"ts-check": "tsc --declaration --emitDeclarationOnly -p ./tsconfig.json",
"clean": "rimraf dist",
"format": "prettier --write '{.,src}/**/*.{ts,tsx}'",
"lint": "eslint \"**/*.{ts,tsx}\" --ignore-path ../../.eslintignore"
"lint": "eslint \"**/*.{ts,tsx}\" --ignore-path ../../.eslintignore",
"test": "vitest",
"coverage": "vitest run --coverage"
},
"peerDependencies": {
"@types/react": "^17.0.0 || ^18.0.0",
Expand All @@ -30,7 +34,11 @@
"@rango-dev/wallets-shared": "^0.37.1-next.1",
"rango-types": "^0.1.69"
},
"devDependencies": {
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.25"
},
"publishConfig": {
"access": "public"
}
}
}
10 changes: 10 additions & 0 deletions wallets/react/src/test-utils/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const TEST_ENVIRONMENT_AGENT = 'HappyDOM/0.0.0';

/**
* Detecting wether our code is running in a test environment or not.
*
* Note: This is only useful when HappyDOM or jsdom has been added to test runner.
*/
export function isRunningInTestEnvironmentWithDom(): boolean {
return navigator.userAgent.includes(TEST_ENVIRONMENT_AGENT);
}
225 changes: 225 additions & 0 deletions wallets/react/src/test-utils/fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
import type { LegacyProviderInterface } from '@rango-dev/wallets-core/legacy';
import type { BlockchainMeta, GenericSigner } from 'rango-types';
import type { EvmTransaction } from 'rango-types/mainApi';

import { DefaultSignerFactory, TransactionType } from 'rango-types';

export const legacyAddress = '0x000000000000000000000000000000000000dead';

export class MockEvmSigner implements GenericSigner<EvmTransaction> {
async signMessage(
_msg: string,
_address: string,
_chainId: string | null
): Promise<string> {
return '0x';
}
async signAndSendTx(
_tx: EvmTransaction,
_address: string,
_chainId: string | null
): Promise<{ hash: string; response?: any }> {
return {
hash: '0x',
};
}
}

export const legacyProvider: LegacyProviderInterface = {
config: {
type: 'legacy-garbage',
},
async connect({ network }) {
// we added a timeout to simulate requesting to extension for connection.
return await new Promise((resolve, reject) => {
setTimeout(() => {
// This is useful for simulating error scenarios.
if (network === 'When Airdrop?') {
reject(
'please stay tuned we shall announce the detailed information soon.'
);
} else {
resolve([
{
accounts: [legacyAddress],
chainId: network || '',
},
]);
}
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
}, 10);
});
},
getInstance() {
return {};
},
async getSigners() {
const signers = new DefaultSignerFactory();
signers.registerSigner(TransactionType.EVM, new MockEvmSigner());
return signers;
},
getWalletInfo() {
return {
name: 'legacy garbage wallet',
color: '#000',
img: 'https://...',
installLink: 'https://...',
supportedChains: [],
};
},
};

export const blockchainsMeta: BlockchainMeta[] = [
{
name: 'BTC',
defaultDecimals: 8,
addressPatterns: [
'^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$|^(bc1)[0-9A-Za-z]{39,59}$',
],
feeAssets: [
{
blockchain: 'BTC',
symbol: 'BTC',
address: null,
},
],
logo: 'https://raw.githubusercontent.com/rango-exchange/assets/main/blockchains/BTC/icon.svg',
displayName: 'Bitcoin',
shortName: 'BTC',
sort: 12,
color: '#F7931A',
enabled: true,
type: TransactionType.TRANSFER,
chainId: null,
info: null,
},
{
name: 'ETH',
defaultDecimals: 18,
addressPatterns: ['^(0x)[0-9A-Fa-f]{40}$'],
feeAssets: [
{
blockchain: 'ETH',
symbol: 'ETH',
address: null,
},
],
logo: 'https://raw.githubusercontent.com/rango-exchange/assets/main/blockchains/ETH/icon.svg',
displayName: 'Ethereum',
shortName: 'ETH',
sort: 0,
color: '#ecf0f1',
enabled: true,
type: TransactionType.EVM,
chainId: '0x1',
info: {
infoType: 'EvmMetaInfo',
chainName: 'Ethereum Mainnet',
nativeCurrency: {
name: 'ETH',
symbol: 'ETH',
decimals: 18,
},
rpcUrls: ['https://rpc.ankr.com/eth'],
blockExplorerUrls: ['https://etherscan.io'],
addressUrl: 'https://etherscan.io/address/{wallet}',
transactionUrl: 'https://etherscan.io/tx/{txHash}',
// enableGasV2: true,
},
},
{
name: 'SOLANA',
defaultDecimals: 9,
addressPatterns: ['^[1-9A-HJ-NP-Za-km-z]{32,44}$'],
feeAssets: [
{
blockchain: 'SOLANA',
symbol: 'SOL',
address: null,
},
],
logo: 'https://raw.githubusercontent.com/rango-exchange/assets/main/blockchains/SOLANA/icon.svg',
displayName: 'Solana',
shortName: 'Solana',
sort: 20,
color: '#708DD2',
enabled: true,
type: TransactionType.SOLANA,
chainId: 'mainnet-beta',
info: null,
},
{
name: 'COSMOS',
defaultDecimals: 6,
addressPatterns: ['^(cosmos1)[0-9a-z]{38}$'],
feeAssets: [
{
blockchain: 'COSMOS',
symbol: 'ATOM',
address: null,
},
],
logo: 'https://raw.githubusercontent.com/rango-exchange/assets/main/blockchains/COSMOS/icon.svg',
displayName: 'Cosmos',
shortName: 'Cosmos',
sort: 15,
color: '#2E3148',
enabled: true,
type: TransactionType.COSMOS,
chainId: 'cosmoshub-4',
info: {
infoType: 'CosmosMetaInfo',
experimental: false,
rpc: 'https://cosmos-rpc.polkachu.com',
rest: 'https://lcd-cosmoshub.blockapsis.com',
cosmostationLcdUrl: 'https://lcd-cosmoshub.blockapsis.com',
cosmostationApiUrl: 'https://cosmos-rpc.polkachu.com',
cosmostationDenomTracePath: '/ibc/apps/transfer/v1/denom_traces/',
mintScanName: 'cosmos',
chainName: 'Cosmos',
stakeCurrency: {
coinDenom: 'ATOM',
coinMinimalDenom: 'uatom',
coinDecimals: 6,
coinGeckoId: 'cosmos',
coinImageUrl: '/tokens/blockchain/cosmos.svg',
},
bip44: {
coinType: 118,
},
bech32Config: {
bech32PrefixAccAddr: 'cosmos',
bech32PrefixAccPub: 'cosmospub',
bech32PrefixValAddr: 'cosmosvaloper',
bech32PrefixValPub: 'cosmosvaloperpub',
bech32PrefixConsAddr: 'cosmosvalcons',
bech32PrefixConsPub: 'cosmosvalconspub',
},
currencies: [
{
coinDenom: 'ATOM',
coinMinimalDenom: 'uatom',
coinDecimals: 6,
coinGeckoId: 'cosmos',
coinImageUrl: '/tokens/blockchain/cosmos.svg',
},
],
feeCurrencies: [
{
coinDenom: 'ATOM',
coinMinimalDenom: 'uatom',
coinDecimals: 6,
coinGeckoId: 'cosmos',
coinImageUrl: '/tokens/blockchain/cosmos.svg',
},
],
features: ['stargate', 'ibc-transfer'],
explorerUrlToTx: 'https://www.mintscan.io/cosmos/txs/{txHash}',
gasPriceStep: {
low: 0.01,
average: 0.025,
high: 0.04,
},
},
},
];
Loading

0 comments on commit 88173e0

Please sign in to comment.