Skip to content

Commit

Permalink
Merge pull request #405 from PayButton/refactor/react-utils
Browse files Browse the repository at this point in the history
refactor: add index utils
  • Loading branch information
Klakurka authored Jun 3, 2024
2 parents 48f69bb + 21d2808 commit 675a2d2
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 82 deletions.
20 changes: 13 additions & 7 deletions react/lib/components/PayButton/PayButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ import React, { useState, useEffect, useCallback, useRef } from 'react';

import { Theme, ThemeName, ThemeProvider, useTheme } from '../../themes';
import Button, { ButtonProps } from '../Button/Button';
import { getFiatPrice } from '../../util/api-client';
import { PaymentDialog } from '../PaymentDialog/PaymentDialog';
import { getCurrencyTypeFromAddress, isValidCashAddress, isValidXecAddress } from '../../util/address';
import { getCurrencyObject } from '../../util/satoshis';
import { generatePaymentId } from '../../util/opReturn';
import { Currency, Transaction, CurrencyObject } from '../../util/types';
import { isFiat } from '../../util/currency';

import {
Transaction,
Currency,
isFiat,
getFiatPrice,
getCurrencyTypeFromAddress,
isValidCashAddress,
isValidXecAddress,
CurrencyObject,
generatePaymentId,
getCurrencyObject
} from '../../util';
import { PaymentDialog } from '../PaymentDialog';
export interface PayButtonProps extends ButtonProps {
to: string;
amount?: number | string;
Expand Down
3 changes: 1 addition & 2 deletions react/lib/components/PaymentDialog/PaymentDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import React, { useState, useEffect } from 'react';
import { Theme, ThemeName, ThemeProvider, useTheme } from '../../themes';
import Button, { ButtonProps } from '../Button/Button';
import { WidgetContainer } from '../Widget/WidgetContainer';
import { isValidCashAddress, isValidXecAddress } from '../../util/address';
import { Currency, CurrencyObject, Transaction } from '../../util/types';
import { Currency, CurrencyObject, Transaction, isValidCashAddress, isValidXecAddress } from '../../util';

export interface PaymentDialogProps extends ButtonProps {
to: string;
Expand Down
33 changes: 18 additions & 15 deletions react/lib/components/Widget/Widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,30 @@ import {
import React, { useEffect, useMemo, useState } from 'react';
import copy from 'copy-to-clipboard';
import QRCode, { BaseQRCodeProps } from 'qrcode.react';
import io, { Socket } from 'socket.io-client';
import PencilIcon from '../../assets/edit-pencil';
import config from '../../config.json';

import { Theme, ThemeName, ThemeProvider, useTheme } from '../../themes';
import { Button, animation } from '../Button/Button';
import BarChart from '../BarChart/BarChart';
import {
Currency,
getAddressBalance,
getAddressDetails,
isFiat,
Transaction,
getCashtabProviderStatus,
CryptoCurrency,
DECIMALS,
CurrencyObject,
getCurrencyObject,
formatPrice,
setListener,
encodeOpReturnProps,
isValidCashAddress,
isValidXecAddress,
getCurrencyTypeFromAddress,
} from '../../util/address';
import { formatPrice } from '../../util/format';
import { DECIMALS } from '../../util/constants';
import { Button, animation } from '../Button/Button';
import BarChart from '../BarChart/BarChart';

import PencilIcon from '../../assets/edit-pencil';
import io, { Socket } from 'socket.io-client';
import { encodeOpReturnProps } from '../../util/opReturn';
import { getAddressDetails, getAddressBalance, getCashtabProviderStatus } from '../../util/api-client';
import { getCurrencyObject } from '../../util/satoshis';
import { Currency, CurrencyObject, Transaction, CryptoCurrency } from '../../util/types';
import { setListener } from '../../util/socket';
import { isFiat } from '../../util/currency';
} from '../../util';

type QRCodeProps = BaseQRCodeProps & { renderAs: 'svg' };

Expand Down
20 changes: 12 additions & 8 deletions react/lib/components/Widget/WidgetContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ import { OptionsObject, SnackbarProvider, useSnackbar } from 'notistack';
import React, { useCallback, useEffect, useMemo, useState } from 'react';

import successSound from '../../assets/success.mp3.json';
import {
getCurrencyTypeFromAddress,
} from '../../util/address';

import {
getFiatPrice,
} from '../../util/api-client';
Currency,
CurrencyObject,
Transaction,
generatePaymentId,
getCurrencyTypeFromAddress,
isCrypto,
isFiat,
isGreaterThanZero,
isValidCurrency,
resolveNumber
} from '../../util';

import Widget, { WidgetProps } from './Widget';
import { generatePaymentId } from '../../util/opReturn';
import { Currency, CurrencyObject, Transaction } from '../../util/types';
import { isGreaterThanZero, resolveNumber } from '../../util/number';
import { isCrypto, isFiat, isValidCurrency } from '../../util/currency';

export interface WidgetContainerProps
extends Omit<WidgetProps, 'success' | 'setNewTxs' | 'setCurrencyObject'> {
Expand Down
80 changes: 40 additions & 40 deletions react/lib/tests/util/currency.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,52 +27,52 @@ describe('isCrypto', () => {
});
});

describe('isValidCurrency', () => {
beforeEach(() => {
jest.resetAllMocks();
});
describe('isValidCurrency', () => {
beforeEach(() => {
jest.resetAllMocks();
});

it('identifies fiat currencies correctly', () => {
expect(isValidCurrency('USD')).toBeTruthy();
expect(isValidCurrency('CAD')).toBeTruthy();
expect(isValidCurrency('EUR')).toBeFalsy();
expect(isValidCurrency('GBP')).toBeFalsy();
expect(isValidCurrency('AUD')).toBeFalsy();
});
it('identifies fiat currencies correctly', () => {
expect(isValidCurrency('USD')).toBeTruthy();
expect(isValidCurrency('CAD')).toBeTruthy();
expect(isValidCurrency('EUR')).toBeFalsy();
expect(isValidCurrency('GBP')).toBeFalsy();
expect(isValidCurrency('AUD')).toBeFalsy();
});

it('identifies crypto currencies correctly', () => {
expect(isValidCurrency('BCH')).toBeTruthy();
expect(isValidCurrency('XEC')).toBeTruthy();
});
it('identifies crypto currencies correctly', () => {
expect(isValidCurrency('BCH')).toBeTruthy();
expect(isValidCurrency('XEC')).toBeTruthy();
});

it('returns false for unrecognized currencies', () => {
expect(isValidCurrency('XYZ')).toBeFalsy();
expect(isValidCurrency('ABC')).toBeFalsy();
});
it('returns false for unrecognized currencies', () => {
expect(isValidCurrency('XYZ')).toBeFalsy();
expect(isValidCurrency('ABC')).toBeFalsy();
});

it('is case-sensitive', () => {
expect(isValidCurrency('bch')).toBeFalsy();
expect(isValidCurrency('usd')).toBeFalsy();
expect(isValidCurrency('xec')).toBeFalsy();
});
it('is case-sensitive', () => {
expect(isValidCurrency('bch')).toBeFalsy();
expect(isValidCurrency('usd')).toBeFalsy();
expect(isValidCurrency('xec')).toBeFalsy();
});

it('returns false for spaces or special characters', () => {
expect(isValidCurrency(' BCH ')).toBeFalsy();
expect(isValidCurrency('USD@')).toBeFalsy();
});
it('returns false for spaces or special characters', () => {
expect(isValidCurrency(' BCH ')).toBeFalsy();
expect(isValidCurrency('USD@')).toBeFalsy();
});
});

describe('isFiat', () => {
test('returns true for valid fiat currencies', () => {
FIAT_CURRENCIES.forEach(currency => {
expect(isFiat(currency)).toBe(true);
});
describe('isFiat', () => {
test('returns true for valid fiat currencies', () => {
FIAT_CURRENCIES.forEach(currency => {
expect(isFiat(currency)).toBe(true);
});

test('returns false for invalid fiat currencies', () => {
const invalidCurrencies = ['INR', 'JPY', 'BTC', 'ETH', 'XYZ', 'EUR'];
invalidCurrencies.forEach(currency => {
expect(isFiat(currency)).toBe(false);
});
});

test('returns false for invalid fiat currencies', () => {
const invalidCurrencies = ['INR', 'JPY', 'BTC', 'ETH', 'XYZ', 'EUR'];
invalidCurrencies.forEach(currency => {
expect(isFiat(currency)).toBe(false);
});
});
});
});
1 change: 0 additions & 1 deletion react/lib/util/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,3 @@ export const getCashtabProviderStatus = () => {
}
return false;
};

2 changes: 1 addition & 1 deletion react/lib/util/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const FIAT_CURRENCIES = ['USD', 'CAD' ];
export const FIAT_CURRENCIES = ['USD', 'CAD'];

export const CRYPTO_CURRENCIES = ['BCH', 'XEC'];

Expand Down
12 changes: 12 additions & 0 deletions react/lib/util/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

export * from './address';
export * from './api-client';
export * from './constants';
export * from './format';
export * from './opReturn';
export * from './randomizeSats';
export * from './satoshis';
export * from './socket';
export * from './types';
export * from './number';
export * from './currency';
2 changes: 1 addition & 1 deletion react/lib/util/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export const resolveNumber = (value: BigNumber.Value) => {

export const isGreaterThanZero = (value: BigNumber.Value) => {
return zero.isLessThan(value)
}
}
11 changes: 4 additions & 7 deletions react/lib/util/satoshis.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { formatPrice, formatBCH, formatXEC } from './format';
import { DECIMALS } from './constants'


import { DECIMALS } from './constants';
import { isCrypto } from './currency';
import { randomizeSatoshis } from './randomizeSats';
import { Currency, CurrencyObject } from './types';
import { formatBCH, formatPrice, formatXEC } from './format';
import { resolveNumber } from './number';
import randomizeSatoshis from './randomizeSats';
import { Currency, CurrencyObject } from './types';


export const getCurrencyObject = (
Expand Down Expand Up @@ -43,7 +41,6 @@ export const getCurrencyObject = (
};
};


export default {
getCurrencyObject,
};

0 comments on commit 675a2d2

Please sign in to comment.