You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
260 lines
8.9 KiB
260 lines
8.9 KiB
4 years ago
|
import assert from 'assert';
|
||
|
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';
|
||
4 years ago
|
import transactions from '../../../test/data/transaction-data.json';
|
||
4 years ago
|
import {
|
||
|
getPreferences,
|
||
|
getShouldShowFiat,
|
||
|
getNativeCurrency,
|
||
|
getCurrentCurrency,
|
||
4 years ago
|
} from '../selectors';
|
||
|
import { getTokens } from '../ducks/metamask/metamask';
|
||
|
import { getMessage } from '../helpers/utils/i18n-helper';
|
||
|
import messages from '../../../app/_locales/en/messages.json';
|
||
|
import { ASSET_ROUTE, DEFAULT_ROUTE } from '../helpers/constants/routes';
|
||
4 years ago
|
import {
|
||
4 years ago
|
TRANSACTION_TYPES,
|
||
4 years ago
|
TRANSACTION_GROUP_CATEGORIES,
|
||
|
TRANSACTION_STATUSES,
|
||
4 years ago
|
} from '../../../shared/constants/transaction';
|
||
|
import * as i18nhooks from './useI18nContext';
|
||
|
import * as useTokenFiatAmountHooks from './useTokenFiatAmount';
|
||
|
import { useTransactionDisplayData } from './useTransactionDisplayData';
|
||
5 years ago
|
|
||
|
const expectedResults = [
|
||
4 years ago
|
{
|
||
|
title: 'Send ETH',
|
||
4 years ago
|
category: TRANSACTION_GROUP_CATEGORIES.SEND,
|
||
5 years ago
|
subtitle: 'To: 0xffe5...1a97',
|
||
5 years ago
|
subtitleContainsOrigin: false,
|
||
4 years ago
|
date: 'May 12, 2020',
|
||
5 years ago
|
primaryCurrency: '-1 ETH',
|
||
|
senderAddress: '0x9eca64466f257793eaa52fcfff5066894b76a149',
|
||
|
recipientAddress: '0xffe5bc4e8f1f969934d773fa67da095d2e491a97',
|
||
|
secondaryCurrency: '-1 ETH',
|
||
|
isPending: false,
|
||
4 years ago
|
displayedStatusKey: TRANSACTION_STATUSES.CONFIRMED,
|
||
4 years ago
|
isSubmitted: false,
|
||
4 years ago
|
},
|
||
|
{
|
||
|
title: 'Send ETH',
|
||
4 years ago
|
category: TRANSACTION_GROUP_CATEGORIES.SEND,
|
||
5 years ago
|
subtitle: 'To: 0x0ccc...8848',
|
||
5 years ago
|
subtitleContainsOrigin: false,
|
||
4 years ago
|
date: 'May 12, 2020',
|
||
5 years ago
|
primaryCurrency: '-2 ETH',
|
||
|
senderAddress: '0x9eca64466f257793eaa52fcfff5066894b76a149',
|
||
|
recipientAddress: '0x0ccc8aeeaf5ce790f3b448325981a143fdef8848',
|
||
|
secondaryCurrency: '-2 ETH',
|
||
|
isPending: false,
|
||
4 years ago
|
displayedStatusKey: TRANSACTION_STATUSES.CONFIRMED,
|
||
4 years ago
|
},
|
||
|
{
|
||
|
title: 'Send ETH',
|
||
4 years ago
|
category: TRANSACTION_GROUP_CATEGORIES.SEND,
|
||
5 years ago
|
subtitle: 'To: 0xffe5...1a97',
|
||
5 years ago
|
subtitleContainsOrigin: false,
|
||
4 years ago
|
date: 'May 12, 2020',
|
||
5 years ago
|
primaryCurrency: '-2 ETH',
|
||
|
senderAddress: '0x9eca64466f257793eaa52fcfff5066894b76a149',
|
||
|
recipientAddress: '0xffe5bc4e8f1f969934d773fa67da095d2e491a97',
|
||
|
secondaryCurrency: '-2 ETH',
|
||
|
isPending: false,
|
||
4 years ago
|
displayedStatusKey: TRANSACTION_STATUSES.CONFIRMED,
|
||
4 years ago
|
},
|
||
|
{
|
||
|
title: 'Receive',
|
||
4 years ago
|
category: TRANSACTION_GROUP_CATEGORIES.RECEIVE,
|
||
5 years ago
|
subtitle: 'From: 0x31b9...4523',
|
||
5 years ago
|
subtitleContainsOrigin: false,
|
||
4 years ago
|
date: 'May 12, 2020',
|
||
5 years ago
|
primaryCurrency: '18.75 ETH',
|
||
|
senderAddress: '0x31b98d14007bdee637298086988a0bbd31184523',
|
||
|
recipientAddress: '0x9eca64466f257793eaa52fcfff5066894b76a149',
|
||
|
secondaryCurrency: '18.75 ETH',
|
||
|
isPending: false,
|
||
4 years ago
|
displayedStatusKey: TRANSACTION_STATUSES.CONFIRMED,
|
||
4 years ago
|
},
|
||
|
{
|
||
|
title: 'Receive',
|
||
4 years ago
|
category: TRANSACTION_GROUP_CATEGORIES.RECEIVE,
|
||
5 years ago
|
subtitle: 'From: 0x9eca...a149',
|
||
5 years ago
|
subtitleContainsOrigin: false,
|
||
4 years ago
|
date: 'May 8, 2020',
|
||
5 years ago
|
primaryCurrency: '0 ETH',
|
||
|
senderAddress: '0x9eca64466f257793eaa52fcfff5066894b76a149',
|
||
|
recipientAddress: '0x9eca64466f257793eaa52fcfff5066894b76a149',
|
||
|
secondaryCurrency: '0 ETH',
|
||
|
isPending: false,
|
||
4 years ago
|
displayedStatusKey: TRANSACTION_STATUSES.CONFIRMED,
|
||
4 years ago
|
},
|
||
|
{
|
||
|
title: 'Receive',
|
||
4 years ago
|
category: TRANSACTION_GROUP_CATEGORIES.RECEIVE,
|
||
5 years ago
|
subtitle: 'From: 0xee01...febb',
|
||
5 years ago
|
subtitleContainsOrigin: false,
|
||
4 years ago
|
date: 'May 24, 2020',
|
||
5 years ago
|
primaryCurrency: '1 ETH',
|
||
|
senderAddress: '0xee014609ef9e09776ac5fe00bdbfef57bcdefebb',
|
||
|
recipientAddress: '0x9eca64466f257793eaa52fcfff5066894b76a149',
|
||
|
secondaryCurrency: '1 ETH',
|
||
|
isPending: false,
|
||
4 years ago
|
displayedStatusKey: TRANSACTION_STATUSES.CONFIRMED,
|
||
4 years ago
|
},
|
||
4 years ago
|
{
|
||
|
title: 'Swap ETH to ABC',
|
||
4 years ago
|
category: TRANSACTION_TYPES.SWAP,
|
||
4 years ago
|
subtitle: '',
|
||
|
subtitleContainsOrigin: false,
|
||
4 years ago
|
date: 'May 12, 2020',
|
||
4 years ago
|
primaryCurrency: '+1 ABC',
|
||
|
senderAddress: '0xee014609ef9e09776ac5fe00bdbfef57bcdefebb',
|
||
|
recipientAddress: '0xabca64466f257793eaa52fcfff5066894b76a149',
|
||
|
secondaryCurrency: undefined,
|
||
|
isPending: false,
|
||
4 years ago
|
displayedStatusKey: TRANSACTION_STATUSES.CONFIRMED,
|
||
4 years ago
|
},
|
||
4 years ago
|
];
|
||
5 years ago
|
|
||
4 years ago
|
let useSelector, useI18nContext, useTokenFiatAmount;
|
||
5 years ago
|
|
||
4 years ago
|
const renderHookWithRouter = (cb, tokenAddress) => {
|
||
4 years ago
|
const initialEntries = [
|
||
|
tokenAddress ? `${ASSET_ROUTE}/${tokenAddress}` : DEFAULT_ROUTE,
|
||
4 years ago
|
];
|
||
4 years ago
|
const wrapper = ({ children }) => (
|
||
|
<MemoryRouter initialEntries={initialEntries}>{children}</MemoryRouter>
|
||
4 years ago
|
);
|
||
|
return renderHook(cb, { wrapper });
|
||
|
};
|
||
4 years ago
|
|
||
5 years ago
|
describe('useTransactionDisplayData', function () {
|
||
|
before(function () {
|
||
4 years ago
|
useSelector = sinon.stub(reactRedux, 'useSelector');
|
||
4 years ago
|
useTokenFiatAmount = sinon.stub(
|
||
|
useTokenFiatAmountHooks,
|
||
|
'useTokenFiatAmount',
|
||
4 years ago
|
);
|
||
5 years ago
|
useTokenFiatAmount.returns((tokenAddress) => {
|
||
4 years ago
|
return tokenAddress ? '1 TST' : undefined;
|
||
|
});
|
||
|
useI18nContext = sinon.stub(i18nhooks, 'useI18nContext');
|
||
4 years ago
|
useI18nContext.returns((key, variables) =>
|
||
|
getMessage('en', messages, key, variables),
|
||
4 years ago
|
);
|
||
5 years ago
|
useSelector.callsFake((selector) => {
|
||
5 years ago
|
if (selector === getTokens) {
|
||
4 years ago
|
return [
|
||
|
{
|
||
|
address: '0xabca64466f257793eaa52fcfff5066894b76a149',
|
||
|
symbol: 'ABC',
|
||
|
decimals: 18,
|
||
|
},
|
||
4 years ago
|
];
|
||
5 years ago
|
} else if (selector === getPreferences) {
|
||
|
return {
|
||
|
useNativeCurrencyAsPrimaryCurrency: true,
|
||
4 years ago
|
};
|
||
5 years ago
|
} else if (selector === getShouldShowFiat) {
|
||
4 years ago
|
return false;
|
||
5 years ago
|
} else if (selector === getNativeCurrency) {
|
||
4 years ago
|
return 'ETH';
|
||
5 years ago
|
} else if (selector === getCurrentCurrency) {
|
||
4 years ago
|
return 'ETH';
|
||
5 years ago
|
}
|
||
4 years ago
|
return null;
|
||
|
});
|
||
|
});
|
||
5 years ago
|
transactions.forEach((transactionGroup, idx) => {
|
||
|
describe(`when called with group containing primaryTransaction id ${transactionGroup.primaryTransaction.id}`, function () {
|
||
4 years ago
|
const expected = expectedResults[idx];
|
||
4 years ago
|
const tokenAddress =
|
||
4 years ago
|
transactionGroup.primaryTransaction?.destinationTokenAddress;
|
||
5 years ago
|
it(`should return a title of ${expected.title}`, function () {
|
||
4 years ago
|
const { result } = renderHookWithRouter(
|
||
|
() => useTransactionDisplayData(transactionGroup),
|
||
|
tokenAddress,
|
||
4 years ago
|
);
|
||
|
assert.strictEqual(result.current.title, expected.title);
|
||
|
});
|
||
5 years ago
|
it(`should return a subtitle of ${expected.subtitle}`, function () {
|
||
4 years ago
|
const { result } = renderHookWithRouter(
|
||
|
() => useTransactionDisplayData(transactionGroup),
|
||
|
tokenAddress,
|
||
4 years ago
|
);
|
||
|
assert.strictEqual(result.current.subtitle, expected.subtitle);
|
||
|
});
|
||
5 years ago
|
it(`should return a category of ${expected.category}`, function () {
|
||
4 years ago
|
const { result } = renderHookWithRouter(
|
||
|
() => useTransactionDisplayData(transactionGroup),
|
||
|
tokenAddress,
|
||
4 years ago
|
);
|
||
|
assert.strictEqual(result.current.category, expected.category);
|
||
|
});
|
||
5 years ago
|
it(`should return a primaryCurrency of ${expected.primaryCurrency}`, function () {
|
||
4 years ago
|
const { result } = renderHookWithRouter(
|
||
|
() => useTransactionDisplayData(transactionGroup),
|
||
|
tokenAddress,
|
||
4 years ago
|
);
|
||
4 years ago
|
assert.strictEqual(
|
||
|
result.current.primaryCurrency,
|
||
|
expected.primaryCurrency,
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
it(`should return a secondaryCurrency of ${expected.secondaryCurrency}`, function () {
|
||
4 years ago
|
const { result } = renderHookWithRouter(
|
||
|
() => useTransactionDisplayData(transactionGroup),
|
||
|
tokenAddress,
|
||
4 years ago
|
);
|
||
4 years ago
|
assert.strictEqual(
|
||
4 years ago
|
result.current.secondaryCurrency,
|
||
|
expected.secondaryCurrency,
|
||
4 years ago
|
);
|
||
|
});
|
||
4 years ago
|
it(`should return a displayedStatusKey of ${expected.displayedStatusKey}`, function () {
|
||
4 years ago
|
const { result } = renderHookWithRouter(
|
||
|
() => useTransactionDisplayData(transactionGroup),
|
||
|
tokenAddress,
|
||
4 years ago
|
);
|
||
4 years ago
|
assert.strictEqual(
|
||
4 years ago
|
result.current.displayedStatusKey,
|
||
|
expected.displayedStatusKey,
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
it(`should return a recipientAddress of ${expected.recipientAddress}`, function () {
|
||
4 years ago
|
const { result } = renderHookWithRouter(
|
||
|
() => useTransactionDisplayData(transactionGroup),
|
||
|
tokenAddress,
|
||
4 years ago
|
);
|
||
4 years ago
|
assert.strictEqual(
|
||
|
result.current.recipientAddress,
|
||
|
expected.recipientAddress,
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
it(`should return a senderAddress of ${expected.senderAddress}`, function () {
|
||
4 years ago
|
const { result } = renderHookWithRouter(
|
||
|
() => useTransactionDisplayData(transactionGroup),
|
||
|
tokenAddress,
|
||
4 years ago
|
);
|
||
|
assert.strictEqual(
|
||
|
result.current.senderAddress,
|
||
|
expected.senderAddress,
|
||
|
);
|
||
|
});
|
||
|
});
|
||
|
});
|
||
5 years ago
|
it('should return an appropriate object', function () {
|
||
4 years ago
|
const { result } = renderHookWithRouter(() =>
|
||
|
useTransactionDisplayData(transactions[0]),
|
||
4 years ago
|
);
|
||
|
assert.deepStrictEqual(result.current, expectedResults[0]);
|
||
|
});
|
||
5 years ago
|
after(function () {
|
||
4 years ago
|
useSelector.restore();
|
||
|
useI18nContext.restore();
|
||
|
});
|
||
|
});
|