forked from javve/list.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.tests.js
164 lines (149 loc) · 3.6 KB
/
list.tests.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
module("List.js standard");
var theList
, ryah = {
id: 4
, name: "Ryan Dahl"
, feature: "Node"
}
, tj = {
id: 5
, name: "TJ Holowaychuk"
, feature: "Node"
}
, jonny = {
id: 6
, name: "Jonny Strömberg"
, feature: "List.js"
};
test('Create List.js from existing list', function() {
var templates = {
valueNames: ['id', 'name', 'feature']
};
theList = new List('list', templates);
ok(true, "list created" );
});
test('Count items', function(){
equals(theList.size(), 3);
});
test('Add one item', function(){
theList.add(ryah);
equals(theList.size(), 4);
});
test('Add two items', function(){
theList.add([tj, jonny]);
equals(theList.size(), 6);
});
test('Remove one item', function() {
var found = theList.remove('id', 6);
equals(found, 1, "Item was note found");
equals(theList.size(), 5, 'List is not one item shorter');
});
test('Try remove one item that not exists', function() {
var found = theList.remove('id', 9);
equals(found, false, 'An item was found');
equals(theList.size(), 5, 'List are not 5 items long');
});
test('Get one item', function() {
var item = theList.get('id', 4);
deepEqual(item.values(), ryah);
});
test('Get two items', function() {
var items = theList.get('feature', 'Node');
items = [
items[0].values(),
items[1].values()
];
deepEqual(items, [ryah, tj], "Say woot");
});
test('Get item that doen not exist', function() {
var item = theList.get('id', 200);
equals(item, null);
});
test('Search', function() {
var items = theList.search('Node');
equals(items.length, 2);
items = [
items[0].values(),
items[1].values()
];
deepEqual(items, [ryah, tj]);
theList.search();
});
test('Search with null values', function() {
var objectWithNulls = {
id: 7
, name: null
, feature: null
};
try {
var items = theList.search('Node');
equals(items.length, 2);
items = [
items[0].values(),
items[1].values()
];
deepEqual(items, [ryah, tj]);
}
finally {
theList.search();
}
});
test('Search for 0', function() {
var objectWithZeros = {
id: 7
, name: "Node"
, feature: 0
};
theList.add(objectWithZeros);
try {
var items = theList.search(0);
equals(items.length, 1);
items = [
items[0].values()
];
deepEqual(items, [objectWithZeros]);
}
finally {
theList.search();
}
});
test('Search with undefined values', function() {
var objectWithZeros = {
id: 7
, name: "Node"
, feature: 0
};
var objectWithUndefined = {
id: 8
, name: "Node"
, feature: undefined
};
theList.add(objectWithUndefined);
try {
var items = theList.search(0);
equals(items.length, 1);
items = [
items[0].values()
];
deepEqual(items, [objectWithZeros]);
}
finally {
theList.search();
}
});
test('Filter', function() {
var visibleItems = theList.filter(function(item) {
if (+item.values().id < 3) {
return true;
} else {
return false;
}
});
equals(visibleItems.length, 2);
equals(visibleItems[0].values().id, 1);
equals(visibleItems[1].values().id, 2);
});
test('Restore from filter', function() {
var visibleItems = theList.filter();
equals(visibleItems.length, 7);
});