Skip to content

Commit

Permalink
test: utils
Browse files Browse the repository at this point in the history
  • Loading branch information
lissavxo committed May 1, 2024
1 parent 035a405 commit bf722dc
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 26 deletions.
17 changes: 17 additions & 0 deletions react/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,21 @@ module.exports = {
collectCoverageFrom: [
'lib/**/*.{ts,tsx,mjs}',
],
coveragePathIgnorePatterns: [
"src/*",
"lib/themes",
"lib/tests",
"lib/assets"
],
coverageThreshold: {
"global": {
"lines": 80
}
},
transformIgnorePatterns: [
'node_modules/(?!(@babylonjs/core|@babylonjs/loaders)).+.[t|j]sx?$',
],
moduleNameMapper: {
"^@/(.*)$": "<rootDir>"
}
};
15 changes: 11 additions & 4 deletions react/lib/components/Widget/WidgetContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useState } from 'react';
import React, { useCallback, useEffect, useState, useMemo } from 'react';

import {
isCrypto,
Expand All @@ -17,7 +17,8 @@ import {

import Widget, { WidgetProps } from './Widget';
import { useCustomSnackbar, withSnackbar } from '../../hooks/toast';
import { useSoundEffects } from '../../hooks/sound';

import successSound from '../../assets/success.mp3.json';

export interface WidgetContainerProps
extends Omit<WidgetProps, 'success' | 'setNewTxs' | 'setCurrencyObject'> {
Expand Down Expand Up @@ -76,12 +77,17 @@ export const WidgetContainer: React.FunctionComponent<WidgetContainerProps> =

const [thisPrice, setThisPrice] = useState(0);
const showSnackbar = useCustomSnackbar();
const { playSuccessSound } = useSoundEffects();

const [success, setSuccess] = useState(false);

const [newTxs, setNewTxs] = useState<Transaction[] | undefined>();
const addrType = getCurrencyTypeFromAddress(to);

const txSound = useMemo(
(): HTMLAudioElement => new Audio(successSound.base64),
[],
);

if (
!isValidCurrency(currency) ||
(isCrypto(currency) && addrType !== currency)
Expand All @@ -104,7 +110,7 @@ export const WidgetContainer: React.FunctionComponent<WidgetContainerProps> =
shouldTriggerOnSuccess(transaction, paymentId, cryptoAmount, opReturn)
) {
if (sound) {
playSuccessSound();
txSound.play().catch(() => {});
}
if (!hideToasts) {
const toastSuccessText = successText ? successText + ' | ' : '';
Expand All @@ -131,6 +137,7 @@ export const WidgetContainer: React.FunctionComponent<WidgetContainerProps> =
to,
paymentId,
opReturn,
txSound
],
);

Expand Down
15 changes: 0 additions & 15 deletions react/lib/hooks/sound.ts

This file was deleted.

95 changes: 95 additions & 0 deletions react/lib/tests/util/api-client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { shouldTriggerOnSuccess } from '../../util/api-client';
import { Transaction } from '../../util';

jest.mock('axios');

global.fetch = jest.fn();

describe('API Client Util Tests', () => {
describe('shouldTriggerOnSuccess', () => {
it('should return true when all conditions match', () => {
const transaction:Transaction = {
amount: '100.00',
paymentId: '123',
message: 'test message',
hash: '',
timestamp: 0,
address: ''
};
expect(shouldTriggerOnSuccess(transaction, '123', '100.00', 'test message')).toBe(true);

});

it('should return false when the payment ID does not match', () => {
const transaction:Transaction = {
amount: '100.00',
paymentId: '123',
message: 'test opReturn message',
hash: '',
timestamp: 0,
address: ''
};
expect(shouldTriggerOnSuccess(transaction, '999', '100.00', 'test opReturn message')).toBe(false);
});

it('should return false when crypto amounts do not match', () => {
const transaction:Transaction = {
amount: '101.00',
paymentId: '123',
message: 'test opReturn message',
hash: '',
timestamp: 0,
address: ''
};
expect(shouldTriggerOnSuccess(transaction, '123', '100.00', 'test opReturn message')).toBe(false);
}); // todo - check if amount higher than the one requested is also valid

it('should return false when OpReturn data does not match', () => {
const transaction:Transaction = {
amount: '101.00',
paymentId: '123',
message: 'test opReturn message',
hash: '',
timestamp: 0,
address: ''
};
expect(shouldTriggerOnSuccess(transaction, '123', '100.00', 'test opReturn')).toBe(false);
});

it('should ignore amount validation when amount is not set', () => {
const transaction:Transaction = {
amount: '101.00',
paymentId: '123',
message: 'test opReturn',
hash: '',
timestamp: 0,
address: ''
};
expect(shouldTriggerOnSuccess(transaction, '123', undefined, 'test opReturn')).toBe(true);
});

it('should ignore paymentId validation when paymentId does not exists', () => {
const transaction:Transaction = {
amount: '101.00',
paymentId: '123',
message: 'test opReturn',
hash: '',
timestamp: 0,
address: ''
};
expect(shouldTriggerOnSuccess(transaction, undefined, '101.00', 'test opReturn')).toBe(true);
});

it('should ignore opReturn validation when opReturn does not exists', () => {
const transaction:Transaction = {
amount: '101.00',
paymentId: '123',
message: 'test opReturn',
hash: '',
timestamp: 0,
address: ''
};
expect(shouldTriggerOnSuccess(transaction, '123', '101.00', undefined)).toBe(true);
});
});
});
60 changes: 60 additions & 0 deletions react/lib/tests/util/currency.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { isCrypto, isValidCurrency } from '../../util';

describe('API Client Util Tests', () => {
describe('isCrypto', () => {
it('recognizes valid crypto currencies', () => {
expect(isCrypto('BCH')).toBeTruthy();
expect(isCrypto('XEC')).toBeTruthy();
});

it('does not recognize invalid crypto currencies', () => {
expect(isCrypto('BTC')).toBeFalsy();
expect(isCrypto('ETH')).toBeFalsy();
});

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

it('returns false for empty strings', () => {
expect(isCrypto('')).toBeFalsy();
});

it('handles whitespace or special characters', () => {
expect(isCrypto(' BCH ')).toBeFalsy();
expect(isCrypto('BCH@')).toBeFalsy();
});
});

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

it('identifies fiat currencies correctly', () => {
expect(isValidCurrency('USD')).toBeTruthy();
expect(isValidCurrency('EUR')).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('is case-sensitive', () => {
expect(isValidCurrency('bch')).toBeFalsy();
expect(isValidCurrency('usd')).toBeFalsy();
});

it('handles strings with spaces or special characters', () => {
expect(isValidCurrency(' BCH ')).toBeFalsy();
expect(isValidCurrency('USD@')).toBeFalsy();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
exportedForTesting,
encodeOpReturnProps,
EncodeOpReturnParams,
} from '../util/opReturn';
} from '../../util/opReturn';

const {
stringToHex,
Expand Down
10 changes: 4 additions & 6 deletions react/lib/util/opReturn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,17 @@ export interface EncodeOpReturnParams {
export function encodeOpReturnProps({
opReturn,
disablePaymentId,
paymentId,
paymentId
}: EncodeOpReturnParams): string {
if (opReturn === undefined) {
opReturn = '';
}

const dataPushdata = getDataPushdata(opReturn, disablePaymentId);
if (paymentId === undefined && !disablePaymentId) {
paymentId = '';
if (paymentId === undefined || disablePaymentId) {
paymentId = ''
}
const pushDataPrefixedPaymentId = prependPaymentIdWithPushdata(
paymentId ?? '',
);
const pushDataPrefixedPaymentId = prependPaymentIdWithPushdata(paymentId ?? '');
return (
OP_RETURN_PREFIX_PUSHDATA +
OP_RETURN_PREFIX +
Expand Down

0 comments on commit bf722dc

Please sign in to comment.