aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit
diff options
context:
space:
mode:
authorfrankiebee <frankie.diamond@gmail.com>2017-08-22 02:35:22 +0800
committerfrankiebee <frankie.diamond@gmail.com>2017-08-22 02:35:22 +0800
commitfbba3a1ac8575b910e8a2a684748d617eec19414 (patch)
treec2940c33345d128a2888fc4441b8342e6b778d65 /test/unit
parent7ea83b6bae34dcf652d85474fe1d82893d592d55 (diff)
parent9203b4edf9df8b616877c57970fb01a7fb87924b (diff)
downloadtangerine-wallet-browser-fbba3a1ac8575b910e8a2a684748d617eec19414.tar.gz
tangerine-wallet-browser-fbba3a1ac8575b910e8a2a684748d617eec19414.tar.zst
tangerine-wallet-browser-fbba3a1ac8575b910e8a2a684748d617eec19414.zip
Merge branch 'master' into transactionControllerRefractorPt3
Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/actions/tx_test.js87
-rw-r--r--test/unit/tx-controller-test.js9
-rw-r--r--test/unit/tx-state-history-helper.js23
-rw-r--r--test/unit/tx-state-manager-test.js19
4 files changed, 46 insertions, 92 deletions
diff --git a/test/unit/actions/tx_test.js b/test/unit/actions/tx_test.js
index 67c72e9a5..ea6dfda6a 100644
--- a/test/unit/actions/tx_test.js
+++ b/test/unit/actions/tx_test.js
@@ -53,7 +53,7 @@ describe('tx confirmation screen', function () {
result = reducers(initialState, action)
done()
})
-
+
})
it('should transition to the account detail view', function () {
@@ -65,91 +65,6 @@ describe('tx confirmation screen', function () {
assert.equal(count, 0)
})
})
-
- describe('sendTx', function () {
- var result
-
- describe('when there is an error', function () {
- before(function (done) {
- actions._setBackgroundConnection({
- approveTransaction (txId, cb) { cb({message: 'An error!'}) },
- })
-
- actions.sendTx({id: firstTxId})(function (action) {
- result = reducers(initialState, action)
- done()
- })
- })
-
- it('should stay on the page', function () {
- assert.equal(result.appState.currentView.name, 'confTx')
- })
-
- it('should set errorMessage on the currentView', function () {
- assert(result.appState.currentView.errorMessage)
- })
- })
-
- describe('when there is success', function () {
- it('should complete tx and go home', function () {
- actions._setBackgroundConnection({
- approveTransaction (txId, cb) { cb() },
- })
-
- var dispatchExpect = sinon.mock()
- dispatchExpect.twice()
-
- actions.sendTx({id: firstTxId})(dispatchExpect)
- })
- })
- })
-
- describe('when there are two pending txs', function () {
- var firstTxId = 1457634084250832
- var result, initialState
- before(function (done) {
- initialState = {
- appState: {
- currentView: {
- name: 'confTx',
- },
- },
- metamask: {
- unapprovedTxs: {
- '1457634084250832': {
- id: firstTxId,
- status: 'unconfirmed',
- time: 1457634084250,
- },
- '1457634084250833': {
- id: 1457634084250833,
- status: 'unconfirmed',
- time: 1457634084255,
- },
- },
- },
- }
- freeze(initialState)
-
- // Mocking a background connection:
- actions._setBackgroundConnection({
- approveTransaction (firstTxId, cb) { cb() },
- })
-
- actions.sendTx({id: firstTxId})(function (action) {
- result = reducers(initialState, action)
- })
- done()
- })
-
- it('should stay on the confTx view', function () {
- assert.equal(result.appState.currentView.name, 'confTx')
- })
-
- it('should transition to the first tx', function () {
- assert.equal(result.appState.currentView.context, 0)
- })
- })
})
})
diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js
index 40be490c0..fdbddac4b 100644
--- a/test/unit/tx-controller-test.js
+++ b/test/unit/tx-controller-test.js
@@ -6,12 +6,15 @@ const clone = require('clone')
const sinon = require('sinon')
const TransactionController = require('../../app/scripts/controllers/transactions')
const TxProvideUtils = require('../../app/scripts/lib/tx-utils')
+const txStateHistoryHelper = require('../../app/scripts/lib/tx-state-history-helper')
+
const noop = () => true
const currentNetworkId = 42
const otherNetworkId = 36
const privKey = new Buffer('8718b9618a37d1fc78c436511fc6df3c8258d3250635bba617f33003270ec03e', 'hex')
const { createStubedProvider } = require('../stub/provider')
+
describe('Transaction Controller', function () {
let txController, engine, provider, providerResultStub
@@ -46,9 +49,10 @@ describe('Transaction Controller', function () {
id: 1,
metamaskNetworkId: currentNetworkId,
txParams,
+ history: [],
}
txController.txStateManager._saveTxList([txMeta])
- stub = sinon.stub(txController, 'addUnapprovedTransaction').returns(Promise.resolve(txMeta))
+ stub = sinon.stub(txController, 'addUnapprovedTransaction').returns(Promise.resolve(txController.txStateManager.addTx(txMeta)))
})
afterEach(function () {
@@ -163,7 +167,8 @@ describe('Transaction Controller', function () {
describe('#addTx', function () {
it('should emit updates', function (done) {
- txMeta = {
+ const txMeta = {
+ id: '1',
status: 'unapproved',
id: 1,
metamaskNetworkId: currentNetworkId,
diff --git a/test/unit/tx-state-history-helper.js b/test/unit/tx-state-history-helper.js
new file mode 100644
index 000000000..5bb6c9bee
--- /dev/null
+++ b/test/unit/tx-state-history-helper.js
@@ -0,0 +1,23 @@
+const assert = require('assert')
+const txStateHistoryHelper = require('../../app/scripts/lib/tx-state-history-helper')
+const testVault = require('../data/v17-long-history.json')
+
+
+describe('tx-state-history-helper', function () {
+ it('migrates history to diffs and can recover original values', function () {
+ testVault.data.TransactionController.transactions.forEach((tx, index) => {
+ const newHistory = txStateHistoryHelper.migrateFromSnapshotsToDiffs(tx.history)
+ newHistory.forEach((newEntry, index) => {
+ if (index === 0) {
+ assert.equal(Array.isArray(newEntry), false, 'initial history item IS NOT a json patch obj')
+ } else {
+ assert.equal(Array.isArray(newEntry), true, 'non-initial history entry IS a json patch obj')
+ }
+ const oldEntry = tx.history[index]
+ const historySubset = newHistory.slice(0, index + 1)
+ const reconstructedValue = txStateHistoryHelper.replayHistory(historySubset)
+ assert.deepEqual(oldEntry, reconstructedValue, 'was able to reconstruct old entry from diffs')
+ })
+ })
+ })
+})
diff --git a/test/unit/tx-state-manager-test.js b/test/unit/tx-state-manager-test.js
index 0b35465d6..998bbe152 100644
--- a/test/unit/tx-state-manager-test.js
+++ b/test/unit/tx-state-manager-test.js
@@ -150,7 +150,7 @@ describe('TransactionStateManger', function () {
assert.equal(result.hash, 'foo')
})
- it('updates gas price', function () {
+ it('updates gas price and adds history items', function () {
const originalGasPrice = '0x01'
const desiredGasPrice = '0x02'
@@ -166,10 +166,21 @@ describe('TransactionStateManger', function () {
const updatedMeta = clone(txMeta)
txStateManager.addTx(txMeta)
- updatedMeta.txParams.gasPrice = desiredGasPrice
- txStateManager.updateTx(updatedMeta)
- let result = txStateManager.getTx('1')
+ const updatedTx = txController.getTx('1')
+ // verify tx was initialized correctly
+ assert.equal(updatedTx.history.length, 1, 'one history item (initial)')
+ assert.equal(Array.isArray(updatedTx.history[0]), false, 'first history item is initial state')
+ assert.deepEqual(updatedTx.history[0], txStateHistoryHelper.snapshotFromTxMeta(updatedTx), 'first history item is initial state')
+ // modify value and updateTx
+ updatedTx.txParams.gasPrice = desiredGasPrice
+ txController.updateTx(updatedTx)
+ // check updated value
+ const result = txController.getTx('1')
assert.equal(result.txParams.gasPrice, desiredGasPrice, 'gas price updated')
+ // validate history was updated
+ assert.equal(result.history.length, 2, 'two history items (initial + diff)')
+ const expectedEntry = { op: 'replace', path: '/txParams/gasPrice', value: desiredGasPrice }
+ assert.deepEqual(result.history[1], [expectedEntry], 'two history items (initial + diff)')
})
})