1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-23 03:36:18 +02:00
metamask-extension/test/unit/migrations/023-test.js
Whymarrh Whitby 4f3fc95d50
Update ESLint rules for test suite (#8023)
* Use @metamask/eslint-config@1.1.0
* Use eslint-plugin-mocha@6.2.2
* Mark root ESLint config as root
* Update Mocha ESLint rules with shared ESLint config
2020-02-11 13:21:13 -03:30

101 lines
3.0 KiB
JavaScript

import assert from 'assert'
import migration23 from '../../../app/scripts/migrations/023'
const storage = {
'meta': {},
'data': {
'TransactionController': {
'transactions': [
],
},
},
}
const transactions = []
const transactions40 = []
const transactions20 = []
const txStates = [
'unapproved',
'approved',
'signed',
'submitted',
'confirmed',
'rejected',
'failed',
'dropped',
]
const deletableTxStates = [
'confirmed',
'rejected',
'failed',
'dropped',
]
let nonDeletableCount = 0
let status
while (transactions.length <= 100) {
status = txStates[Math.floor(Math.random() * Math.floor(txStates.length - 1))]
if (!deletableTxStates.find((s) => s === status)) {
nonDeletableCount++
}
transactions.push({ status })
}
while (transactions40.length < 40) {
status = txStates[Math.floor(Math.random() * Math.floor(txStates.length - 1))]
transactions40.push({ status })
}
while (transactions20.length < 20) {
status = txStates[Math.floor(Math.random() * Math.floor(txStates.length - 1))]
transactions20.push({ status })
}
storage.data.TransactionController.transactions = transactions
describe('storage is migrated successfully and the proper transactions are remove from state', function () {
it('should remove transactions that are unneeded', function (done) {
migration23.migrate(storage)
.then((migratedData) => {
let leftoverNonDeletableTxCount = 0
const migratedTransactions = migratedData.data.TransactionController.transactions
migratedTransactions.forEach((tx) => {
if (!deletableTxStates.find((s) => s === tx.status)) {
leftoverNonDeletableTxCount++
}
})
assert.equal(leftoverNonDeletableTxCount, nonDeletableCount, 'migration shouldnt delete transactions we want to keep')
assert((migratedTransactions.length >= 40), `should be equal or greater to 40 if they are non deletable states got ${migratedTransactions.length} transactions`)
done()
}).catch(done)
})
it('should not remove any transactions because 40 is the expectable limit', function (done) {
storage.meta.version = 22
storage.data.TransactionController.transactions = transactions40
migration23.migrate(storage)
.then((migratedData) => {
const migratedTransactions = migratedData.data.TransactionController.transactions
assert.equal(migratedTransactions.length, 40, 'migration shouldnt delete when at limit')
done()
}).catch(done)
})
it('should not remove any transactions because 20 txs is under the expectable limit', function (done) {
storage.meta.version = 22
storage.data.TransactionController.transactions = transactions20
migration23.migrate(storage)
.then((migratedData) => {
const migratedTransactions = migratedData.data.TransactionController.transactions
assert.equal(migratedTransactions.length, 20, 'migration shouldnt delete when under limit')
done()
}).catch(done)
})
})