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

116 lines
3.3 KiB
JavaScript
Raw Normal View History

import assert from 'assert'
import migration23 from '../../../app/scripts/migrations/023'
import { TRANSACTION_STATUSES } from '../../../shared/constants/transaction'
const storage = {
2020-11-03 00:41:28 +01:00
meta: {},
data: {
TransactionController: {
transactions: [],
},
},
}
const transactions = []
const transactions40 = []
const transactions20 = []
const txStates = Object.values(TRANSACTION_STATUSES)
const deletableTxStates = [
TRANSACTION_STATUSES.CONFIRMED,
TRANSACTION_STATUSES.REJECTED,
TRANSACTION_STATUSES.FAILED,
TRANSACTION_STATUSES.DROPPED,
]
let nonDeletableCount = 0
let status
while (transactions.length <= 100) {
status = txStates[Math.floor(Math.random() * Math.floor(txStates.length - 1))]
// This is an old migration, let's allow it
// eslint-disable-next-line no-loop-func
if (!deletableTxStates.find((s) => s === status)) {
nonDeletableCount += 1
}
2019-12-03 21:50:55 +01:00
transactions.push({ status })
}
while (transactions40.length < 40) {
status = txStates[Math.floor(Math.random() * Math.floor(txStates.length - 1))]
2019-12-03 21:50:55 +01:00
transactions40.push({ status })
}
while (transactions20.length < 20) {
status = txStates[Math.floor(Math.random() * Math.floor(txStates.length - 1))]
2019-12-03 21:50:55 +01:00
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) {
2020-11-03 00:41:28 +01:00
migration23
.migrate(storage)
.then((migratedData) => {
let leftoverNonDeletableTxCount = 0
2020-11-03 00:41:28 +01:00
const migratedTransactions =
migratedData.data.TransactionController.transactions
migratedTransactions.forEach((tx) => {
if (!deletableTxStates.find((s) => s === tx.status)) {
leftoverNonDeletableTxCount += 1
}
})
2020-11-03 00:41:28 +01:00
assert.equal(
leftoverNonDeletableTxCount,
nonDeletableCount,
"migration shouldn't 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()
2020-11-03 00:41:28 +01:00
})
.catch(done)
})
2020-07-20 19:02:49 +02:00
it('should not remove any transactions because 40 is the expected limit', function (done) {
storage.meta.version = 22
storage.data.TransactionController.transactions = transactions40
2020-11-03 00:41:28 +01:00
migration23
.migrate(storage)
.then((migratedData) => {
2020-11-03 00:41:28 +01:00
const migratedTransactions =
migratedData.data.TransactionController.transactions
2020-11-03 00:41:28 +01:00
assert.equal(
migratedTransactions.length,
40,
"migration shouldn't delete when at limit",
)
done()
2020-11-03 00:41:28 +01:00
})
.catch(done)
})
2020-07-20 19:02:49 +02:00
it('should not remove any transactions because 20 txs is under the expected limit', function (done) {
storage.meta.version = 22
storage.data.TransactionController.transactions = transactions20
2020-11-03 00:41:28 +01:00
migration23
.migrate(storage)
.then((migratedData) => {
2020-11-03 00:41:28 +01:00
const migratedTransactions =
migratedData.data.TransactionController.transactions
assert.equal(
migratedTransactions.length,
20,
"migration shouldn't delete when under limit",
)
done()
2020-11-03 00:41:28 +01:00
})
.catch(done)
})
})