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
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/* Copyright (c) 2009-2010, Canonical Ltd. All rights reserved. */
YUI({
base: '../../../../canonical/launchpad/icing/yui/',
filter: 'raw', combine: false, fetchCSS: false
}).use('test', 'console', 'lp.registry.milestonetable', function(Y) {
var milestonetable = Y.lp.registry.milestonetable;
var suite = new Y.Test.Suite("milestonetable Tests");
suite.add(new Y.Test.Case({
// Test the setup method.
name: 'setup',
_should: {
error: {
test_config_undefined: true,
test_config_property_undefined: true,
test_missing_tbody_is_an_error: true
}
},
setUp: function() {
this.tbody = Y.one('#milestone-rows');
},
tearDown: function() {
delete this.tbody;
milestonetable._milestone_row_uri_template = null;
milestonetable._tbody = null;
},
test_good_config: function() {
// Verify the config data is stored.
var config = {
milestone_row_uri_template: '/uri',
milestone_rows_id: '#milestone-rows'
};
milestonetable.setup(config);
Y.Assert.areSame(
config.milestone_row_uri_template,
milestonetable._milestone_row_uri_template);
Y.Assert.areSame(this.tbody, milestonetable._tbody);
},
test_config_undefined: function() {
// Verify an error is thrown if there is no config.
milestonetable.setup();
},
test_config_property_undefined: function() {
// Verify an error is thrown when the config is incomplete.
var config = {
milestone_row_uri_template: '/uri'
};
milestonetable.setup(config);
},
test_missing_tbody_is_an_error: function() {
// Verify an error is thrown when the id cannot be found.
var config = {
milestone_row_uri_template: '/uri',
milestone_rows_id: 'does-not-exist'
};
milestonetable.setup(config);
}
}));
suite.add(new Y.Test.Case({
// Test the _setup_milestone_event_data method.
name: '_setup_milestone_event_data',
setUp: function() {
this.tbody = Y.one('#milestone-rows');
},
tearDown: function() {
delete this.tbody;
},
test_data_state: function() {
// Verify the milestone name, target container, and callbacks.
var data = milestonetable._setup_milestone_event_data(
{name: '0.1'}, this.tbody);
Y.Assert.areEqual('0.1', data.name);
Y.Assert.areSame(this.tbody, data.tbody);
Y.Assert.areSame(
milestonetable._on_add_success, data.success_handle.sub.fn);
Y.Assert.areSame(
milestonetable._on_add_failure, data.failure_handle.sub.fn);
}
}));
suite.add(new Y.Test.Case({
// Test the _prepend_node method.
name: '_prepend_node',
setUp: function() {
this.parent_node = null;
this.child_node = Y.Node.create('<li>3</li>');
},
tearDown: function() {
delete this.parent_node;
delete this.child_node;
},
test_empty_container: function() {
// Verify that the child is added to the parent node.
this.parent_node = Y.Node.create('<ul></ul>');
milestonetable._prepend_node(this.parent_node, this.child_node);
Y.Assert.areSame(this.parent_node, this.child_node.ancestor());
},
test_non_empty_container: function() {
// Verify that the child is the first child in the parent node.
this.parent_node = Y.Node.create(
'<ul><li>2</l1><li>1</l1></ul>');
milestonetable._prepend_node(this.parent_node, this.child_node);
Y.Assert.areSame(this.child_node.ancestor(), this.parent_node);
var first_child = this.child_node.ancestor().get(
'children').item(0);
Y.Assert.areSame(this.child_node, first_child);
}
}));
suite.add(new Y.Test.Case({
// Test the _ensure_table_is_seen method.
name: '_ensure_table_is_seen',
setUp: function() {
this.table = null;
this.tbody = null;
},
tearDown: function() {
delete this.table;
delete this.tbody;
},
test_unseen_container: function() {
// Verify that the container's unseen class is removed.
this.table = Y.Node.create(
'<table class="listing unseen"><tbody></tbody></table>');
this.tbody = this.table.get('children').item(0);
milestonetable._ensure_table_is_seen(this.tbody);
Y.Assert.areEqual('listing', this.table.get('className'));
}
}));
suite.add(new Y.Test.Case({
// Test the _clear_add_handlers method.
name: '_clear_add_handlers',
setUp: function() {
this.data = milestonetable._setup_milestone_event_data(
{name: '0.1'}, Y.one('#milestone-rows'));
},
tearDown: function() {
delete this.data;
},
test_handlers_are_detached: function() {
// Verify the callbacks are detached.
// If this fails, multiple prepends will happen.
Y.Assert.isUndefined(this.data.success_handle.sub.deleted);
Y.Assert.isUndefined(this.data.failure_handle.sub.deleted);
milestonetable._clear_add_handlers(this.data);
Y.Assert.isTrue(this.data.success_handle.sub.deleted);
Y.Assert.isTrue(this.data.failure_handle.sub.deleted);
}
}));
suite.add(new Y.Test.Case({
// Test the _on_add_failure and _on_add_success callback methods.
name: '_on_add_<failure|success>',
setUp: function() {
this.failure = 'Could not retrieve milestone 0.1';
this.success = 'New milestone 0.1';
this.response = {
responseText: " <tr><td>" + this.success + "<td></tr> "};
this.data = milestonetable._setup_milestone_event_data(
{name: '0.1'}, Y.one('#milestone-rows'));
// Needed to reset the DOM.
this.table = this.data.tbody.ancestor();
this.tbody_markup = this.table.get('innerHTML');
},
tearDown: function() {
this.table.set('innerHTML', this.tbody_markup);
delete this.failure;
delete this.success;
delete this.response;
delete this.data;
delete this.table;
delete this.tbody_markup;
},
test_failure_message_prepended: function() {
// Verify the failure handler prepends the message.
milestonetable._on_add_failure('id', this.response, this.data);
var tr = this.data.tbody.get('children').item(0);
Y.Assert.areEqual(this.failure, tr.get('text'));
},
test_success_milestone_prepended: function() {
// Verify the success handler prepends the milestone.
milestonetable._on_add_success('id', this.response, this.data);
var tr = this.data.tbody.get('children').item(0);
Y.Assert.areEqual(this.success, tr.get('text'));
}
}));
suite.add(new Y.Test.Case({
// Test the get_milestone_row method.
name: 'get_milestone_row',
setUp: function() {
milestonetable.setup({
milestone_row_uri_template: '/path/{name}/+row',
milestone_rows_id: '#milestone-rows'
});
// Monkey patch Y.io to verify it is called.
this.original_io = Y.io;
var record = {uri: null};
this.record = record;
Y.io = function(uri) {
record.uri = uri;};
},
tearDown: function() {
Y.io = this.original_io;
delete milestonetable._milestone_row_uri_template;
delete milestonetable._tbody;
delete this.record;
},
test_get_milestone_row: function() {
// Verify the callbacks milestoneoverlay callback executes.
milestonetable.get_milestone_row({name: '0.1'});
Y.Assert.areEqual('/path/0.1/+row', this.record.uri);
}
}));
// Lock, stock, and two smoking barrels.
var handle_complete = function(data) {
status_node = Y.Node.create(
'<p id="complete">Test status: complete</p>');
Y.one('body').appendChild(status_node);
};
Y.Test.Runner.on('complete', handle_complete);
Y.Test.Runner.add(suite);
var console = new Y.Console({newestOnTop: false});
console.render('#log');
Y.on('domready', function() {
Y.Test.Runner.run();
});
});
|