Merge pull request #2259 from MetaMask/tx-history-fix

tx state history - fix bug where initial snapshot was mutated on updateTx
feature/default_network_editable
Frankie 7 years ago committed by GitHub
commit bf390bd63d
  1. 3
      app/scripts/lib/tx-state-history-helper.js
  2. 22
      test/unit/tx-state-history-helper.js

@ -24,7 +24,8 @@ function generateHistoryEntry(previousState, newState) {
return jsonDiffer.compare(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) return shortHistory.reduce((val, entry) => jsonDiffer.applyPatch(val, entry).newDocument)
} }

@ -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')
})
}) })

Loading…
Cancel
Save