forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest.widget.listView.js
More file actions
245 lines (211 loc) · 8.9 KB
/
Copy pathtest.widget.listView.js
File metadata and controls
245 lines (211 loc) · 8.9 KB
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
(function() {
var listView = function(args) {
var basicArgs = {
listView: {
fields: {},
dataProvider: function() {}
}
};
return $('<div>').listView(
$.extend(true, {}, basicArgs, args)
).find('.list-view');
};
module('List view', {
setup: function() {
window.pageSize = 20;
},
teardown: function() {
delete window.pageSize;
}
});
test('Basic', function() {
var $listView = listView();
var $toolbar = $listView.find('> .toolbar');
var $table = $listView.find('> .data-table');
equal($listView.length, 1, 'List view present');
equal($toolbar.length, 1, 'Toolbar present');
equal($table.length, 1, 'Data table div present');
equal($table.find('> .fixed-header table thead tr').length, 1, 'Fixed header present');
equal($table.find('> table.body tbody').length, 1, 'Body table present');
});
test('Fields: basic', function() {
var $listView = listView({
listView: {
fields: {
fieldA: { label: 'TestFieldA' }
}
}
});
var $fields = $listView.find('.fixed-header table thead tr th');
equal($fields.length, 1, 'Column present');
ok($fields.hasClass('fieldA'), 'Has ID as classname');
equal($fields.html(), 'TestFieldA', 'Has correct label');
});
test('Fields: n columns', function() {
var testFields = {
fieldA: { label: 'TestFieldA' },
fieldB: { label: 'TestFieldB' },
fieldC: { label: 'TestFieldC' }
};
var $listView = listView({
listView: {
fields: testFields
}
});
var $fields = $listView.find('.fixed-header table thead tr th');
$.each(testFields, function(k, v) {
var $field = $fields.filter('.' + k);
equal($field.length, 1, k + '-> Column present');
equal($field.html(), v.label, k + '-> Has correct label');
});
});
test('Data loading state', function() {
var $listView = listView();
equal($listView.find('table.body tr.loading').length, 1, 'Row has loading state');
equal($listView.find('table.body tr.loading td.loading.icon').length, 1, 'Row cell has loading icon');
});
asyncTest('Data provider: basic', function() {
expect(3);
var $listView = listView({
listView: {
fields: {
fieldA: { label: 'TestFieldA' },
fieldB: { label: 'TestFieldB' }
},
dataProvider: function(args) {
args.response.success({ data: [] });
ok(true, 'Data provider called');
start();
}
}
});
equal($listView.find('.data-table table.body tbody tr.empty td').length, 1, 'Body table has empty table row');
equal($listView.find('.data-table table.body tbody tr.empty td').html(), 'label.no.data', 'Empty contents notice displayed');
});
asyncTest('Data provider: load data', function() {
var $listView = listView({
listView: {
fields: {
fieldA: { label: 'TestFieldA' },
fieldB: { label: 'TestFieldB' }
},
dataProvider: function(args) {
args.response.success({
data: [
{ fieldA: 'FieldDataA', fieldB: 'FieldDataB' }
]
});
start();
}
}
});
equal($listView.find('table.body tbody tr').length, 1, 'Body table has table row');
equal($listView.find('table.body tbody tr td').length, 2, 'Body table has table cells');
equal($listView.find('table.body tbody tr td.fieldA > span').html(), 'FieldDataA', 'FieldDataA content present');
equal($listView.find('table.body tbody tr td.fieldB > span').html(), 'FieldDataB', 'FieldDataB content present');
});
asyncTest('Data provider: multiple rows of data', function() {
var testData = [
{ fieldA: 'FieldDataA1', fieldB: 'FieldDataB1' },
{ fieldA: 'FieldDataA2', fieldB: 'FieldDataB2' },
{ fieldA: 'FieldDataA3', fieldB: 'FieldDataB3' }
];
var $listView = listView({
listView: {
fields: {
fieldA: { label: 'TestFieldA' },
fieldB: { label: 'TestFieldB' }
},
dataProvider: function(args) {
args.response.success({
data: testData
});
start();
}
}
});
equal($listView.find('table.body tbody tr').length, 3, 'Body table has correct # of table rows');
$(testData).map(function(index, data) {
var $tr = $listView.find('table.body tbody tr').filter(function() {
return $(this).index() === index;
});
equal($tr.find('td.fieldA > span').html(), 'FieldDataA' + (index + 1), 'FieldDataA' + (index + 1) + ' present');
equal($tr.find('td.fieldB > span').html(), 'FieldDataB' + (index + 1), 'FieldDataB' + (index + 1) + ' present');
});
});
test('Field pre-filter', function() {
var $listView = listView({
listView: {
fields: {
fieldA: { label: 'TestFieldA' },
fieldB: { label: 'TestFieldB' },
fieldHidden: { label: 'TestFieldHidden' }
},
preFilter: function(args) {
return ['fieldHidden'];
},
dataProvider: function(args) {
args.response.success({
data: [
{ fieldA: 'FieldDataA', fieldB: 'FieldDataB', fieldHidden: 'FieldDataHidden' }
]
});
start();
}
}
});
equal($listView.find('table tr th').length, 2, 'Correct number of header columns present');
equal($listView.find('table.body tbody tr td').length, 2, 'Correct number of data body columns present');
ok(!$listView.find('table.body tbody td.fieldHidden').length, 'Hidden field not present');
});
test('Filter dropdown', function() {
var $listView = listView({
listView: {
fields: {
state: { label: 'State' }
},
filters: {
on: { label: 'FilterOnLabel' },
off: { label: 'FilterOffLabel' }
},
dataProvider: function(args) {
var filterBy = args.filterBy.kind;
var data = filterBy === 'on' ? [{ state: 'on' }] : [{ state: 'off' }];
args.response.success({
data: data
});
start();
}
}
});
var $filters = $listView.find('.filters select');
var testFilterDropdownContent = function() {
equal($filters.find('option').length, 2, 'Correct # of filters present');
equal($filters.find('option:first').html(), 'FilterOnLabel', 'Filter on label present');
equal($filters.find('option:last').html(), 'FilterOffLabel', 'Filter off label present');
};
testFilterDropdownContent();
equal($filters.find('option').val(), 'on', 'Correct default filter active');
equal($listView.find('tbody td.state span').html(), 'on', '"on" data item visible');
ok($filters.val('off').trigger('change'), 'Change filter to "off"');
equal($listView.find('tbody td.state span').html(), 'off', '"off" data item visible');
equal($filters.val(), 'off', 'Correct filter active');
testFilterDropdownContent();
});
}());