1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-12 12:47:14 +01:00
metamask-extension/test/unit/migrations/migrator-test.js

69 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-05-11 19:47:58 +02:00
const assert = require('assert')
const clone = require('clone')
const Migrator = require('../../../app/scripts/lib/migrator/')
const liveMigrations = require('../../../app/scripts/migrations/')
const stubMigrations = [
2017-05-11 19:47:58 +02:00
{
version: 1,
migrate: (data) => {
// clone the data just like we do in migrations
const clonedData = clone(data)
clonedData.meta.version = 1
return Promise.resolve(clonedData)
},
},
{
version: 2,
migrate: (data) => {
const clonedData = clone(data)
clonedData.meta.version = 2
return Promise.resolve(clonedData)
},
},
{
version: 3,
migrate: (data) => {
const clonedData = clone(data)
clonedData.meta.version = 3
return Promise.resolve(clonedData)
},
},
]
2017-05-28 20:18:07 +02:00
const versionedData = {meta: {version: 0}, data: {hello: 'world'}}
const firstTimeState = {
meta: { version: 0 },
data: require('../../../app/scripts/first-time-state'),
}
2018-04-06 03:05:03 +02:00
describe('Migrator', () => {
const migrator = new Migrator({ migrations: stubMigrations })
2017-05-11 19:47:58 +02:00
it('migratedData version should be version 3', (done) => {
migrator.migrateData(versionedData)
.then((migratedData) => {
assert.equal(migratedData.meta.version, stubMigrations[2].version)
2017-05-11 19:47:58 +02:00
done()
}).catch(done)
})
it('should match the last version in live migrations', (done) => {
const migrator = new Migrator({ migrations: liveMigrations })
migrator.migrateData(firstTimeState)
.then((migratedData) => {
const last = liveMigrations.length - 1
assert.equal(migratedData.meta.version, liveMigrations[last].version)
done()
}).catch(done)
})
2018-04-06 04:28:53 +02:00
it('should emit an error', function (done) {
this.timeout(15000)
const migrator = new Migrator({ migrations: [{ version: 1, migrate: async () => { throw new Error('test') } } ] })
migrator.on('error', () => done())
migrator.migrateData({ meta: {version: 0} })
.then((migratedData) => {
}).catch(done)
})
2017-05-11 19:47:58 +02:00
})