-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
87 lines (78 loc) · 3.25 KB
/
test.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
80
81
82
83
84
85
86
87
var vows = require('vows'),
assert = require('assert'),
dirtyable = require('./dirtyable.js');
vows.describe('Rails-like dirty behavior').addBatch({
'A dirtied object': {
topic: function() {
var object = {
foo: 'bar',
bar: 'I already said bar!',
// keep baz undefined.
arrayTest: [],
};
dirtyable.extend(object, ['foo', 'bar', 'baz', 'arrayTest']);
return object;
},
'should start with its original values': function(obj) {
assert.equal(obj.foo, 'bar');
assert.equal(obj.bar, 'I already said bar!');
assert.equal(typeof obj.baz, 'undefined');
},
'should unchanged': function(obj) {
assert.equal(obj.foo_isChanged, false);
assert.equal(obj.bar_isChanged, false);
assert.equal(obj.isChanged, false);
assert(Array.isArray(obj.changed));
assert.equal(obj.changed.length, 0);
assert.equal(typeof obj.changes, 'object');
assert.equal(Object.keys(obj.changes).length, 0);
assert.deepEqual(obj.changedProperties, {});
},
'should track changes via setter': function(obj) {
assert.equal(obj.foo_isChanged, false);
obj.foo = 'baz';
assert.equal(obj.foo_isChanged, true);
assert.equal(obj.bar_isChanged, false);
assert.equal(obj.foo_was, 'bar');
assert.deepEqual(obj.foo_change, ['bar', 'baz']);
assert.equal(obj.isChanged, true);
assert.deepEqual(obj.changed, ['foo']);
assert.deepEqual(obj.changes, {'foo': ['bar', 'baz']});
assert.deepEqual(obj.changedProperties, {'foo': 'bar'});
},
'should track changes via property_will_change': function(obj) {
// Normally, pushing does nothing
assert.equal(obj.arrayTest_isChanged, false);
obj.arrayTest.push(1);
assert.equal(obj.arrayTest_isChanged, false);
// But when we call will_change beforehand, it works!
obj.arrayTest_will_change();
obj.arrayTest.push(2);
assert.equal(obj.arrayTest_isChanged, true);
assert.deepEqual(obj.arrayTest_change, [[1], [1, 2]]);
},
'should be cleanable': {
topic: function(obj) {
obj.bar = 'changed_bar';
return obj;
},
'by setting a property to its original value': function(obj) {
assert.equal(obj.foo_isChanged, true);
obj.foo = 'bar';
assert.equal(obj.foo_isChanged, false);
},
'by calling reset_property': function(obj) {
assert.equal(obj.foo_isChanged, false);
obj.foo = 'new foo';
assert.equal(obj.foo_isChanged, true);
obj.reset_foo();
assert.equal(obj.foo_isChanged, false);
},
'by calling changedProperties.clear()': function(obj) {
assert.equal(obj.isChanged, true);
obj.changedProperties.clear();
assert.equal(obj.isChanged, false);
}
}
}
}).export(module);