2021-02-04 19:15:23 +01:00
|
|
|
import React from 'react';
|
|
|
|
import * as reactRedux from 'react-redux';
|
|
|
|
import { renderHook } from '@testing-library/react-hooks';
|
|
|
|
import sinon from 'sinon';
|
|
|
|
import { MemoryRouter } from 'react-router-dom';
|
2021-04-28 21:53:59 +02:00
|
|
|
import transactions from '../../test/data/transaction-data.json';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
getPreferences,
|
|
|
|
getShouldShowFiat,
|
|
|
|
getCurrentCurrency,
|
2021-03-18 11:20:06 +01:00
|
|
|
getCurrentChainId,
|
2021-03-16 22:00:08 +01:00
|
|
|
} from '../selectors';
|
2021-06-08 18:03:59 +02:00
|
|
|
import { getTokens, getNativeCurrency } from '../ducks/metamask/metamask';
|
2021-03-16 22:00:08 +01:00
|
|
|
import { getMessage } from '../helpers/utils/i18n-helper';
|
2021-04-28 21:53:59 +02:00
|
|
|
import messages from '../../app/_locales/en/messages.json';
|
2021-03-16 22:00:08 +01:00
|
|
|
import { ASSET_ROUTE, DEFAULT_ROUTE } from '../helpers/constants/routes';
|
2021-04-28 21:53:59 +02:00
|
|
|
import { MAINNET_CHAIN_ID } from '../../shared/constants/network';
|
2020-11-07 08:38:12 +01:00
|
|
|
import {
|
2021-03-10 21:16:44 +01:00
|
|
|
TRANSACTION_TYPES,
|
2020-11-07 08:38:12 +01:00
|
|
|
TRANSACTION_GROUP_CATEGORIES,
|
|
|
|
TRANSACTION_STATUSES,
|
2021-04-28 21:53:59 +02:00
|
|
|
} from '../../shared/constants/transaction';
|
2021-03-16 22:00:08 +01:00
|
|
|
import * as i18nhooks from './useI18nContext';
|
|
|
|
import * as useTokenFiatAmountHooks from './useTokenFiatAmount';
|
|
|
|
import { useTransactionDisplayData } from './useTransactionDisplayData';
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
const expectedResults = [
|
2020-08-19 18:27:05 +02:00
|
|
|
{
|
2021-03-19 18:12:11 +01:00
|
|
|
title: 'Send',
|
2020-11-07 08:38:12 +01:00
|
|
|
category: TRANSACTION_GROUP_CATEGORIES.SEND,
|
2021-10-13 19:54:48 +02:00
|
|
|
subtitle: 'To: 0xffe...1a97',
|
2020-06-17 18:38:15 +02:00
|
|
|
subtitleContainsOrigin: false,
|
2021-01-04 18:45:54 +01:00
|
|
|
date: 'May 12, 2020',
|
2020-05-26 22:49:11 +02:00
|
|
|
primaryCurrency: '-1 ETH',
|
|
|
|
senderAddress: '0x9eca64466f257793eaa52fcfff5066894b76a149',
|
|
|
|
recipientAddress: '0xffe5bc4e8f1f969934d773fa67da095d2e491a97',
|
|
|
|
secondaryCurrency: '-1 ETH',
|
|
|
|
isPending: false,
|
2020-11-07 08:38:12 +01:00
|
|
|
displayedStatusKey: TRANSACTION_STATUSES.CONFIRMED,
|
2020-10-19 16:45:21 +02:00
|
|
|
isSubmitted: false,
|
2020-08-19 18:27:05 +02:00
|
|
|
},
|
|
|
|
{
|
2021-03-19 18:12:11 +01:00
|
|
|
title: 'Send',
|
2020-11-07 08:38:12 +01:00
|
|
|
category: TRANSACTION_GROUP_CATEGORIES.SEND,
|
2021-10-13 19:54:48 +02:00
|
|
|
subtitle: 'To: 0x0cc...8848',
|
2020-06-17 18:38:15 +02:00
|
|
|
subtitleContainsOrigin: false,
|
2021-01-04 18:45:54 +01:00
|
|
|
date: 'May 12, 2020',
|
2020-05-26 22:49:11 +02:00
|
|
|
primaryCurrency: '-2 ETH',
|
|
|
|
senderAddress: '0x9eca64466f257793eaa52fcfff5066894b76a149',
|
|
|
|
recipientAddress: '0x0ccc8aeeaf5ce790f3b448325981a143fdef8848',
|
|
|
|
secondaryCurrency: '-2 ETH',
|
|
|
|
isPending: false,
|
2020-11-07 08:38:12 +01:00
|
|
|
displayedStatusKey: TRANSACTION_STATUSES.CONFIRMED,
|
2020-08-19 18:27:05 +02:00
|
|
|
},
|
|
|
|
{
|
2021-03-19 18:12:11 +01:00
|
|
|
title: 'Send',
|
2020-11-07 08:38:12 +01:00
|
|
|
category: TRANSACTION_GROUP_CATEGORIES.SEND,
|
2021-10-13 19:54:48 +02:00
|
|
|
subtitle: 'To: 0xffe...1a97',
|
2020-06-17 18:38:15 +02:00
|
|
|
subtitleContainsOrigin: false,
|
2021-01-04 18:45:54 +01:00
|
|
|
date: 'May 12, 2020',
|
2020-05-26 22:49:11 +02:00
|
|
|
primaryCurrency: '-2 ETH',
|
|
|
|
senderAddress: '0x9eca64466f257793eaa52fcfff5066894b76a149',
|
|
|
|
recipientAddress: '0xffe5bc4e8f1f969934d773fa67da095d2e491a97',
|
|
|
|
secondaryCurrency: '-2 ETH',
|
|
|
|
isPending: false,
|
2020-11-07 08:38:12 +01:00
|
|
|
displayedStatusKey: TRANSACTION_STATUSES.CONFIRMED,
|
2020-08-19 18:27:05 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Receive',
|
2020-11-07 08:38:12 +01:00
|
|
|
category: TRANSACTION_GROUP_CATEGORIES.RECEIVE,
|
2021-10-13 19:54:48 +02:00
|
|
|
subtitle: 'From: 0x31b...4523',
|
2020-06-17 18:38:15 +02:00
|
|
|
subtitleContainsOrigin: false,
|
2021-01-04 18:45:54 +01:00
|
|
|
date: 'May 12, 2020',
|
2020-05-26 22:49:11 +02:00
|
|
|
primaryCurrency: '18.75 ETH',
|
|
|
|
senderAddress: '0x31b98d14007bdee637298086988a0bbd31184523',
|
|
|
|
recipientAddress: '0x9eca64466f257793eaa52fcfff5066894b76a149',
|
|
|
|
secondaryCurrency: '18.75 ETH',
|
|
|
|
isPending: false,
|
2020-11-07 08:38:12 +01:00
|
|
|
displayedStatusKey: TRANSACTION_STATUSES.CONFIRMED,
|
2020-08-19 18:27:05 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Receive',
|
2020-11-07 08:38:12 +01:00
|
|
|
category: TRANSACTION_GROUP_CATEGORIES.RECEIVE,
|
2021-10-13 19:54:48 +02:00
|
|
|
subtitle: 'From: 0x9ec...a149',
|
2020-06-17 18:38:15 +02:00
|
|
|
subtitleContainsOrigin: false,
|
2021-01-04 18:45:54 +01:00
|
|
|
date: 'May 8, 2020',
|
2020-05-26 22:49:11 +02:00
|
|
|
primaryCurrency: '0 ETH',
|
|
|
|
senderAddress: '0x9eca64466f257793eaa52fcfff5066894b76a149',
|
|
|
|
recipientAddress: '0x9eca64466f257793eaa52fcfff5066894b76a149',
|
|
|
|
secondaryCurrency: '0 ETH',
|
|
|
|
isPending: false,
|
2020-11-07 08:38:12 +01:00
|
|
|
displayedStatusKey: TRANSACTION_STATUSES.CONFIRMED,
|
2020-08-19 18:27:05 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Receive',
|
2020-11-07 08:38:12 +01:00
|
|
|
category: TRANSACTION_GROUP_CATEGORIES.RECEIVE,
|
2021-10-13 19:54:48 +02:00
|
|
|
subtitle: 'From: 0xee0...febb',
|
2020-06-17 18:38:15 +02:00
|
|
|
subtitleContainsOrigin: false,
|
2021-01-04 18:45:54 +01:00
|
|
|
date: 'May 24, 2020',
|
2020-05-26 22:49:11 +02:00
|
|
|
primaryCurrency: '1 ETH',
|
|
|
|
senderAddress: '0xee014609ef9e09776ac5fe00bdbfef57bcdefebb',
|
|
|
|
recipientAddress: '0x9eca64466f257793eaa52fcfff5066894b76a149',
|
|
|
|
secondaryCurrency: '1 ETH',
|
|
|
|
isPending: false,
|
2020-11-07 08:38:12 +01:00
|
|
|
displayedStatusKey: TRANSACTION_STATUSES.CONFIRMED,
|
2020-08-19 18:27:05 +02:00
|
|
|
},
|
2020-10-06 20:28:38 +02:00
|
|
|
{
|
|
|
|
title: 'Swap ETH to ABC',
|
2021-03-10 21:16:44 +01:00
|
|
|
category: TRANSACTION_TYPES.SWAP,
|
2020-10-06 20:28:38 +02:00
|
|
|
subtitle: '',
|
|
|
|
subtitleContainsOrigin: false,
|
2021-01-04 18:45:54 +01:00
|
|
|
date: 'May 12, 2020',
|
2020-10-06 20:28:38 +02:00
|
|
|
primaryCurrency: '+1 ABC',
|
|
|
|
senderAddress: '0xee014609ef9e09776ac5fe00bdbfef57bcdefebb',
|
|
|
|
recipientAddress: '0xabca64466f257793eaa52fcfff5066894b76a149',
|
|
|
|
secondaryCurrency: undefined,
|
|
|
|
isPending: false,
|
2020-11-07 08:38:12 +01:00
|
|
|
displayedStatusKey: TRANSACTION_STATUSES.CONFIRMED,
|
2020-10-06 20:28:38 +02:00
|
|
|
},
|
2022-05-02 23:49:01 +02:00
|
|
|
{
|
|
|
|
title: 'Contract Deployment',
|
|
|
|
category: TRANSACTION_GROUP_CATEGORIES.INTERACTION,
|
|
|
|
subtitle: 'metamask.github.io',
|
|
|
|
subtitleContainsOrigin: true,
|
|
|
|
date: 'May 12, 2020',
|
|
|
|
primaryCurrency: '-0 ETH',
|
|
|
|
senderAddress: '0xee014609ef9e09776ac5fe00bdbfef57bcdefebb',
|
|
|
|
recipientAddress: undefined,
|
|
|
|
secondaryCurrency: '-0 ETH',
|
|
|
|
isPending: false,
|
|
|
|
displayedStatusKey: TRANSACTION_STATUSES.CONFIRMED,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let useSelector, useI18nContext, useTokenFiatAmount;
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2020-10-06 20:28:38 +02:00
|
|
|
const renderHookWithRouter = (cb, tokenAddress) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const initialEntries = [
|
|
|
|
tokenAddress ? `${ASSET_ROUTE}/${tokenAddress}` : DEFAULT_ROUTE,
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
2020-11-07 08:38:12 +01:00
|
|
|
const wrapper = ({ children }) => (
|
|
|
|
<MemoryRouter initialEntries={initialEntries}>{children}</MemoryRouter>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
return renderHook(cb, { wrapper });
|
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('useTransactionDisplayData', () => {
|
2021-07-23 01:13:40 +02:00
|
|
|
const dispatch = sinon.spy();
|
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
beforeAll(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
useSelector = sinon.stub(reactRedux, 'useSelector');
|
2020-11-03 00:41:28 +01:00
|
|
|
useTokenFiatAmount = sinon.stub(
|
|
|
|
useTokenFiatAmountHooks,
|
|
|
|
'useTokenFiatAmount',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-06-12 19:37:06 +02:00
|
|
|
useTokenFiatAmount.returns((tokenAddress) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return tokenAddress ? '1 TST' : undefined;
|
|
|
|
});
|
|
|
|
useI18nContext = sinon.stub(i18nhooks, 'useI18nContext');
|
2020-11-03 00:41:28 +01:00
|
|
|
useI18nContext.returns((key, variables) =>
|
|
|
|
getMessage('en', messages, key, variables),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-05-26 22:49:11 +02:00
|
|
|
useSelector.callsFake((selector) => {
|
2020-05-29 02:43:44 +02:00
|
|
|
if (selector === getTokens) {
|
2020-11-03 00:41:28 +01:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
address: '0xabca64466f257793eaa52fcfff5066894b76a149',
|
|
|
|
symbol: 'ABC',
|
|
|
|
decimals: 18,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
2020-05-26 22:49:11 +02:00
|
|
|
} else if (selector === getPreferences) {
|
|
|
|
return {
|
|
|
|
useNativeCurrencyAsPrimaryCurrency: true,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-05-26 22:49:11 +02:00
|
|
|
} else if (selector === getShouldShowFiat) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return false;
|
2020-05-26 22:49:11 +02:00
|
|
|
} else if (selector === getNativeCurrency) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return 'ETH';
|
2020-05-26 22:49:11 +02:00
|
|
|
} else if (selector === getCurrentCurrency) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return 'ETH';
|
2021-03-18 11:20:06 +01:00
|
|
|
} else if (selector === getCurrentChainId) {
|
|
|
|
return MAINNET_CHAIN_ID;
|
2020-05-26 22:49:11 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return null;
|
|
|
|
});
|
2021-07-23 01:13:40 +02:00
|
|
|
sinon.stub(reactRedux, 'useDispatch').returns(dispatch);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
2020-05-26 22:49:11 +02:00
|
|
|
transactions.forEach((transactionGroup, idx) => {
|
2021-04-15 20:01:46 +02:00
|
|
|
describe(`when called with group containing primaryTransaction id ${transactionGroup.primaryTransaction.id}`, () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const expected = expectedResults[idx];
|
2020-11-03 00:41:28 +01:00
|
|
|
const tokenAddress =
|
2021-02-04 19:15:23 +01:00
|
|
|
transactionGroup.primaryTransaction?.destinationTokenAddress;
|
2021-04-15 20:01:46 +02:00
|
|
|
it(`should return a title of ${expected.title}`, () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHookWithRouter(
|
|
|
|
() => useTransactionDisplayData(transactionGroup),
|
|
|
|
tokenAddress,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(result.current.title).toStrictEqual(expected.title);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
it(`should return a subtitle of ${expected.subtitle}`, () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHookWithRouter(
|
|
|
|
() => useTransactionDisplayData(transactionGroup),
|
|
|
|
tokenAddress,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(result.current.subtitle).toStrictEqual(expected.subtitle);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
it(`should return a category of ${expected.category}`, () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHookWithRouter(
|
|
|
|
() => useTransactionDisplayData(transactionGroup),
|
|
|
|
tokenAddress,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(result.current.category).toStrictEqual(expected.category);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
it(`should return a primaryCurrency of ${expected.primaryCurrency}`, () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHookWithRouter(
|
|
|
|
() => useTransactionDisplayData(transactionGroup),
|
|
|
|
tokenAddress,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(result.current.primaryCurrency).toStrictEqual(
|
2020-12-03 16:46:22 +01:00
|
|
|
expected.primaryCurrency,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
it(`should return a secondaryCurrency of ${expected.secondaryCurrency}`, () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHookWithRouter(
|
|
|
|
() => useTransactionDisplayData(transactionGroup),
|
|
|
|
tokenAddress,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(result.current.secondaryCurrency).toStrictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
expected.secondaryCurrency,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
it(`should return a displayedStatusKey of ${expected.displayedStatusKey}`, () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHookWithRouter(
|
|
|
|
() => useTransactionDisplayData(transactionGroup),
|
|
|
|
tokenAddress,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(result.current.displayedStatusKey).toStrictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
expected.displayedStatusKey,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
it(`should return a recipientAddress of ${expected.recipientAddress}`, () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHookWithRouter(
|
|
|
|
() => useTransactionDisplayData(transactionGroup),
|
|
|
|
tokenAddress,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(result.current.recipientAddress).toStrictEqual(
|
2020-12-03 16:46:22 +01:00
|
|
|
expected.recipientAddress,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
it(`should return a senderAddress of ${expected.senderAddress}`, () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHookWithRouter(
|
|
|
|
() => useTransactionDisplayData(transactionGroup),
|
|
|
|
tokenAddress,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(result.current.senderAddress).toStrictEqual(
|
2021-02-04 19:15:23 +01:00
|
|
|
expected.senderAddress,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should return an appropriate object', () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHookWithRouter(() =>
|
|
|
|
useTransactionDisplayData(transactions[0]),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(result.current).toStrictEqual(expectedResults[0]);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|