2020-05-26 22:49:11 +02:00
|
|
|
import assert from 'assert'
|
2020-10-06 20:28:38 +02:00
|
|
|
import React from 'react'
|
2020-08-18 21:18:25 +02:00
|
|
|
import * as reactRedux from 'react-redux'
|
2020-05-26 22:49:11 +02:00
|
|
|
import { renderHook } from '@testing-library/react-hooks'
|
|
|
|
import sinon from 'sinon'
|
2020-10-06 20:28:38 +02:00
|
|
|
import { MemoryRouter } from 'react-router-dom'
|
2020-05-26 22:49:11 +02:00
|
|
|
import transactions from '../../../../test/data/transaction-data.json'
|
|
|
|
import { useTransactionDisplayData } from '../useTransactionDisplayData'
|
2020-06-12 19:37:06 +02:00
|
|
|
import * as useTokenFiatAmountHooks from '../useTokenFiatAmount'
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
getPreferences,
|
|
|
|
getShouldShowFiat,
|
|
|
|
getNativeCurrency,
|
|
|
|
getCurrentCurrency,
|
|
|
|
} from '../../selectors'
|
2020-05-29 02:43:44 +02:00
|
|
|
import { getTokens } from '../../ducks/metamask/metamask'
|
2020-05-26 22:49:11 +02:00
|
|
|
import * as i18nhooks from '../useI18nContext'
|
|
|
|
import { getMessage } from '../../helpers/utils/i18n-helper'
|
|
|
|
import messages from '../../../../app/_locales/en/messages.json'
|
2020-10-06 20:28:38 +02:00
|
|
|
import { ASSET_ROUTE, DEFAULT_ROUTE } from '../../helpers/constants/routes'
|
2020-11-07 08:38:12 +01:00
|
|
|
import {
|
|
|
|
TRANSACTION_CATEGORIES,
|
|
|
|
TRANSACTION_GROUP_CATEGORIES,
|
|
|
|
TRANSACTION_STATUSES,
|
|
|
|
} from '../../../../shared/constants/transaction'
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
const expectedResults = [
|
2020-08-19 18:27:05 +02:00
|
|
|
{
|
|
|
|
title: 'Send ETH',
|
2020-11-07 08:38:12 +01:00
|
|
|
category: TRANSACTION_GROUP_CATEGORIES.SEND,
|
2020-05-26 22:49:11 +02:00
|
|
|
subtitle: 'To: 0xffe5...1a97',
|
2020-06-17 18:38:15 +02:00
|
|
|
subtitleContainsOrigin: false,
|
2020-05-26 22:49:11 +02:00
|
|
|
date: 'May 12',
|
|
|
|
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
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Send ETH',
|
2020-11-07 08:38:12 +01:00
|
|
|
category: TRANSACTION_GROUP_CATEGORIES.SEND,
|
2020-05-26 22:49:11 +02:00
|
|
|
subtitle: 'To: 0x0ccc...8848',
|
2020-06-17 18:38:15 +02:00
|
|
|
subtitleContainsOrigin: false,
|
2020-05-26 22:49:11 +02:00
|
|
|
date: 'May 12',
|
|
|
|
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
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Send ETH',
|
2020-11-07 08:38:12 +01:00
|
|
|
category: TRANSACTION_GROUP_CATEGORIES.SEND,
|
2020-05-26 22:49:11 +02:00
|
|
|
subtitle: 'To: 0xffe5...1a97',
|
2020-06-17 18:38:15 +02:00
|
|
|
subtitleContainsOrigin: false,
|
2020-05-26 22:49:11 +02:00
|
|
|
date: 'May 12',
|
|
|
|
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,
|
2020-05-26 22:49:11 +02:00
|
|
|
subtitle: 'From: 0x31b9...4523',
|
2020-06-17 18:38:15 +02:00
|
|
|
subtitleContainsOrigin: false,
|
2020-05-26 22:49:11 +02:00
|
|
|
date: 'May 12',
|
|
|
|
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,
|
2020-05-26 22:49:11 +02:00
|
|
|
subtitle: 'From: 0x9eca...a149',
|
2020-06-17 18:38:15 +02:00
|
|
|
subtitleContainsOrigin: false,
|
2020-05-26 22:49:11 +02:00
|
|
|
date: 'May 8',
|
|
|
|
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,
|
2020-05-26 22:49:11 +02:00
|
|
|
subtitle: 'From: 0xee01...febb',
|
2020-06-17 18:38:15 +02:00
|
|
|
subtitleContainsOrigin: false,
|
2020-05-26 22:49:11 +02:00
|
|
|
date: 'May 24',
|
|
|
|
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',
|
2020-11-07 08:38:12 +01:00
|
|
|
category: TRANSACTION_CATEGORIES.SWAP,
|
2020-10-06 20:28:38 +02:00
|
|
|
subtitle: '',
|
|
|
|
subtitleContainsOrigin: false,
|
|
|
|
date: 'May 12',
|
|
|
|
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
|
|
|
},
|
2020-05-26 22:49:11 +02:00
|
|
|
]
|
|
|
|
|
2020-06-12 19:37:06 +02: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,
|
|
|
|
]
|
2020-11-07 08:38:12 +01:00
|
|
|
const wrapper = ({ children }) => (
|
|
|
|
<MemoryRouter initialEntries={initialEntries}>{children}</MemoryRouter>
|
|
|
|
)
|
2020-10-06 20:28:38 +02:00
|
|
|
return renderHook(cb, { wrapper })
|
|
|
|
}
|
|
|
|
|
2020-05-26 22:49:11 +02:00
|
|
|
describe('useTransactionDisplayData', function () {
|
|
|
|
before(function () {
|
|
|
|
useSelector = sinon.stub(reactRedux, 'useSelector')
|
2020-11-03 00:41:28 +01:00
|
|
|
useTokenFiatAmount = sinon.stub(
|
|
|
|
useTokenFiatAmountHooks,
|
|
|
|
'useTokenFiatAmount',
|
|
|
|
)
|
2020-06-12 19:37:06 +02:00
|
|
|
useTokenFiatAmount.returns((tokenAddress) => {
|
|
|
|
return tokenAddress ? '1 TST' : undefined
|
|
|
|
})
|
2020-05-26 22:49:11 +02:00
|
|
|
useI18nContext = sinon.stub(i18nhooks, 'useI18nContext')
|
2020-11-03 00:41:28 +01:00
|
|
|
useI18nContext.returns((key, variables) =>
|
|
|
|
getMessage('en', messages, key, variables),
|
|
|
|
)
|
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,
|
|
|
|
},
|
|
|
|
]
|
2020-05-26 22:49:11 +02:00
|
|
|
} else if (selector === getPreferences) {
|
|
|
|
return {
|
|
|
|
useNativeCurrencyAsPrimaryCurrency: true,
|
|
|
|
}
|
|
|
|
} else if (selector === getShouldShowFiat) {
|
|
|
|
return false
|
|
|
|
} else if (selector === getNativeCurrency) {
|
|
|
|
return 'ETH'
|
|
|
|
} else if (selector === getCurrentCurrency) {
|
|
|
|
return 'ETH'
|
|
|
|
}
|
2020-08-19 18:27:05 +02:00
|
|
|
return null
|
2020-05-26 22:49:11 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
transactions.forEach((transactionGroup, idx) => {
|
|
|
|
describe(`when called with group containing primaryTransaction id ${transactionGroup.primaryTransaction.id}`, function () {
|
|
|
|
const expected = expectedResults[idx]
|
2020-11-03 00:41:28 +01:00
|
|
|
const tokenAddress =
|
|
|
|
transactionGroup.primaryTransaction?.destinationTokenAddress
|
2020-05-26 22:49:11 +02:00
|
|
|
it(`should return a title of ${expected.title}`, function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHookWithRouter(
|
|
|
|
() => useTransactionDisplayData(transactionGroup),
|
|
|
|
tokenAddress,
|
|
|
|
)
|
2020-05-26 22:49:11 +02:00
|
|
|
assert.equal(result.current.title, expected.title)
|
|
|
|
})
|
|
|
|
it(`should return a subtitle of ${expected.subtitle}`, function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHookWithRouter(
|
|
|
|
() => useTransactionDisplayData(transactionGroup),
|
|
|
|
tokenAddress,
|
|
|
|
)
|
2020-05-26 22:49:11 +02:00
|
|
|
assert.equal(result.current.subtitle, expected.subtitle)
|
|
|
|
})
|
|
|
|
it(`should return a category of ${expected.category}`, function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHookWithRouter(
|
|
|
|
() => useTransactionDisplayData(transactionGroup),
|
|
|
|
tokenAddress,
|
|
|
|
)
|
2020-05-26 22:49:11 +02:00
|
|
|
assert.equal(result.current.category, expected.category)
|
|
|
|
})
|
|
|
|
it(`should return a primaryCurrency of ${expected.primaryCurrency}`, function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHookWithRouter(
|
|
|
|
() => useTransactionDisplayData(transactionGroup),
|
|
|
|
tokenAddress,
|
|
|
|
)
|
2020-05-26 22:49:11 +02:00
|
|
|
assert.equal(result.current.primaryCurrency, expected.primaryCurrency)
|
|
|
|
})
|
|
|
|
it(`should return a secondaryCurrency of ${expected.secondaryCurrency}`, function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHookWithRouter(
|
|
|
|
() => useTransactionDisplayData(transactionGroup),
|
|
|
|
tokenAddress,
|
|
|
|
)
|
|
|
|
assert.equal(
|
|
|
|
result.current.secondaryCurrency,
|
|
|
|
expected.secondaryCurrency,
|
|
|
|
)
|
2020-05-26 22:49:11 +02:00
|
|
|
})
|
2020-10-07 23:59:38 +02:00
|
|
|
it(`should return a displayedStatusKey of ${expected.displayedStatusKey}`, function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHookWithRouter(
|
|
|
|
() => useTransactionDisplayData(transactionGroup),
|
|
|
|
tokenAddress,
|
|
|
|
)
|
|
|
|
assert.equal(
|
|
|
|
result.current.displayedStatusKey,
|
|
|
|
expected.displayedStatusKey,
|
|
|
|
)
|
2020-05-26 22:49:11 +02:00
|
|
|
})
|
|
|
|
it(`should return a recipientAddress of ${expected.recipientAddress}`, function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHookWithRouter(
|
|
|
|
() => useTransactionDisplayData(transactionGroup),
|
|
|
|
tokenAddress,
|
|
|
|
)
|
2020-05-26 22:49:11 +02:00
|
|
|
assert.equal(result.current.recipientAddress, expected.recipientAddress)
|
|
|
|
})
|
|
|
|
it(`should return a senderAddress of ${expected.senderAddress}`, function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHookWithRouter(
|
|
|
|
() => useTransactionDisplayData(transactionGroup),
|
|
|
|
tokenAddress,
|
|
|
|
)
|
2020-05-26 22:49:11 +02:00
|
|
|
assert.equal(result.current.senderAddress, expected.senderAddress)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
it('should return an appropriate object', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHookWithRouter(() =>
|
|
|
|
useTransactionDisplayData(transactions[0]),
|
|
|
|
)
|
2020-05-26 22:49:11 +02:00
|
|
|
assert.deepEqual(result.current, expectedResults[0])
|
|
|
|
})
|
|
|
|
after(function () {
|
|
|
|
useSelector.restore()
|
|
|
|
useI18nContext.restore()
|
|
|
|
})
|
|
|
|
})
|