Skip to content

Commit

Permalink
[FEAT] scanner incomingData unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielbazan7 committed Sep 14, 2023
1 parent 6f3f5f9 commit 48bdaa8
Show file tree
Hide file tree
Showing 8 changed files with 1,239 additions and 30 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"ios:device": "export NODE_OPTIONS=--openssl-legacy-provider && react-native run-ios --device",
"ios:device:release": "export NODE_OPTIONS=--openssl-legacy-provider && react-native run-ios --configuration Release --device",
"start": "export NODE_OPTIONS=--openssl-legacy-provider && react-native start",
"test:coverage": "jest --coverage --testMatch='**/*.spec.{js,tsx}' --config='jest.config.js'",
"test:unit": "jest --watch --testMatch='**/*.spec.{js,tsx}' --config='jest.config.js'",
"test:coverage": "jest --coverage --testMatch='**/*.spec.{js,tsx,ts}' --config='jest.config.js'",
"test:unit": "jest --watch --testMatch='**/*.spec.{js,tsx,ts}' --config='jest.config.js'",
"test:cache": "jest --cache",
"prebuild:android:debug": "yarn set:dev",
"build:android:debug": "export NODE_OPTIONS=--openssl-legacy-provider && ./scripts/android-debug.sh",
Expand Down
4 changes: 2 additions & 2 deletions src/navigation/scan/screens/Scan.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import {RNCamera} from 'react-native-camera';
import styled from 'styled-components/native';
import ScanGuideSvg from '../../../../assets/img/qr-scan-guides.svg';
import {useDispatch} from 'react-redux';
import {incomingData} from '../../../store/scan/scan.effects';
import debounce from 'lodash.debounce';
import {useRoute} from '@react-navigation/native';
Expand All @@ -12,6 +11,7 @@ import {navigationRef} from '../../../Root';
import {AppActions} from '../../../store/app';
import {CustomErrorMessage} from '../../wallet/components/ErrorMessages';
import {useTranslation} from 'react-i18next';
import {useAppDispatch} from '@/utils/hooks';

const ScanContainer = styled.SafeAreaView`
flex: 1;
Expand All @@ -31,7 +31,7 @@ interface Props {

const Scan = () => {
const {t} = useTranslation();
const dispatch = useDispatch();
const dispatch = useAppDispatch();
const route = useRoute<RouteProp<ScanStackParamList, 'Root'>>();
const {onScanComplete} = route.params || {};

Expand Down
14 changes: 10 additions & 4 deletions src/navigation/wallet/screens/GlobalSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import React, {
} from 'react';
import styled from 'styled-components/native';
import {useAppDispatch, useAppSelector} from '../../../utils/hooks';
import {SUPPORTED_COINS, SUPPORTED_TOKENS} from '../../../constants/currencies';
import {
BitpaySupportedEvmCoins,
SUPPORTED_COINS,
SUPPORTED_TOKENS,
} from '../../../constants/currencies';
import {Wallet} from '../../../store/wallet/wallet.models';
import {
convertToFiat,
Expand Down Expand Up @@ -144,7 +148,7 @@ export type GlobalSelectParamList = {
sendMax?: boolean | undefined;
message?: string;
feePerKb?: number;
showERC20Tokens?: boolean;
showEVMWalletsAndTokens?: boolean;
};
};
amount?: number;
Expand Down Expand Up @@ -262,8 +266,10 @@ const GlobalSelect: React.FC<GlobalSelectProps> = ({
if (recipient.currency && recipient.chain) {
wallets = wallets.filter(
wallet =>
wallet.currencyAbbreviation === recipient?.currency &&
wallet.chain === recipient?.chain,
(wallet.currencyAbbreviation === recipient?.currency &&
wallet.chain === recipient?.chain) ||
(recipient?.opts?.showEVMWalletsAndTokens &&
BitpaySupportedEvmCoins[wallet.currencyAbbreviation]),
);
}
if (recipient?.network) {
Expand Down
8 changes: 7 additions & 1 deletion src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {DISABLE_DEVELOPMENT_LOGGING} from '@env';
import {
Action,
AnyAction,
Store,
applyMiddleware,
combineReducers,
legacy_createStore as createStore,
Expand Down Expand Up @@ -58,6 +60,7 @@ import {

import {Storage} from 'redux-persist';
import {MMKV} from 'react-native-mmkv';
import {AppDispatch} from '@/utils/hooks';

export const storage = new MMKV();

Expand Down Expand Up @@ -241,5 +244,8 @@ export function configureTestStore(initialState: any) {
trace: true,
traceLimit: 25,
})(applyMiddleware(...middlewares));
return createStore(rootReducer, initialState, middlewareEnhancers);
const store = createStore(rootReducer, initialState, middlewareEnhancers);
return store as Store<RootState, AnyAction> & {
dispatch: AppDispatch;
};
}
50 changes: 29 additions & 21 deletions src/store/scan/scan.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export const incomingData =
dispatch(handleBitPayUri(data, opts?.wallet));
// Import Private Key
} else if (IsValidImportPrivateKey(data)) {
goToImport(data);
dispatch(goToImport(data));
// Join multisig wallet
} else if (IsValidJoinCode(data)) {
dispatch(goToJoinWallet(data));
Expand Down Expand Up @@ -229,7 +229,7 @@ const goToPayPro =
const invoiceId = data.split('/i/')[1].split('?')[0];
const payProUrl = GetPayProUrl(data);
const {host} = new URL(payProUrl);

dispatch(LogActions.info('[scan] Incoming-data: Payment Protocol request'));
try {
dispatch(startOnGoingProcessModal('FETCHING_PAYMENT_INFO'));
const payProOptions = await dispatch(GetPayProOptions(payProUrl));
Expand Down Expand Up @@ -266,8 +266,6 @@ const goToPayPro =
});
} catch (e: any) {
dispatch(dismissOnGoingProcessModal());
await sleep(800);

dispatch(
showBottomNotificationModal({
type: 'warning',
Expand Down Expand Up @@ -476,9 +474,10 @@ const goToConfirm =
...recipient,
...{
opts: {
showERC20Tokens:
!!BitpaySupportedEvmCoins[recipient.currency.toLowerCase()], // no wallet selected - if ETH address show token wallets in next view
showEVMWalletsAndTokens:
!!BitpaySupportedEvmCoins[recipient.currency.toLowerCase()], // no wallet selected - if EVM address show all evm wallets and tokens in next view
message: opts?.message || '',
feePerKb: opts?.feePerKb,
},
},
},
Expand Down Expand Up @@ -556,7 +555,7 @@ export const goToAmount =
chain,
recipient,
wallet,
opts: urlOpts,
opts,
}: {
coin: string;
chain: string;
Expand Down Expand Up @@ -585,8 +584,10 @@ export const goToAmount =
...recipient,
...{
opts: {
showERC20Tokens:
!!BitpaySupportedEvmCoins[recipient.currency.toLowerCase()], // no wallet selected - if ETH address show token wallets in next view
showEVMWalletsAndTokens:
!!BitpaySupportedEvmCoins[recipient.currency.toLowerCase()], // no wallet selected - if EVM address show all evm wallets and tokens in next view
message: opts?.message || '',
feePerKb: opts?.feePerKb,
},
},
},
Expand All @@ -607,7 +608,7 @@ export const goToAmount =
amount: Number(amount),
wallet,
setButtonState,
opts: {...urlOpts, ...amountOpts},
opts: {...opts, ...amountOpts},
}),
);
},
Expand Down Expand Up @@ -755,7 +756,7 @@ const handleBitcoinCashUriLegacyAddress =
dispatch => {
dispatch(
LogActions.info(
'[scan] Incoming-data: Bitcoin Cash URI with legacy address',
'[scan] Incoming-data: BitcoinCash URI with legacy address',
),
);
const coin = 'bch';
Expand Down Expand Up @@ -899,7 +900,7 @@ const handleRippleUri =
currency: coin,
chain,
address,
destinationTag: Number(destinationTag),
destinationTag: destinationTag ? Number(destinationTag) : undefined,
};
if (!amountParam.exec(data)) {
dispatch(goToAmount({coin, chain, recipient, wallet}));
Expand Down Expand Up @@ -1340,7 +1341,7 @@ const handlePlainAddress =
dispatch(LogActions.info(`[scan] Incoming-data: ${coin} plain address`));
const network = Object.keys(bitcoreLibs).includes(coin)
? GetAddressNetwork(address, coin as keyof BitcoreLibs)
: undefined; // There is no way to tell if an eth address is goerli or livenet so let's skip the network filter
: undefined; // There is no way to tell if an evm address is goerli or livenet so let's skip the network filter
const recipient = {
type: opts?.context || 'address',
name: opts?.name,
Expand All @@ -1354,14 +1355,21 @@ const handlePlainAddress =
dispatch(goToAmount({coin, chain, recipient, wallet: opts?.wallet}));
};

const goToImport = (importQrCodeData: string): void => {
navigationRef.navigate('Wallet', {
screen: WalletScreens.IMPORT,
params: {
importQrCodeData,
},
});
};
const goToImport =
(importQrCodeData: string): Effect<void> =>
(dispatch, getState) => {
dispatch(
LogActions.info(
'[scan] Incoming-data (redirect): QR code export feature',
),
);
navigationRef.navigate('Wallet', {
screen: WalletScreens.IMPORT,
params: {
importQrCodeData,
},
});
};

const goToJoinWallet =
(data: string): Effect<void> =>
Expand Down
Loading

0 comments on commit 48bdaa8

Please sign in to comment.