-
Notifications
You must be signed in to change notification settings - Fork 8
/
DefaultLocalStateStore.js
79 lines (77 loc) · 3 KB
/
DefaultLocalStateStore.js
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
69
70
71
72
73
74
75
76
77
78
79
var fs = require('fs');
var assert = require('chai').assert;
var MigratProject = require('../lib/MigratProject.js');
var MigratState = require('../lib/MigratState.js');
var MigratStateStore = require('../lib/MigratStateStore.js');
var MigratMigration = require('../lib/MigratMigration.js');
var MockMigratMigration = require('./mocks/MockMigration.js');
var LocalStateStore = require('../lib/DefaultStateStores/LocalStateStore.js');
var randomTempStateFile = function(state) {
var stateFile = __dirname + '/temp/.migratdb.' + String(Math.random()).substring(2);
if (state) { fs.writeFileSync(stateFile, JSON.stringify(state), 'utf8'); }
return stateFile;
};
describe('DefaultStateStores/LocalStateStore', function() {
it('should return an instance of MigratStateStore', function() {
var project = new MigratProject({});
var store = LocalStateStore(project);
assert.instanceOf(store, MigratStateStore);
});
describe('.get()', function() {
it('should not error if state file doesn\'t exist yet', function(done) {
var project = new MigratProject({localState: './does/not/exist.json'});
var store = LocalStateStore(project);
store.get(function(err, state) {
assert.isNull(err);
assert.instanceOf(state, MigratState);
assert.equal(JSON.stringify(state.state), '{}');
done();
});
});
it('should return proper MigratState object', function(done) {
var stateFile = randomTempStateFile({'1414046792583-test.all.js': 1414046792583});
var project = new MigratProject({localState: stateFile});
var store = LocalStateStore(project);
store.get(function(err, state) {
var migration = new MigratMigration({filename: '1414046792583-test.all.js'});
assert.isNull(err);
assert.instanceOf(state, MigratState);
assert.isTrue(state.exists(migration));
done();
});
});
it('should return error if "localState" not defined in project config', function(done) {
var project = new MigratProject({});
var store = LocalStateStore(project);
store.get(function(err, state) {
assert.instanceOf(err, Error);
assert.match(err.message, /localState/);
done();
});
});
});
describe('.set()', function() {
it('should update state file', function(done) {
var stateFile = randomTempStateFile();
var project = new MigratProject({localState: stateFile});
var store = LocalStateStore(project);
var state = new MigratState({'1414046792583-set.all.js': 1414046792583});
store.set(state, function(err) {
assert.isNull(err);
var content = fs.readFileSync(stateFile, 'utf8');
assert.equal(content, '{"1414046792583-set.all.js":1414046792583}');
done();
});
});
it('should return error if "localState" not defined in project config', function(done) {
var project = new MigratProject({});
var store = LocalStateStore(project);
var state = new MigratState({'1414046792583-set.all.js': 1414046792583});
store.set(state, function(err, state) {
assert.instanceOf(err, Error);
assert.match(err.message, /localState/);
done();
});
});
});
});