-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
98 lines (76 loc) · 2.45 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
88
89
90
91
92
93
94
95
96
97
98
var test = require('tap').test;
var _ = require('underscore');
var BBDelta = require('./');
function expectEvent(t, obj, name, block) {
var seen = false;
var handler = function() {
seen = true;
};
obj.on(name, handler);
block();
obj.off(name, handler);
t.ok(seen, 'expect event ' + name);
}
test('resetModel', function(t) {
t.plan(10);
var model = new BBDelta.Model();
expectEvent(t, model, 'change:foo', function() {
t.ok(model.reset({ foo: 5 }), "add first attribute");
});
expectEvent(t, model, 'change:bar', function() {
t.ok(model.reset({ foo: 5, bar: 8 }), "add second attribute");
});
expectEvent(t, model, 'change:foo', function() {
t.ok(model.reset({ bar: 8 }), "remove first attribute");
t.notOk(_.has(model.attributes, 'foo'), "attr should be deleted");
});
expectEvent(t, model, 'change:bar', function() {
model.sync = function(method, model, options) {
options.success(model, {}, options);
};
model.fetch({ reset: false });
t.ok(_.has(model.attributes, 'bar'), "fetch & set keeps attr");
model.fetch({ reset: true });
t.notOk(_.has(model.attributes, 'bar'), "fetch & reset deletes attr");
});
});
test('collectionDelta', function(t) {
t.plan(11);
var collection = new BBDelta.Collection();
expectEvent(t, collection, 'reset', function() {
t.ok(collection.delta([
{ id: 5 }
]), "add first model");
});
expectEvent(t, collection, 'add', function() {
t.ok(collection.delta([
{ id: 5 },
{ id: 8 }
]), "add second model");
});
expectEvent(t, collection, 'change', function() {
t.ok(collection.delta([
{ id: 5, foo: 'bar' },
{ id: 8 }
]), "change first model");
});
expectEvent(t, collection, 'remove', function() {
t.ok(collection.delta([
{ id: 8 }
]), "remove first model");
});
expectEvent(t, collection, 'reset', function() {
t.ok(collection.delta([
{ id: 3 }
]), "replace contents");
});
expectEvent(t, collection, 'add', function() {
collection.sync = function(method, collection, options) {
options.success(collection, [
{ id: 3 },
{ id: 7 }
], options);
};
collection.fetch({ delta: true });
});
});