1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/app/scripts/migrations/057.test.js

194 lines
4.8 KiB
JavaScript
Raw Normal View History

import { strict as assert } from 'assert';
import migration57 from './057';
describe('migration #57', function () {
it('should update the version metadata', async function () {
const oldStorage = {
meta: {
version: 56,
},
data: {},
};
const newStorage = await migration57.migrate(oldStorage);
assert.deepEqual(newStorage.meta, {
version: 57,
});
});
it('should transactions array into an object keyed by id', async function () {
const oldStorage = {
meta: {},
data: {
TransactionController: {
transactions: [
{
id: 0,
txParams: { foo: 'bar' },
},
{
id: 1,
txParams: { foo: 'bar' },
},
{
id: 2,
txParams: { foo: 'bar' },
},
{
id: 3,
txParams: { foo: 'bar' },
},
],
},
foo: 'bar',
},
};
const newStorage = await migration57.migrate(oldStorage);
assert.deepEqual(newStorage.data, {
TransactionController: {
transactions: {
0: {
id: 0,
txParams: { foo: 'bar' },
},
1: {
id: 1,
txParams: { foo: 'bar' },
},
2: {
id: 2,
txParams: { foo: 'bar' },
},
3: { id: 3, txParams: { foo: 'bar' } },
},
},
foo: 'bar',
});
});
it('should handle transactions without an id, just in case', async function () {
const oldStorage = {
meta: {},
data: {
TransactionController: {
transactions: [
{
id: 0,
txParams: { foo: 'bar' },
},
{
txParams: { foo: 'bar' },
},
{
txParams: { foo: 'bar' },
},
{
txParams: { foo: 'bar' },
},
],
},
foo: 'bar',
},
};
const newStorage = await migration57.migrate(oldStorage);
const expectedTransactions = {};
for (const transaction of Object.values(
newStorage.data.TransactionController.transactions,
)) {
// Make sure each transaction now has an id.
assert.ok(
typeof transaction.id !== 'undefined',
'transaction id is undefined',
);
// Build expected transaction object
expectedTransactions[transaction.id] = transaction;
}
// Ensure that we got the correct number of transactions
assert.equal(
Object.keys(expectedTransactions).length,
oldStorage.data.TransactionController.transactions.length,
);
// Ensure that the one transaction with id is preserved, even though it is
// a falsy id.
assert.equal(newStorage.data.TransactionController.transactions[0].id, 0);
});
it('should not blow up if transactions are not an array', async function () {
const storageWithTransactionsAsString = {
meta: {},
data: {
TransactionController: {
transactions: 'someone might have weird state in the future',
},
},
};
const storageWithTransactionsAsArrayOfString = {
meta: {},
data: {
TransactionController: {
transactions: 'someone might have weird state in the future'.split(
'',
),
},
},
};
const result1 = await migration57.migrate(storageWithTransactionsAsString);
const result2 = await migration57.migrate(
storageWithTransactionsAsArrayOfString,
);
assert.deepEqual(storageWithTransactionsAsString.data, result1.data);
assert.deepEqual(storageWithTransactionsAsArrayOfString.data, result2.data);
});
it('should do nothing if transactions state does not exist', async function () {
const oldStorage = {
meta: {},
data: {
TransactionController: {
bar: 'baz',
},
foo: 'bar',
},
};
const newStorage = await migration57.migrate(oldStorage);
assert.deepEqual(oldStorage.data, newStorage.data);
});
it('should convert empty array into empty object', async function () {
const oldStorage = {
meta: {},
data: {
TransactionController: {
transactions: [],
bar: 'baz',
},
foo: 'bar',
},
};
const newStorage = await migration57.migrate(oldStorage);
assert.deepEqual(newStorage.data, {
TransactionController: {
transactions: {},
bar: 'baz',
},
foo: 'bar',
});
});
it('should do nothing if state is empty', async function () {
const oldStorage = {
meta: {},
data: {},
};
const newStorage = await migration57.migrate(oldStorage);
assert.deepEqual(oldStorage.data, newStorage.data);
});
});