2020-01-09 04:34:58 +01:00
import assert from 'assert'
import sinon from 'sinon'
import proxyquire from 'proxyquire'
2020-10-30 15:17:36 +01:00
import nock from 'nock'
import { cloneDeep } from 'lodash'
2020-01-09 04:34:58 +01:00
2020-10-30 15:17:36 +01:00
import waitUntilCalled from '../../../lib/wait-until-called'
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
import {
GOERLI ,
KOVAN ,
MAINNET ,
2020-10-31 01:58:12 +01:00
MAINNET _CHAIN _ID ,
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
RINKEBY ,
ROPSTEN ,
2020-10-31 01:58:12 +01:00
ROPSTEN _CHAIN _ID ,
ROPSTEN _NETWORK _ID ,
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
} from '../../../../app/scripts/controllers/network/enums'
2020-11-07 08:38:12 +01:00
import {
TRANSACTION _CATEGORIES ,
TRANSACTION _STATUSES ,
} from '../../../../shared/constants/transaction'
2020-08-19 18:27:05 +02:00
2020-11-03 00:41:28 +01:00
const IncomingTransactionsController = proxyquire (
'../../../../app/scripts/controllers/incoming-transactions' ,
{
'../lib/random-id' : { default : ( ) => 54321 } ,
} ,
) . default
2019-08-16 20:54:10 +02:00
2020-10-31 01:58:12 +01:00
const FAKE _CHAIN _ID = '0x1338'
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
const MOCK _SELECTED _ADDRESS = '0x0101'
2020-11-03 22:16:28 +01:00
const SET _STATE _TIMEOUT = 10
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
2020-11-03 00:41:28 +01:00
function getEmptyInitState ( ) {
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
return {
2019-08-16 20:54:10 +02:00
incomingTransactions : { } ,
incomingTxLastFetchedBlocksByNetwork : {
2019-09-18 18:30:29 +02:00
[ GOERLI ] : null ,
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
[ KOVAN ] : null ,
2019-08-16 20:54:10 +02:00
[ MAINNET ] : null ,
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
[ RINKEBY ] : null ,
[ ROPSTEN ] : null ,
2019-08-16 20:54:10 +02:00
} ,
}
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
}
2019-08-16 20:54:10 +02:00
2020-11-03 00:41:28 +01:00
function getNonEmptyInitState ( ) {
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
return {
2019-08-16 20:54:10 +02:00
incomingTransactions : {
'0x123456' : { id : 777 } ,
} ,
incomingTxLastFetchedBlocksByNetwork : {
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
[ GOERLI ] : 1 ,
[ KOVAN ] : 2 ,
[ MAINNET ] : 3 ,
[ RINKEBY ] : 5 ,
[ ROPSTEN ] : 4 ,
2019-08-16 20:54:10 +02:00
} ,
}
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
}
2019-08-16 20:54:10 +02:00
2020-11-03 00:41:28 +01:00
function getMockNetworkController ( chainId = FAKE _CHAIN _ID ) {
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
return {
2020-10-31 01:58:12 +01:00
getCurrentChainId : ( ) => chainId ,
2019-08-16 20:54:10 +02:00
on : sinon . spy ( ) ,
}
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
}
2019-08-16 20:54:10 +02:00
2020-11-03 00:41:28 +01:00
function getMockPreferencesController ( {
showIncomingTransactions = true ,
} = { } ) {
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
return {
getSelectedAddress : sinon . stub ( ) . returns ( MOCK _SELECTED _ADDRESS ) ,
2019-08-16 20:54:10 +02:00
store : {
2019-08-21 20:42:14 +02:00
getState : sinon . stub ( ) . returns ( {
featureFlags : {
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
showIncomingTransactions ,
2019-08-21 20:42:14 +02:00
} ,
} ) ,
2019-08-16 20:54:10 +02:00
subscribe : sinon . spy ( ) ,
} ,
}
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
}
2020-11-03 00:41:28 +01:00
function getMockBlockTracker ( ) {
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
return {
addListener : sinon . stub ( ) . callsArgWithAsync ( 1 , '0xa' ) ,
removeListener : sinon . spy ( ) ,
testProperty : 'fakeBlockTracker' ,
getCurrentBlock : ( ) => '0xa' ,
}
}
2020-10-30 15:17:36 +01:00
/ * *
* A transaction object in the format returned by the Etherscan API .
*
* Note that this is not an exhaustive type definiton ; only the properties we use are defined
*
* @ typedef { Object } EtherscanTransaction
* @ property { string } blockNumber - The number of the block this transaction was found in , in decimal
* @ property { string } from - The hex - prefixed address of the sender
* @ property { string } gas - The gas limit , in decimal WEI
* @ property { string } gasPrice - The gas price , in decimal WEI
* @ property { string } hash - The hex - prefixed transaction hash
* @ property { string } isError - Whether the transaction was confirmed or failed ( 0 for confirmed , 1 for failed )
* @ property { string } nonce - The transaction nonce , in decimal
* @ property { string } timeStamp - The timestamp for the transaction , in seconds
* @ property { string } to - The hex - prefixed address of the recipient
* @ property { string } value - The amount of ETH sent in this transaction , in decimal WEI
* /
/ * *
* Returns a transaction object matching the expected format returned
* by the Etherscan API
*
* @ param { string } [ toAddress ] - The hex - prefixed address of the recipient
* @ param { number } [ blockNumber ] - The block number for the transaction
* @ returns { EtherscanTransaction }
* /
2020-11-03 00:41:28 +01:00
const getFakeEtherscanTransaction = (
toAddress = MOCK _SELECTED _ADDRESS ,
blockNumber = 10 ,
) => {
2020-10-30 15:17:36 +01:00
return {
blockNumber : blockNumber . toString ( ) ,
from : '0xfake' ,
gas : '0' ,
gasPrice : '0' ,
hash : '0xfake' ,
isError : '0' ,
nonce : '100' ,
timeStamp : '16000000000000' ,
to : toAddress ,
value : '0' ,
}
}
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
describe ( 'IncomingTransactionsController' , function ( ) {
afterEach ( function ( ) {
sinon . restore ( )
2020-10-30 15:17:36 +01:00
nock . cleanAll ( )
Replace shared mocks in incoming transaction controller tests (#9754)
The shared mocks used previously in the incoming transaction controller
tests have been replaced with functions that can generate a new mock
for each test.
We should avoid ever sharing mocks between tests. It's quite easy for
a mock to get accidentally mutated or not correctly "reset" for the
next test, leading to test inter-dependencies and misleading results.
In particular, it is unsafe to share a `sinon` fake (e.g. a spy or
stub) because they can't be fully reset between tests. Or at least it's
difficult to reset them property, and it can't be done while also
following their recommendations for preventing memory leaks.
The spy API and all related state can be reset with `resetHistory`,
which can be called between each test. However `sinon` also recommends
calling `restore` after each test, and this will cause `sinon` to drop
its internal reference to the fake object, meaning any subsequent call
to `resetHistory` would fail. This is intentional, as it's required to
prevent memory from building up during the test run, but it also means
that sharing `sinon` fakes is particularly difficult to do safely.
Instead we should never share mocks in the first place, which has other
benefits anyway.
This was discovered while writing tests for #9583. I mistakenly
believed that `sinon.restore()` would reset the spy state, and this was
responsible for many hours of debugging test failures.
2020-10-29 16:16:04 +01:00
} )
2019-08-16 20:54:10 +02:00
2020-02-11 17:51:13 +01:00
describe ( 'constructor' , function ( ) {
it ( 'should set up correct store, listeners and properties in the constructor' , function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : { } ,
} ,
)
2019-08-16 20:54:10 +02:00
sinon . spy ( incomingTransactionsController , '_update' )
2020-11-03 00:41:28 +01:00
assert . deepEqual (
incomingTransactionsController . store . getState ( ) ,
getEmptyInitState ( ) ,
)
2019-08-16 20:54:10 +02:00
assert ( incomingTransactionsController . networkController . on . calledOnce )
2020-11-03 00:41:28 +01:00
assert . equal (
incomingTransactionsController . networkController . on . getCall ( 0 ) . args [ 0 ] ,
'networkDidChange' ,
)
const networkControllerListenerCallback = incomingTransactionsController . networkController . on . getCall (
0 ,
) . args [ 1 ]
2019-08-16 20:54:10 +02:00
assert . equal ( incomingTransactionsController . _update . callCount , 0 )
networkControllerListenerCallback ( 'testNetworkType' )
assert . equal ( incomingTransactionsController . _update . callCount , 1 )
2020-11-03 00:41:28 +01:00
assert . deepEqual (
incomingTransactionsController . _update . getCall ( 0 ) . args [ 0 ] ,
{
address : '0x0101' ,
} ,
)
2019-08-16 20:54:10 +02:00
incomingTransactionsController . _update . resetHistory ( )
} )
2020-02-11 17:51:13 +01:00
it ( 'should set the store to a provided initial state' , function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2019-08-16 20:54:10 +02:00
2020-11-03 00:41:28 +01:00
assert . deepEqual (
incomingTransactionsController . store . getState ( ) ,
getNonEmptyInitState ( ) ,
)
2019-08-16 20:54:10 +02:00
} )
} )
2020-10-30 15:17:36 +01:00
describe ( 'update events' , function ( ) {
it ( 'should set up a listener for the latest block' , async function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : { } ,
} ,
)
2019-08-20 20:52:00 +02:00
incomingTransactionsController . start ( )
assert ( incomingTransactionsController . blockTracker . addListener . calledOnce )
2020-11-03 00:41:28 +01:00
assert . equal (
incomingTransactionsController . blockTracker . addListener . getCall ( 0 )
. args [ 0 ] ,
'latest' ,
)
2020-10-30 15:17:36 +01:00
} )
it ( 'should update upon latest block when started and on supported network' , async function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ROPSTEN _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
const startBlock = getNonEmptyInitState ( )
. incomingTxLastFetchedBlocksByNetwork [ ROPSTEN ]
2020-10-30 15:17:36 +01:00
nock ( 'https://api-ropsten.etherscan.io' )
2020-11-03 00:41:28 +01:00
. get (
` /api?module=account&action=txlist&address= ${ MOCK _SELECTED _ADDRESS } &tag=latest&page=1&startBlock= ${ startBlock } ` ,
)
2020-10-30 15:17:36 +01:00
. reply (
200 ,
JSON . stringify ( {
status : '1' ,
result : [ getFakeEtherscanTransaction ( ) ] ,
} ) ,
)
2020-11-03 00:41:28 +01:00
const updateStateStub = sinon . stub (
incomingTransactionsController . store ,
'updateState' ,
)
const updateStateCalled = waitUntilCalled (
updateStateStub ,
incomingTransactionsController . store ,
)
2020-10-30 15:17:36 +01:00
incomingTransactionsController . start ( )
2020-12-04 18:17:57 +01:00
await updateStateCalled ( )
2020-10-30 15:17:36 +01:00
const actualState = incomingTransactionsController . store . getState ( )
const generatedTxId = actualState ? . incomingTransactions ? . [ '0xfake' ] ? . id
const actualStateWithoutGenerated = cloneDeep ( actualState )
delete actualStateWithoutGenerated ? . incomingTransactions ? . [ '0xfake' ] ? . id
2020-11-03 00:41:28 +01:00
assert . ok (
typeof generatedTxId === 'number' && generatedTxId > 0 ,
'Generated transaction ID should be a positive number' ,
)
2020-10-30 15:17:36 +01:00
assert . deepStrictEqual (
actualStateWithoutGenerated ,
{
incomingTransactions : {
... getNonEmptyInitState ( ) . incomingTransactions ,
'0xfake' : {
blockNumber : '10' ,
hash : '0xfake' ,
metamaskNetworkId : '3' ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . CONFIRMED ,
2020-10-30 15:17:36 +01:00
time : 16000000000000000 ,
2020-11-07 08:38:12 +01:00
transactionCategory : TRANSACTION _CATEGORIES . INCOMING ,
2020-10-30 15:17:36 +01:00
txParams : {
from : '0xfake' ,
gas : '0x0' ,
gasPrice : '0x0' ,
nonce : '0x64' ,
to : '0x0101' ,
value : '0x0' ,
} ,
} ,
} ,
incomingTxLastFetchedBlocksByNetwork : {
... getNonEmptyInitState ( ) . incomingTxLastFetchedBlocksByNetwork ,
[ ROPSTEN ] : 11 ,
} ,
} ,
'State should have been updated after first block was received' ,
)
} )
2020-10-31 01:58:12 +01:00
it ( 'should not update upon latest block when started and not on supported network' , async function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2020-10-31 01:58:12 +01:00
// reply with a valid request for any supported network, so that this test has every opportunity to fail
2020-11-03 22:16:28 +01:00
for ( const network of [
GOERLI ,
KOVAN ,
MAINNET ,
RINKEBY ,
ROPSTEN ,
'undefined' ,
] ) {
2020-11-03 00:41:28 +01:00
nock (
` https://api ${
network === MAINNET ? '' : ` - ${ network . toLowerCase ( ) } `
} . etherscan . io ` ,
)
2020-10-31 01:58:12 +01:00
. get ( /api.+/u )
. reply (
200 ,
JSON . stringify ( {
status : '1' ,
result : [ getFakeEtherscanTransaction ( ) ] ,
} ) ,
)
}
2020-11-03 00:41:28 +01:00
const updateStateStub = sinon . stub (
incomingTransactionsController . store ,
'updateState' ,
)
const updateStateCalled = waitUntilCalled (
updateStateStub ,
incomingTransactionsController . store ,
)
const putStateStub = sinon . stub (
incomingTransactionsController . store ,
'putState' ,
)
const putStateCalled = waitUntilCalled (
putStateStub ,
incomingTransactionsController . store ,
)
2020-10-30 15:17:36 +01:00
incomingTransactionsController . start ( )
2020-10-31 01:58:12 +01:00
try {
await Promise . race ( [
2020-12-04 18:17:57 +01:00
updateStateCalled ( ) ,
putStateCalled ( ) ,
2020-10-31 01:58:12 +01:00
new Promise ( ( _ , reject ) => {
2020-11-03 22:16:28 +01:00
setTimeout ( ( ) => reject ( new Error ( 'TIMEOUT' ) ) , SET _STATE _TIMEOUT )
2020-10-31 01:58:12 +01:00
} ) ,
] )
assert . fail ( 'Update state should not have been called' )
} catch ( error ) {
assert ( error . message === 'TIMEOUT' , 'TIMEOUT error should be thrown' )
}
2020-10-30 15:17:36 +01:00
} )
it ( 'should not update upon latest block when started and incoming transactions disabled' , async function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ) ,
preferencesController : getMockPreferencesController ( {
showIncomingTransactions : false ,
} ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2020-10-30 15:17:36 +01:00
// reply with a valid request for any supported network, so that this test has every opportunity to fail
2020-11-03 22:16:28 +01:00
for ( const network of [
GOERLI ,
KOVAN ,
MAINNET ,
RINKEBY ,
ROPSTEN ,
'undefined' ,
] ) {
2020-11-03 00:41:28 +01:00
nock (
` https://api ${
network === MAINNET ? '' : ` - ${ network . toLowerCase ( ) } `
} . etherscan . io ` ,
)
2020-10-30 15:17:36 +01:00
. get ( /api.+/u )
. reply (
200 ,
JSON . stringify ( {
status : '1' ,
result : [ getFakeEtherscanTransaction ( ) ] ,
} ) ,
)
}
2020-11-03 00:41:28 +01:00
const updateStateStub = sinon . stub (
incomingTransactionsController . store ,
'updateState' ,
)
const updateStateCalled = waitUntilCalled (
updateStateStub ,
incomingTransactionsController . store ,
)
const putStateStub = sinon . stub (
incomingTransactionsController . store ,
'putState' ,
)
const putStateCalled = waitUntilCalled (
putStateStub ,
incomingTransactionsController . store ,
)
2020-10-30 15:17:36 +01:00
incomingTransactionsController . start ( )
try {
await Promise . race ( [
2020-12-04 18:17:57 +01:00
updateStateCalled ( ) ,
putStateCalled ( ) ,
2020-10-30 15:17:36 +01:00
new Promise ( ( _ , reject ) => {
2020-11-03 22:16:28 +01:00
setTimeout ( ( ) => reject ( new Error ( 'TIMEOUT' ) ) , SET _STATE _TIMEOUT )
2020-10-30 15:17:36 +01:00
} ) ,
] )
assert . fail ( 'Update state should not have been called' )
} catch ( error ) {
assert ( error . message === 'TIMEOUT' , 'TIMEOUT error should be thrown' )
}
} )
it ( 'should not update upon latest block when not started' , async function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ROPSTEN _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2020-10-30 15:17:36 +01:00
// reply with a valid request for any supported network, so that this test has every opportunity to fail
2020-11-03 22:16:28 +01:00
for ( const network of [
GOERLI ,
KOVAN ,
MAINNET ,
RINKEBY ,
ROPSTEN ,
'undefined' ,
] ) {
2020-11-03 00:41:28 +01:00
nock (
` https://api ${
network === MAINNET ? '' : ` - ${ network . toLowerCase ( ) } `
} . etherscan . io ` ,
)
2020-10-30 15:17:36 +01:00
. get ( /api.+/u )
. reply (
200 ,
JSON . stringify ( {
status : '1' ,
result : [ getFakeEtherscanTransaction ( ) ] ,
} ) ,
)
}
2020-11-03 00:41:28 +01:00
const updateStateStub = sinon . stub (
incomingTransactionsController . store ,
'updateState' ,
)
const updateStateCalled = waitUntilCalled (
updateStateStub ,
incomingTransactionsController . store ,
)
const putStateStub = sinon . stub (
incomingTransactionsController . store ,
'putState' ,
)
const putStateCalled = waitUntilCalled (
putStateStub ,
incomingTransactionsController . store ,
)
2020-10-30 15:17:36 +01:00
try {
await Promise . race ( [
2020-12-04 18:17:57 +01:00
updateStateCalled ( ) ,
putStateCalled ( ) ,
2020-10-30 15:17:36 +01:00
new Promise ( ( _ , reject ) => {
2020-11-03 22:16:28 +01:00
setTimeout ( ( ) => reject ( new Error ( 'TIMEOUT' ) ) , SET _STATE _TIMEOUT )
2020-10-30 15:17:36 +01:00
} ) ,
] )
assert . fail ( 'Update state should not have been called' )
} catch ( error ) {
assert ( error . message === 'TIMEOUT' , 'TIMEOUT error should be thrown' )
}
} )
it ( 'should not update upon latest block when stopped' , async function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ROPSTEN _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2020-10-30 15:17:36 +01:00
// reply with a valid request for any supported network, so that this test has every opportunity to fail
2020-11-03 22:16:28 +01:00
for ( const network of [
GOERLI ,
KOVAN ,
MAINNET ,
RINKEBY ,
ROPSTEN ,
'undefined' ,
] ) {
2020-11-03 00:41:28 +01:00
nock (
` https://api ${
network === MAINNET ? '' : ` - ${ network . toLowerCase ( ) } `
} . etherscan . io ` ,
)
2020-10-30 15:17:36 +01:00
. get ( /api.+/u )
. reply (
200 ,
JSON . stringify ( {
status : '1' ,
result : [ getFakeEtherscanTransaction ( ) ] ,
} ) ,
)
}
2020-11-03 00:41:28 +01:00
const updateStateStub = sinon . stub (
incomingTransactionsController . store ,
'updateState' ,
)
const updateStateCalled = waitUntilCalled (
updateStateStub ,
incomingTransactionsController . store ,
)
const putStateStub = sinon . stub (
incomingTransactionsController . store ,
'putState' ,
)
const putStateCalled = waitUntilCalled (
putStateStub ,
incomingTransactionsController . store ,
)
2020-10-30 15:17:36 +01:00
incomingTransactionsController . stop ( )
try {
await Promise . race ( [
2020-12-04 18:17:57 +01:00
updateStateCalled ( ) ,
putStateCalled ( ) ,
2020-10-30 15:17:36 +01:00
new Promise ( ( _ , reject ) => {
2020-11-03 22:16:28 +01:00
setTimeout ( ( ) => reject ( new Error ( 'TIMEOUT' ) ) , SET _STATE _TIMEOUT )
2020-10-30 15:17:36 +01:00
} ) ,
] )
assert . fail ( 'Update state should not have been called' )
} catch ( error ) {
assert ( error . message === 'TIMEOUT' , 'TIMEOUT error should be thrown' )
}
} )
it ( 'should update when the selected address changes and on supported network' , async function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ROPSTEN _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2020-10-30 15:17:36 +01:00
const NEW _MOCK _SELECTED _ADDRESS = ` ${ MOCK _SELECTED _ADDRESS } 9 `
2020-11-03 00:41:28 +01:00
const startBlock = getNonEmptyInitState ( )
. incomingTxLastFetchedBlocksByNetwork [ ROPSTEN ]
2020-10-30 15:17:36 +01:00
nock ( 'https://api-ropsten.etherscan.io' )
2020-11-03 00:41:28 +01:00
. get (
` /api?module=account&action=txlist&address= ${ NEW _MOCK _SELECTED _ADDRESS } &tag=latest&page=1&startBlock= ${ startBlock } ` ,
)
2020-10-30 15:17:36 +01:00
. reply (
200 ,
JSON . stringify ( {
status : '1' ,
result : [ getFakeEtherscanTransaction ( NEW _MOCK _SELECTED _ADDRESS ) ] ,
} ) ,
)
2020-11-03 00:41:28 +01:00
const updateStateStub = sinon . stub (
incomingTransactionsController . store ,
'updateState' ,
)
const updateStateCalled = waitUntilCalled (
updateStateStub ,
incomingTransactionsController . store ,
)
2020-10-30 15:17:36 +01:00
2020-11-03 00:41:28 +01:00
const subscription = incomingTransactionsController . preferencesController . store . subscribe . getCall (
1 ,
) . args [ 0 ]
2020-10-30 15:17:36 +01:00
// The incoming transactions controller will always skip the first event
// We need to call subscription twice to test the event handling
// TODO: stop skipping the first event
await subscription ( { selectedAddress : MOCK _SELECTED _ADDRESS } )
await subscription ( { selectedAddress : NEW _MOCK _SELECTED _ADDRESS } )
2020-12-04 18:17:57 +01:00
await updateStateCalled ( )
2020-10-30 15:17:36 +01:00
const actualState = incomingTransactionsController . store . getState ( )
const generatedTxId = actualState ? . incomingTransactions ? . [ '0xfake' ] ? . id
const actualStateWithoutGenerated = cloneDeep ( actualState )
delete actualStateWithoutGenerated ? . incomingTransactions ? . [ '0xfake' ] ? . id
2020-11-03 00:41:28 +01:00
assert . ok (
typeof generatedTxId === 'number' && generatedTxId > 0 ,
'Generated transaction ID should be a positive number' ,
)
2020-10-30 15:17:36 +01:00
assert . deepStrictEqual (
actualStateWithoutGenerated ,
{
incomingTransactions : {
... getNonEmptyInitState ( ) . incomingTransactions ,
'0xfake' : {
blockNumber : '10' ,
hash : '0xfake' ,
metamaskNetworkId : '3' ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . CONFIRMED ,
2020-10-30 15:17:36 +01:00
time : 16000000000000000 ,
2020-11-07 08:38:12 +01:00
transactionCategory : TRANSACTION _CATEGORIES . INCOMING ,
2020-10-30 15:17:36 +01:00
txParams : {
from : '0xfake' ,
gas : '0x0' ,
gasPrice : '0x0' ,
nonce : '0x64' ,
to : '0x01019' ,
value : '0x0' ,
} ,
} ,
} ,
incomingTxLastFetchedBlocksByNetwork : {
... getNonEmptyInitState ( ) . incomingTxLastFetchedBlocksByNetwork ,
[ ROPSTEN ] : 11 ,
} ,
} ,
'State should have been updated after first block was received' ,
)
} )
2020-10-31 01:58:12 +01:00
it ( 'should not update when the selected address changes and not on supported network' , async function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : { ... getMockBlockTracker ( ) } ,
networkController : getMockNetworkController ( ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2020-10-30 15:17:36 +01:00
const NEW _MOCK _SELECTED _ADDRESS = ` ${ MOCK _SELECTED _ADDRESS } 9 `
// reply with a valid request for any supported network, so that this test has every opportunity to fail
2020-11-03 22:16:28 +01:00
for ( const network of [
GOERLI ,
KOVAN ,
MAINNET ,
RINKEBY ,
ROPSTEN ,
'undefined' ,
] ) {
2020-11-03 00:41:28 +01:00
nock (
` https://api ${
network === MAINNET ? '' : ` - ${ network . toLowerCase ( ) } `
} . etherscan . io ` ,
)
2020-10-30 15:17:36 +01:00
. get ( /api.+/u )
. reply (
200 ,
JSON . stringify ( {
status : '1' ,
result : [ getFakeEtherscanTransaction ( NEW _MOCK _SELECTED _ADDRESS ) ] ,
} ) ,
)
}
2020-11-03 00:41:28 +01:00
const updateStateStub = sinon . stub (
incomingTransactionsController . store ,
'updateState' ,
)
const updateStateCalled = waitUntilCalled (
updateStateStub ,
incomingTransactionsController . store ,
)
const putStateStub = sinon . stub (
incomingTransactionsController . store ,
'putState' ,
)
const putStateCalled = waitUntilCalled (
putStateStub ,
incomingTransactionsController . store ,
)
2020-10-30 15:17:36 +01:00
2020-11-03 00:41:28 +01:00
const subscription = incomingTransactionsController . preferencesController . store . subscribe . getCall (
1 ,
) . args [ 0 ]
2020-10-30 15:17:36 +01:00
// The incoming transactions controller will always skip the first event
// We need to call subscription twice to test the event handling
// TODO: stop skipping the first event
await subscription ( { selectedAddress : MOCK _SELECTED _ADDRESS } )
await subscription ( { selectedAddress : NEW _MOCK _SELECTED _ADDRESS } )
2020-10-31 01:58:12 +01:00
try {
await Promise . race ( [
2020-12-04 18:17:57 +01:00
updateStateCalled ( ) ,
putStateCalled ( ) ,
2020-10-31 01:58:12 +01:00
new Promise ( ( _ , reject ) => {
2020-11-03 22:16:28 +01:00
setTimeout ( ( ) => reject ( new Error ( 'TIMEOUT' ) ) , SET _STATE _TIMEOUT )
2020-10-31 01:58:12 +01:00
} ) ,
] )
assert . fail ( 'Update state should not have been called' )
} catch ( error ) {
assert ( error . message === 'TIMEOUT' , 'TIMEOUT error should be thrown' )
}
2020-10-30 15:17:36 +01:00
} )
it ( 'should update when switching to a supported network' , async function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ROPSTEN _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
const startBlock = getNonEmptyInitState ( )
. incomingTxLastFetchedBlocksByNetwork [ ROPSTEN ]
2020-10-30 15:17:36 +01:00
nock ( 'https://api-ropsten.etherscan.io' )
2020-11-03 00:41:28 +01:00
. get (
` /api?module=account&action=txlist&address= ${ MOCK _SELECTED _ADDRESS } &tag=latest&page=1&startBlock= ${ startBlock } ` ,
)
2020-10-30 15:17:36 +01:00
. reply (
200 ,
JSON . stringify ( {
status : '1' ,
result : [ getFakeEtherscanTransaction ( ) ] ,
} ) ,
)
2020-11-03 00:41:28 +01:00
const updateStateStub = sinon . stub (
incomingTransactionsController . store ,
'updateState' ,
)
const updateStateCalled = waitUntilCalled (
updateStateStub ,
incomingTransactionsController . store ,
)
2020-10-30 15:17:36 +01:00
2020-11-03 00:41:28 +01:00
const subscription = incomingTransactionsController . networkController . on . getCall (
0 ,
) . args [ 1 ]
incomingTransactionsController . networkController = getMockNetworkController (
ROPSTEN _CHAIN _ID ,
)
2020-10-30 15:17:36 +01:00
await subscription ( ROPSTEN )
2020-12-04 18:17:57 +01:00
await updateStateCalled ( )
2020-10-30 15:17:36 +01:00
const actualState = incomingTransactionsController . store . getState ( )
const generatedTxId = actualState ? . incomingTransactions ? . [ '0xfake' ] ? . id
const actualStateWithoutGenerated = cloneDeep ( actualState )
delete actualStateWithoutGenerated ? . incomingTransactions ? . [ '0xfake' ] ? . id
2020-11-03 00:41:28 +01:00
assert . ok (
typeof generatedTxId === 'number' && generatedTxId > 0 ,
'Generated transaction ID should be a positive number' ,
)
2020-10-30 15:17:36 +01:00
assert . deepStrictEqual (
actualStateWithoutGenerated ,
{
incomingTransactions : {
... getNonEmptyInitState ( ) . incomingTransactions ,
'0xfake' : {
blockNumber : '10' ,
hash : '0xfake' ,
metamaskNetworkId : '3' ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . CONFIRMED ,
2020-10-30 15:17:36 +01:00
time : 16000000000000000 ,
2020-11-07 08:38:12 +01:00
transactionCategory : TRANSACTION _CATEGORIES . INCOMING ,
2020-10-30 15:17:36 +01:00
txParams : {
from : '0xfake' ,
gas : '0x0' ,
gasPrice : '0x0' ,
nonce : '0x64' ,
to : '0x0101' ,
value : '0x0' ,
} ,
} ,
} ,
incomingTxLastFetchedBlocksByNetwork : {
... getNonEmptyInitState ( ) . incomingTxLastFetchedBlocksByNetwork ,
[ ROPSTEN ] : 11 ,
} ,
} ,
'State should have been updated after first block was received' ,
)
} )
2020-10-31 01:58:12 +01:00
it ( 'should not update when switching to an unsupported network' , async function ( ) {
const networkController = getMockNetworkController ( ROPSTEN _CHAIN _ID )
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2020-10-30 15:17:36 +01:00
// reply with a valid request for any supported network, so that this test has every opportunity to fail
2020-11-03 22:16:28 +01:00
for ( const network of [
GOERLI ,
KOVAN ,
MAINNET ,
RINKEBY ,
ROPSTEN ,
'undefined' ,
] ) {
2020-11-03 00:41:28 +01:00
nock (
` https://api ${
network === MAINNET ? '' : ` - ${ network . toLowerCase ( ) } `
} . etherscan . io ` ,
)
2020-10-30 15:17:36 +01:00
. get ( /api.+/u )
. reply (
200 ,
JSON . stringify ( {
status : '1' ,
result : [ getFakeEtherscanTransaction ( ) ] ,
} ) ,
)
}
2020-11-03 00:41:28 +01:00
const updateStateStub = sinon . stub (
incomingTransactionsController . store ,
'updateState' ,
)
const updateStateCalled = waitUntilCalled (
updateStateStub ,
incomingTransactionsController . store ,
)
const putStateStub = sinon . stub (
incomingTransactionsController . store ,
'putState' ,
)
const putStateCalled = waitUntilCalled (
putStateStub ,
incomingTransactionsController . store ,
)
2020-10-30 15:17:36 +01:00
2020-11-03 00:41:28 +01:00
const subscription = incomingTransactionsController . networkController . on . getCall (
0 ,
) . args [ 1 ]
2020-10-30 15:17:36 +01:00
2020-10-31 01:58:12 +01:00
networkController . getCurrentChainId = ( ) => FAKE _CHAIN _ID
await subscription ( )
2020-10-30 15:17:36 +01:00
2020-10-31 01:58:12 +01:00
try {
await Promise . race ( [
2020-12-04 18:17:57 +01:00
updateStateCalled ( ) ,
putStateCalled ( ) ,
2020-10-31 01:58:12 +01:00
new Promise ( ( _ , reject ) => {
2020-11-03 22:16:28 +01:00
setTimeout ( ( ) => reject ( new Error ( 'TIMEOUT' ) ) , SET _STATE _TIMEOUT )
2020-10-31 01:58:12 +01:00
} ) ,
] )
assert . fail ( 'Update state should not have been called' )
} catch ( error ) {
assert ( error . message === 'TIMEOUT' , 'TIMEOUT error should be thrown' )
}
2019-08-20 20:52:00 +02:00
} )
} )
2020-02-11 17:51:13 +01:00
describe ( '_getDataForUpdate' , function ( ) {
it ( 'should call fetchAll with the correct params when passed a new block number and the current network has no stored block' , async function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ROPSTEN _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getEmptyInitState ( ) ,
} ,
)
2019-08-16 20:54:10 +02:00
incomingTransactionsController . _fetchAll = sinon . stub ( ) . returns ( { } )
2020-10-31 01:58:12 +01:00
await incomingTransactionsController . _getDataForUpdate ( {
address : 'fakeAddress' ,
chainId : ROPSTEN _CHAIN _ID ,
newBlockNumberDec : 999 ,
2019-08-16 20:54:10 +02:00
} )
assert ( incomingTransactionsController . _fetchAll . calledOnce )
2020-11-03 00:41:28 +01:00
assert . deepEqual (
incomingTransactionsController . _fetchAll . getCall ( 0 ) . args ,
[ 'fakeAddress' , 999 , ROPSTEN _CHAIN _ID ] ,
)
2019-08-16 20:54:10 +02:00
} )
2020-10-31 01:58:12 +01:00
it ( 'should call fetchAll with the correct params when passed a new block number but the current network has a stored block' , async function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ROPSTEN _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2019-08-16 20:54:10 +02:00
incomingTransactionsController . _fetchAll = sinon . stub ( ) . returns ( { } )
await incomingTransactionsController . _getDataForUpdate ( {
address : 'fakeAddress' ,
2020-10-31 01:58:12 +01:00
chainId : ROPSTEN _CHAIN _ID ,
newBlockNumberDec : 999 ,
2019-08-16 20:54:10 +02:00
} )
assert ( incomingTransactionsController . _fetchAll . calledOnce )
2020-11-03 00:41:28 +01:00
assert . deepEqual (
incomingTransactionsController . _fetchAll . getCall ( 0 ) . args ,
[ 'fakeAddress' , 4 , ROPSTEN _CHAIN _ID ] ,
)
2019-08-16 20:54:10 +02:00
} )
2020-02-11 17:51:13 +01:00
it ( 'should return the expected data' , async function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ROPSTEN _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2019-08-16 20:54:10 +02:00
incomingTransactionsController . _fetchAll = sinon . stub ( ) . returns ( {
latestIncomingTxBlockNumber : 444 ,
txs : [ { id : 555 } ] ,
} )
const result = await incomingTransactionsController . _getDataForUpdate ( {
address : 'fakeAddress' ,
2020-10-31 01:58:12 +01:00
chainId : ROPSTEN _CHAIN _ID ,
2019-08-16 20:54:10 +02:00
} )
assert . deepEqual ( result , {
latestIncomingTxBlockNumber : 444 ,
newTxs : [ { id : 555 } ] ,
currentIncomingTxs : {
'0x123456' : { id : 777 } ,
} ,
currentBlocksByNetwork : {
2020-10-31 01:58:12 +01:00
[ GOERLI ] : 1 ,
[ KOVAN ] : 2 ,
[ MAINNET ] : 3 ,
[ RINKEBY ] : 5 ,
[ ROPSTEN ] : 4 ,
2019-08-16 20:54:10 +02:00
} ,
2020-10-31 01:58:12 +01:00
fetchedBlockNumber : 4 ,
chainId : ROPSTEN _CHAIN _ID ,
2019-08-16 20:54:10 +02:00
} )
} )
} )
2020-02-11 17:51:13 +01:00
describe ( '_updateStateWithNewTxData' , function ( ) {
2019-08-16 20:54:10 +02:00
const MOCK _INPUT _WITHOUT _LASTEST = {
newTxs : [ { id : 555 , hash : '0xfff' } ] ,
currentIncomingTxs : {
'0x123456' : { id : 777 , hash : '0x123456' } ,
} ,
currentBlocksByNetwork : {
2020-10-31 01:58:12 +01:00
[ GOERLI ] : 1 ,
[ KOVAN ] : 2 ,
[ MAINNET ] : 3 ,
[ RINKEBY ] : 5 ,
[ ROPSTEN ] : 4 ,
2019-08-16 20:54:10 +02:00
} ,
fetchedBlockNumber : 1111 ,
2020-10-31 01:58:12 +01:00
chainId : ROPSTEN _CHAIN _ID ,
2019-08-16 20:54:10 +02:00
}
const MOCK _INPUT _WITH _LASTEST = {
... MOCK _INPUT _WITHOUT _LASTEST ,
latestIncomingTxBlockNumber : 444 ,
}
2020-02-11 17:51:13 +01:00
it ( 'should update state with correct blockhash and transactions when passed a truthy latestIncomingTxBlockNumber' , async function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ROPSTEN _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2019-08-16 20:54:10 +02:00
sinon . spy ( incomingTransactionsController . store , 'updateState' )
2020-11-03 00:41:28 +01:00
await incomingTransactionsController . _updateStateWithNewTxData (
MOCK _INPUT _WITH _LASTEST ,
)
2019-08-16 20:54:10 +02:00
assert ( incomingTransactionsController . store . updateState . calledOnce )
2020-11-03 00:41:28 +01:00
assert . deepEqual (
incomingTransactionsController . store . updateState . getCall ( 0 ) . args [ 0 ] ,
{
incomingTxLastFetchedBlocksByNetwork : {
... MOCK _INPUT _WITH _LASTEST . currentBlocksByNetwork ,
[ ROPSTEN ] : 445 ,
} ,
incomingTransactions : {
'0x123456' : { id : 777 , hash : '0x123456' } ,
'0xfff' : { id : 555 , hash : '0xfff' } ,
} ,
2019-08-16 20:54:10 +02:00
} ,
2020-11-03 00:41:28 +01:00
)
2019-08-16 20:54:10 +02:00
} )
2020-02-11 17:51:13 +01:00
it ( 'should update state with correct blockhash and transactions when passed a falsy latestIncomingTxBlockNumber' , async function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ROPSTEN _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2019-08-16 20:54:10 +02:00
sinon . spy ( incomingTransactionsController . store , 'updateState' )
2020-11-03 00:41:28 +01:00
await incomingTransactionsController . _updateStateWithNewTxData (
MOCK _INPUT _WITHOUT _LASTEST ,
)
2019-08-16 20:54:10 +02:00
assert ( incomingTransactionsController . store . updateState . calledOnce )
2020-11-03 00:41:28 +01:00
assert . deepEqual (
incomingTransactionsController . store . updateState . getCall ( 0 ) . args [ 0 ] ,
{
incomingTxLastFetchedBlocksByNetwork : {
... MOCK _INPUT _WITH _LASTEST . currentBlocksByNetwork ,
[ ROPSTEN ] : 1112 ,
} ,
incomingTransactions : {
'0x123456' : { id : 777 , hash : '0x123456' } ,
'0xfff' : { id : 555 , hash : '0xfff' } ,
} ,
2019-08-16 20:54:10 +02:00
} ,
2020-11-03 00:41:28 +01:00
)
2019-08-16 20:54:10 +02:00
} )
} )
2020-02-11 17:51:13 +01:00
describe ( '_fetchTxs' , function ( ) {
2020-11-03 00:41:28 +01:00
const mockFetch = sinon . stub ( ) . returns (
Promise . resolve ( {
json : ( ) => Promise . resolve ( { someKey : 'someValue' } ) ,
} ) ,
)
2019-08-16 20:54:10 +02:00
let tempFetch
2020-02-11 17:51:13 +01:00
beforeEach ( function ( ) {
2020-04-15 19:23:27 +02:00
tempFetch = window . fetch
window . fetch = mockFetch
2019-08-16 20:54:10 +02:00
} )
2020-02-11 17:51:13 +01:00
afterEach ( function ( ) {
2020-04-15 19:23:27 +02:00
window . fetch = tempFetch
2019-08-16 20:54:10 +02:00
mockFetch . resetHistory ( )
} )
2020-02-11 17:51:13 +01:00
it ( 'should call fetch with the expected url when passed an address, block number and supported network' , async function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ROPSTEN _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2019-08-16 20:54:10 +02:00
2020-11-03 00:41:28 +01:00
await incomingTransactionsController . _fetchTxs (
'0xfakeaddress' ,
'789' ,
ROPSTEN _CHAIN _ID ,
)
2019-08-16 20:54:10 +02:00
assert ( mockFetch . calledOnce )
2020-11-03 00:41:28 +01:00
assert . equal (
mockFetch . getCall ( 0 ) . args [ 0 ] ,
` https://api- ${ ROPSTEN } .etherscan.io/api?module=account&action=txlist&address=0xfakeaddress&tag=latest&page=1&startBlock=789 ` ,
)
2019-08-16 20:54:10 +02:00
} )
2020-02-11 17:51:13 +01:00
it ( 'should call fetch with the expected url when passed an address, block number and MAINNET' , async function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( MAINNET _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2019-08-16 20:54:10 +02:00
2020-11-03 00:41:28 +01:00
await incomingTransactionsController . _fetchTxs (
'0xfakeaddress' ,
'789' ,
MAINNET _CHAIN _ID ,
)
2019-08-16 20:54:10 +02:00
assert ( mockFetch . calledOnce )
2020-11-03 00:41:28 +01:00
assert . equal (
mockFetch . getCall ( 0 ) . args [ 0 ] ,
` https://api.etherscan.io/api?module=account&action=txlist&address=0xfakeaddress&tag=latest&page=1&startBlock=789 ` ,
)
2019-08-16 20:54:10 +02:00
} )
2020-02-11 17:51:13 +01:00
it ( 'should call fetch with the expected url when passed an address and supported network, but a falsy block number' , async function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ROPSTEN _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2019-08-16 20:54:10 +02:00
2020-11-03 00:41:28 +01:00
await incomingTransactionsController . _fetchTxs (
'0xfakeaddress' ,
null ,
ROPSTEN _CHAIN _ID ,
)
2019-08-16 20:54:10 +02:00
assert ( mockFetch . calledOnce )
2020-11-03 00:41:28 +01:00
assert . equal (
mockFetch . getCall ( 0 ) . args [ 0 ] ,
` https://api- ${ ROPSTEN } .etherscan.io/api?module=account&action=txlist&address=0xfakeaddress&tag=latest&page=1 ` ,
)
2019-08-16 20:54:10 +02:00
} )
2020-02-11 17:51:13 +01:00
it ( 'should return the results from the fetch call, plus the address and currentNetworkID, when passed an address, block number and supported network' , async function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ROPSTEN _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2019-08-16 20:54:10 +02:00
2020-11-03 00:41:28 +01:00
const result = await incomingTransactionsController . _fetchTxs (
'0xfakeaddress' ,
'789' ,
ROPSTEN _CHAIN _ID ,
)
2019-08-16 20:54:10 +02:00
assert ( mockFetch . calledOnce )
assert . deepEqual ( result , {
someKey : 'someValue' ,
address : '0xfakeaddress' ,
2020-10-31 01:58:12 +01:00
chainId : ROPSTEN _CHAIN _ID ,
2019-08-16 20:54:10 +02:00
} )
} )
} )
2020-02-11 17:51:13 +01:00
describe ( '_processTxFetchResponse' , function ( ) {
it ( 'should return a null block number and empty tx array if status is 0' , function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ROPSTEN _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2019-08-16 20:54:10 +02:00
const result = incomingTransactionsController . _processTxFetchResponse ( {
status : '0' ,
result : [ { id : 1 } ] ,
address : '0xfakeaddress' ,
} )
assert . deepEqual ( result , {
latestIncomingTxBlockNumber : null ,
txs : [ ] ,
} )
} )
2020-02-11 17:51:13 +01:00
it ( 'should return a null block number and empty tx array if the passed result array is empty' , function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ROPSTEN _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2019-08-16 20:54:10 +02:00
const result = incomingTransactionsController . _processTxFetchResponse ( {
status : '1' ,
result : [ ] ,
address : '0xfakeaddress' ,
} )
assert . deepEqual ( result , {
latestIncomingTxBlockNumber : null ,
txs : [ ] ,
} )
} )
2020-02-11 17:51:13 +01:00
it ( 'should return the expected block number and tx list when passed data from a successful fetch' , function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ROPSTEN _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2019-08-16 20:54:10 +02:00
2020-10-31 01:58:12 +01:00
incomingTransactionsController . _normalizeTxFromEtherscan = ( tx ) => ( {
2019-08-16 20:54:10 +02:00
... tx ,
2020-10-31 01:58:12 +01:00
currentNetworkID : ROPSTEN _NETWORK _ID ,
2019-08-16 20:54:10 +02:00
normalized : true ,
} )
const result = incomingTransactionsController . _processTxFetchResponse ( {
status : '1' ,
address : '0xfakeaddress' ,
2020-10-31 01:58:12 +01:00
chainId : ROPSTEN _CHAIN _ID ,
2019-08-16 20:54:10 +02:00
result : [
{
hash : '0xabc123' ,
txParams : {
to : '0xfakeaddress' ,
} ,
blockNumber : 5000 ,
time : 10 ,
} ,
{
hash : '0xabc123' ,
txParams : {
to : '0xfakeaddress' ,
} ,
blockNumber : 5000 ,
time : 10 ,
} ,
{
hash : '0xabc1234' ,
txParams : {
to : '0xfakeaddress' ,
} ,
blockNumber : 5000 ,
time : 9 ,
} ,
{
hash : '0xabc12345' ,
txParams : {
to : '0xfakeaddress' ,
} ,
blockNumber : 5001 ,
time : 11 ,
} ,
{
hash : '0xabc123456' ,
txParams : {
to : '0xfakeaddress' ,
} ,
blockNumber : 5001 ,
time : 12 ,
} ,
{
hash : '0xabc1234567' ,
txParams : {
to : '0xanotherFakeaddress' ,
} ,
blockNumber : 5002 ,
time : 13 ,
} ,
] ,
} )
assert . deepEqual ( result , {
latestIncomingTxBlockNumber : 5001 ,
txs : [
{
hash : '0xabc1234' ,
txParams : {
to : '0xfakeaddress' ,
} ,
blockNumber : 5000 ,
time : 9 ,
normalized : true ,
2020-10-31 01:58:12 +01:00
currentNetworkID : ROPSTEN _NETWORK _ID ,
2019-08-16 20:54:10 +02:00
} ,
{
hash : '0xabc123' ,
txParams : {
to : '0xfakeaddress' ,
} ,
blockNumber : 5000 ,
time : 10 ,
normalized : true ,
2020-10-31 01:58:12 +01:00
currentNetworkID : ROPSTEN _NETWORK _ID ,
2019-08-16 20:54:10 +02:00
} ,
{
hash : '0xabc12345' ,
txParams : {
to : '0xfakeaddress' ,
} ,
blockNumber : 5001 ,
time : 11 ,
normalized : true ,
2020-10-31 01:58:12 +01:00
currentNetworkID : ROPSTEN _NETWORK _ID ,
2019-08-16 20:54:10 +02:00
} ,
{
hash : '0xabc123456' ,
txParams : {
to : '0xfakeaddress' ,
} ,
blockNumber : 5001 ,
time : 12 ,
normalized : true ,
2020-10-31 01:58:12 +01:00
currentNetworkID : ROPSTEN _NETWORK _ID ,
2019-08-16 20:54:10 +02:00
} ,
] ,
} )
} )
} )
2020-02-11 17:51:13 +01:00
describe ( '_normalizeTxFromEtherscan' , function ( ) {
it ( 'should return the expected data when the tx is in error' , function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ROPSTEN _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2019-08-16 20:54:10 +02:00
2020-11-03 00:41:28 +01:00
const result = incomingTransactionsController . _normalizeTxFromEtherscan (
{
timeStamp : '4444' ,
isError : '1' ,
blockNumber : 333 ,
from : '0xa' ,
gas : '11' ,
gasPrice : '12' ,
nonce : '13' ,
to : '0xe' ,
value : '15' ,
hash : '0xg' ,
} ,
ROPSTEN _CHAIN _ID ,
)
2019-08-16 20:54:10 +02:00
assert . deepEqual ( result , {
blockNumber : 333 ,
id : 54321 ,
2020-10-31 01:58:12 +01:00
metamaskNetworkId : ROPSTEN _NETWORK _ID ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . FAILED ,
2019-08-16 20:54:10 +02:00
time : 4444000 ,
txParams : {
from : '0xa' ,
gas : '0xb' ,
gasPrice : '0xc' ,
nonce : '0xd' ,
to : '0xe' ,
value : '0xf' ,
} ,
hash : '0xg' ,
2020-11-07 08:38:12 +01:00
transactionCategory : TRANSACTION _CATEGORIES . INCOMING ,
2019-08-16 20:54:10 +02:00
} )
} )
2020-02-11 17:51:13 +01:00
it ( 'should return the expected data when the tx is not in error' , function ( ) {
2020-11-03 00:41:28 +01:00
const incomingTransactionsController = new IncomingTransactionsController (
{
blockTracker : getMockBlockTracker ( ) ,
networkController : getMockNetworkController ( ROPSTEN _CHAIN _ID ) ,
preferencesController : getMockPreferencesController ( ) ,
initState : getNonEmptyInitState ( ) ,
} ,
)
2019-08-16 20:54:10 +02:00
2020-11-03 00:41:28 +01:00
const result = incomingTransactionsController . _normalizeTxFromEtherscan (
{
timeStamp : '4444' ,
isError : '0' ,
blockNumber : 333 ,
from : '0xa' ,
gas : '11' ,
gasPrice : '12' ,
nonce : '13' ,
to : '0xe' ,
value : '15' ,
hash : '0xg' ,
} ,
ROPSTEN _CHAIN _ID ,
)
2019-08-16 20:54:10 +02:00
assert . deepEqual ( result , {
blockNumber : 333 ,
id : 54321 ,
2020-10-31 01:58:12 +01:00
metamaskNetworkId : ROPSTEN _NETWORK _ID ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . CONFIRMED ,
2019-08-16 20:54:10 +02:00
time : 4444000 ,
txParams : {
from : '0xa' ,
gas : '0xb' ,
gasPrice : '0xc' ,
nonce : '0xd' ,
to : '0xe' ,
value : '0xf' ,
} ,
hash : '0xg' ,
2020-11-07 08:38:12 +01:00
transactionCategory : TRANSACTION _CATEGORIES . INCOMING ,
2019-08-16 20:54:10 +02:00
} )
} )
} )
} )