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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
/**
* ownCloud - News app
*
* @author Bernhard Posselt
* Copyright (c) 2012 - Bernhard Posselt <nukeawhale@gmail.com>
*
* This file is licensed under the Affero General Public License version 3 or later.
* See the COPYING-README file
*
*/
var News = News || {};
var NewsTests = NewsTests || {};
// variables with are used again
NewsTests.jsonStruct = [
{
id: 1,
title: 'hi',
type: News.MenuNodeType.Folder,
unreadCount: 1,
children: [
{
id: 2,
title: 'hi too',
type: News.MenuNodeType.Feed,
unreadCount: 4,
children: [],
icon: 'img/testing.png'
},
{
id: 3,
title: 'hi 3',
type: News.MenuNodeType.Feed,
children: [],
unreadCount: 3,
},
{
id: 2,
title: 'hi 3',
type: News.MenuNodeType.Folder,
children: [],
unreadCount: 13,
},
]
},
{
id: 4,
title: 'hi 4',
type: News.MenuNodeType.Feed,
children: [],
unreadCount: 1,
},
{
id: 5,
title: 'hi 4',
type: News.MenuNodeType.Feed,
children: [],
unreadCount: 50,
},
];
QUnit.testStart(function(){
NewsTests.menu = new News.Menu('feed_menu');
NewsTests.popMenu = new News.Menu('feed_menu');
NewsTests.popMenu.populateFromJSON(NewsTests.jsonStruct, NewsTests.popMenu);
// empty menu
NewsTests.$menuContainer = $('<div>');
NewsTests.$menuContainer.append(NewsTests.menu.render());
NewsTests.$menuDomElem = NewsTests.$menuContainer.children('ul');
// populated menu
NewsTests.$popMenuContainer = $('<div>');
NewsTests.$popMenuContainer.append(NewsTests.popMenu.render());
NewsTests.$popMenuDomElem = NewsTests.$popMenuContainer.children('ul');
var menuData = {
title: 'this is rad',
unreadCount: 111,
};
NewsTests.node = new News.MenuNode(News.MenuNodeType.Feed, 6, menuData);
});
/**
* Empty menu tests
*/
test('Empty menu should have certain variables', function(){
equal(NewsTests.menu._children.length, 0);
equal(NewsTests.menu._class, 'feed_menu');
equal(NewsTests.menu._parent, false);
equal(NewsTests.menu.getSize(), 0);
});
test('Empty menu dom should have certain dom elements', function(){
equal(NewsTests.$menuContainer.length, 1);
ok(NewsTests.$menuDomElem.hasClass('feed_menu'));
equal(NewsTests.$menuDomElem.children().length, 0);
});
/**
* Find node tests
*/
test('Finding a folder node should succeed', function(){
var node = NewsTests.popMenu._findNode(News.MenuNodeType.Folder, 1);
ok(node !== undefined);
});
test('Finding a nested folder node should succeed', function(){
var node = NewsTests.popMenu._findNode(News.MenuNodeType.Folder, 2);
ok(node !== undefined);
});
test('Finding a folder node that doesnt exist should fail', function(){
var node = NewsTests.popMenu._findNode(News.MenuNodeType.Folder, 10);
ok(node === undefined);
});
test('Finding a feed node should succeed', function(){
var node = NewsTests.popMenu._findNode(News.MenuNodeType.Feed, 4);
ok(node !== undefined);
});
test('Finding a feed node that doesnt exist should fail', function(){
var node = NewsTests.popMenu._findNode(News.MenuNodeType.Feed, 10);
ok(node === undefined);
});
/**
* Adding nodes test
*/
test('Adding a node should set children and parent correctly', function(){
NewsTests.menu._addChildNode(NewsTests.node);
equal(NewsTests.node._parent, NewsTests.menu);
ok(NewsTests.menu._children.indexOf(NewsTests.node) !== -1);
equal(NewsTests.menu.getSize(), 1);
});
/**
* Creating nodes tests
*/
test('Adding a new node should create the correct dom', function(){
NewsTests.menu.createNode(News.MenuNodeType.Folder, 0, NewsTests.node);
equal(NewsTests.$menuDomElem.children().length, 1);
});
/**
* Removing nodes tests
*/
test('Removing a feed node should remove the correct dom', function(){
equal(NewsTests.$popMenuDomElem.children().length, 3);
var child = NewsTests.popMenu.removeNode(News.MenuNodeType.Feed, 2);
ok(child !== undefined);
// top length should stay the same
equal(NewsTests.$popMenuDomElem.children().length, 3);
equal(NewsTests.popMenu._children.length, 3);
// but size should have been reduced by 1
equal(NewsTests.popMenu.getSize(), 5);
});
test('Removing a non existent node should not change anything', function(){
var child = NewsTests.popMenu.removeNode(News.MenuNodeType.Feed, 12);
ok(child === undefined);
// top length should stay the same
equal(NewsTests.$popMenuDomElem.children().length, 3);
equal(NewsTests.popMenu._children.length, 3);
equal(NewsTests.popMenu.getSize(), 6);
});
test('Removing a parent node should remove its children', function(){
var child = NewsTests.popMenu.removeNode(News.MenuNodeType.Folder, 1);
ok(child !== undefined);
equal(NewsTests.$popMenuDomElem.children().length, 2);
equal(NewsTests.popMenu._children.length, 2);
equal(NewsTests.popMenu.getSize(), 2);
});
/**
* Updating nodes tests
*/
test('Updating a node should update the object', function(){
var data = {
unreadCount: 112,
title: 'no way',
icon: 'test.png'
};
NewsTests.popMenu.updateNode(News.MenuNodeType.Feed, 2, data);
var node = NewsTests.popMenu._findNode(News.MenuNodeType.Feed, 2);
equal(node._unreadCount, data.unreadCount);
equal(node._title, data.title);
equal(node._icon, data.icon);
});
test('Updating a node should update the dom', function(){
var data = {
unreadCount: 112,
title: 'no way',
icon: 'test.png'
};
NewsTests.popMenu.updateNode(News.MenuNodeType.Feed, 2, data);
var node = NewsTests.popMenu._findNode(News.MenuNodeType.Feed, 2);
equal(node._$htmlElement.children('.title').html(), data.title);
//equal(node._$htmlElement.css('background-image'), data.icon); this fails in firefox, dunno why
});
/**
* Unread count tests
*/
test('Setting an unread count of 0 should add the all_read class', function(){
NewsTests.popMenu.updateNode(News.MenuNodeType.Folder, 1, {unreadCount: 0});
var node = NewsTests.popMenu._findNode(News.MenuNodeType.Folder, 1);
equal(node._unreadCount, 0);
ok(node._$htmlElement.hasClass('all_read'));
});
test('Setting an unread count of 0 and then 12 should remove the all_read and hidden class', function(){
NewsTests.popMenu.updateNode(News
|