aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2017-10-03 04:14:42 +0800
committerkumavis <aaron@kumavis.me>2017-10-03 04:14:42 +0800
commitd29b5f10ef5137ab56ecc9615e5e894082db9803 (patch)
tree78d6b3b1bb8f9e7c6e5a0fb5d8a0fef9b122b18c
parent1769f880dbbb8d16312586e421dec938ca249754 (diff)
downloadtangerine-wallet-browser-d29b5f10ef5137ab56ecc9615e5e894082db9803.tar
tangerine-wallet-browser-d29b5f10ef5137ab56ecc9615e5e894082db9803.tar.gz
tangerine-wallet-browser-d29b5f10ef5137ab56ecc9615e5e894082db9803.tar.bz2
tangerine-wallet-browser-d29b5f10ef5137ab56ecc9615e5e894082db9803.tar.lz
tangerine-wallet-browser-d29b5f10ef5137ab56ecc9615e5e894082db9803.tar.xz
tangerine-wallet-browser-d29b5f10ef5137ab56ecc9615e5e894082db9803.tar.zst
tangerine-wallet-browser-d29b5f10ef5137ab56ecc9615e5e894082db9803.zip
tx state history - fix bug where initial snapshot was mutated on updateTx
-rw-r--r--app/scripts/lib/tx-state-history-helper.js3
-rw-r--r--app/scripts/lib/tx-state-manager.js2
-rw-r--r--test/unit/tx-state-history-helper.js22
3 files changed, 25 insertions, 2 deletions
diff --git a/app/scripts/lib/tx-state-history-helper.js b/app/scripts/lib/tx-state-history-helper.js
index 304069d57..5ebd78689 100644
--- a/app/scripts/lib/tx-state-history-helper.js
+++ b/app/scripts/lib/tx-state-history-helper.js
@@ -24,7 +24,8 @@ function generateHistoryEntry(previousState, newState) {
return jsonDiffer.compare(previousState, newState)
}
-function replayHistory(shortHistory) {
+function replayHistory(_shortHistory) {
+ const shortHistory = clone(_shortHistory)
return shortHistory.reduce((val, entry) => jsonDiffer.applyPatch(val, entry).newDocument)
}
diff --git a/app/scripts/lib/tx-state-manager.js b/app/scripts/lib/tx-state-manager.js
index abb9d7910..4493889bf 100644
--- a/app/scripts/lib/tx-state-manager.js
+++ b/app/scripts/lib/tx-state-manager.js
@@ -97,7 +97,7 @@ module.exports = class TransactionStateManger extends EventEmitter {
const previousState = txStateHistoryHelper.replayHistory(txMeta.history)
// generate history entry and add to history
const entry = txStateHistoryHelper.generateHistoryEntry(previousState, currentState)
- txMeta.history.push(entry)
+ txMeta.history.push(entry)
// commit txMeta to state
const txId = txMeta.id
diff --git a/test/unit/tx-state-history-helper.js b/test/unit/tx-state-history-helper.js
index 5bb6c9bee..e5075af88 100644
--- a/test/unit/tx-state-history-helper.js
+++ b/test/unit/tx-state-history-helper.js
@@ -20,4 +20,26 @@ describe('tx-state-history-helper', function () {
})
})
})
+
+ it('replaying history does not mutate the original obj', function () {
+ const initialState = { test: true, message: 'hello', value: 1 }
+ const diff1 = {
+ "op": "replace",
+ "path": "/message",
+ "value": "haay",
+ }
+ const diff2 = {
+ "op": "replace",
+ "path": "/value",
+ "value": 2,
+ }
+ const history = [initialState, diff1, diff2]
+
+ const beforeStateSnapshot = JSON.stringify(initialState)
+ const latestState = txStateHistoryHelper.replayHistory(history)
+ const afterStateSnapshot = JSON.stringify(initialState)
+
+ assert.notEqual(initialState, latestState, 'initial state is not the same obj as the latest state')
+ assert.equal(beforeStateSnapshot, afterStateSnapshot, 'initial state is not modified during run')
+ })
})