forked from almende/vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataView.test.js
208 lines (175 loc) · 5.54 KB
/
DataView.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
var assert = require('assert');
var vis = require('../dist/vis');
var moment = vis.moment;
var DataSet = vis.DataSet;
var DataView = vis.DataView;
// TODO: test the source code immediately, but this is ES6
// TODO: improve DataView tests, split up in one test per function
describe('DataView', function () {
it('should work', function () {
var groups = new DataSet();
// add items with different groups
groups.add([
{id: 1, content: 'Item 1', group: 1},
{id: 2, content: 'Item 2', group: 2},
{id: 3, content: 'Item 3', group: 2},
{id: 4, content: 'Item 4', group: 1},
{id: 5, content: 'Item 5', group: 3}
]);
var group2 = new DataView(groups, {
filter: function (item) {
return item.group == 2;
}
});
// test getting the filtered data
assert.deepEqual(group2.get(), [
{id: 2, content: 'Item 2', group: 2},
{id: 3, content: 'Item 3', group: 2}
]);
assert.equal(group2.length, 2);
// test filtering the view contents
assert.deepEqual(group2.get({
filter: function (item) {
return item.id > 2;
}
}), [
{id: 3, content: 'Item 3', group: 2}
]);
// test event subscription
var groupsTriggerCount = 0;
groups.on('*', function () {
groupsTriggerCount++;
});
var group2TriggerCount = 0;
group2.on('*', function () {
group2TriggerCount++;
});
groups.update({id:2, content: 'Item 2 (changed)'});
assert.equal(groupsTriggerCount, 1);
assert.equal(group2TriggerCount, 1);
assert.equal(group2.length, 2);
groups.update({id:5, content: 'Item 5 (changed)'});
assert.equal(groupsTriggerCount, 2);
assert.equal(group2TriggerCount, 1);
assert.equal(group2.length, 2);
// detach the view from groups
group2.setData(null);
assert.equal(groupsTriggerCount, 2);
assert.equal(group2TriggerCount, 2);
assert.equal(group2.length, 0);
groups.update({id:2, content: 'Item 2 (changed again)'});
assert.equal(groupsTriggerCount, 3);
assert.equal(group2TriggerCount, 2);
// test updating of .length property
group2.setData(groups);
assert.equal(group2.length, 2);
// add a new item
groups.add({id: 6, content: 'Item 6', group: 2});
assert.equal(group2.length, 3);
// change an items group to 2
groups.update({id: 4, group: 2});
assert.equal(group2.length, 4);
// change an items group to 1
groups.update({id: 4, group: 1});
assert.equal(group2.length, 3);
// remove an item
groups.remove(2);
assert.equal(group2.length, 2);
// remove all items
groups.clear();
assert.equal(group2.length, 0);
});
it('should refresh a DataView with filter', function () {
var data = new DataSet([
{id:1, value:2},
{id:2, value:4},
{id:3, value:7}
]);
var threshold = 5;
// create a view. The view has a filter with a dynamic property `threshold`
var view = new DataView(data, {
filter: function (item) {
return item.value < threshold;
}
});
var added, updated, removed;
view.on('add', function (event, props) {added = added.concat(props.items)});
view.on('update', function (event, props) {updated = updated.concat(props.items)});
view.on('remove', function (event, props) {removed = removed.concat(props.items)});
assert.deepEqual(view.get(), [
{id:1, value:2},
{id:2, value:4}
]);
// change the threshold to 3
added = [];
updated = [];
removed = [];
threshold = 3;
view.refresh();
assert.deepEqual(view.get(), [{id:1, value:2}]);
assert.deepEqual(added, []);
assert.deepEqual(updated, []);
assert.deepEqual(removed, [2]);
// change threshold to 8
added = [];
updated = [];
removed = [];
threshold = 8;
view.refresh();
assert.deepEqual(view.get(), [
{id:1, value:2},
{id:2, value:4},
{id:3, value:7}
]);
assert.deepEqual(added, [2, 3]);
assert.deepEqual(updated, []);
assert.deepEqual(removed, []);
});
it('should pass data of changed items when updating a DataSet', function () {
var data = new DataSet([
{id: 1, title: 'Item 1', group: 1},
{id: 2, title: 'Item 2', group: 2},
{id: 3, title: 'Item 3', group: 2}
]);
var view = new DataView(data, {
filter: function (item) {
return item.group === 2;
}
});
var dataUpdates = [];
var viewUpdates = [];
data.on('update', function (event, properties, senderId) {
dataUpdates.push([event, properties]);
});
view.on('update', function (event, properties, senderId) {
viewUpdates.push([event, properties]);
});
// make a change not affecting the DataView
data.update({id: 1, title: 'Item 1 (changed)'});
assert.deepEqual(dataUpdates, [
['update', {
items: [1],
data: [{id: 1, title: 'Item 1 (changed)'}],
oldData: [{"group": 1, "id": 1, "title": "Item 1"}]
}]
]);
assert.deepEqual(viewUpdates, []);
// make a change affecting the DataView
data.update({id: 2, title: 'Item 2 (changed)'});
assert.deepEqual(dataUpdates, [
['update', {
items: [1],
data: [{id: 1, title: 'Item 1 (changed)'}],
oldData: [{"group": 1, "id": 1, "title": "Item 1"}]
}],
['update', {
items: [2],
data: [{id: 2, title: 'Item 2 (changed)'}],
oldData: [{"group": 2, "id": 2, "title": "Item 2"}]
}]
]);
assert.deepEqual(viewUpdates, [
['update', {items: [2], data: [{id: 2, title: 'Item 2 (changed)'}]}]
]);
});
});