aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit/migrations/migrator-test.js
blob: 3dcc5aff7971be9f3a37f9cf5c6625e57d92886c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const assert = require('assert')
const clone = require('clone')
const Migrator = require('../../../app/scripts/lib/migrator/')
const liveMigrations = require('../../../app/scripts/migrations/')
const stubMigrations = [
  {
    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)
    },
  },
]
const versionedData = {meta: {version: 0}, data: {hello: 'world'}}

const firstTimeState = {
  meta: { version: 0 },
  data: require('../../../app/scripts/first-time-state'),
}

describe('Migrator', () => {
  const migrator = new Migrator({ migrations: stubMigrations })
  it('migratedData version should be version 3', (done) => {
    migrator.migrateData(versionedData)
      .then((migratedData) => {
        assert.equal(migratedData.meta.version, stubMigrations[2].version)
        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)
  })

  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(() => {
      }).catch(done)
  })

})