~launchpad-pqm/launchpad/devel

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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
YUI({
    base: '../../../../canonical/launchpad/icing/yui/',
    filter: 'raw', combine: false, fetchCSS: false
    }).use('test', 'console', 'lp.bugs.filebug_dupefinder',
        'node-event-simulate', function(Y) {

var suite = new Y.Test.Suite("lp.bugs.filebug_dupefinder Tests");
var module = Y.lp.bugs.filebug_dupefinder;

/*
 * A wrapper for the Y.Event.simulate() function.  The wrapper accepts
 * CSS selectors and Node instances instead of raw nodes.
 */
function simulate(widget, selector, evtype, options) {
    var node_to_use = widget;
    if (selector !== undefined) {
        node_to_use = widget.one(selector);
    }
    var rawnode = Y.Node.getDOMNode(node_to_use);
    Y.Event.simulate(rawnode, evtype, options);
}

/**
 * A stub io handler.
 */
function IOStub(test_case){
    if (!(this instanceof IOStub)) {
        throw new Error("Constructor called as a function");
    }
    this.calls = [];
    this.io = function(url, config) {
        this.calls.push(url);
        var response = {responseText: ''};
        // We may have been passed text to use in the response.
        if (Y.Lang.isValue(arguments.callee.responseText)) {
            response.responseText = arguments.callee.responseText;
        }
        // We currently only support calling the success handler.
        config.on.success(undefined, response, arguments.callee.args);
        // After calling the handler, resume the test.
        if (Y.Lang.isFunction(arguments.callee.doAfter)) {
            test_case.resume(arguments.callee.doAfter);
        }
    };
}

suite.add(new Y.Test.Case({
    name: 'Test filebug form manipulation.',

    setUp: function() {
        // Reset the HTML elements.
        Y.one("#possible-duplicates").set('innerHTML', '');
        Y.one(Y.DOM.byId('field.comment')).set('value', '');
        var node = Y.one(Y.DOM.byId('field.search'));
        if (node !== null) {
            node.set('value', '');
        }
        node = Y.one(Y.DOM.byId('field.title'));
        if (node !== null) {
            node.set('value', '');
        }
        Y.one('#filebug-form-container').addClass('transparent')
                .setStyles({opacity: '0', display: 'none'});

        this.config = {};
        this.config.yio = new IOStub(this);
        module.setup_config(this.config);
    },

    tearDown: function() {
        Y.one('#filebug-form').set(
                'action', 'https://bugs.launchpad.dev/foo/+filebug');
    },

    /**
     * Some helper functions
     */
    selectNode: function(node, selector) {
        if (!Y.Lang.isValue(node)) {
            node = Y.one('#test-root');
        }
        var node_to_use = node;
        if (Y.Lang.isValue(selector)) {
            node_to_use = node.one(selector);
        }
        return node_to_use;
    },

    assertIsVisible: function(node, selector) {
        node = this.selectNode(node, selector);
        Y.Assert.areNotEqual('none', node.getStyle('display'));
    },

    assertIsNotVisible: function(node, selector) {
        node = this.selectNode(node, selector);
        Y.Assert.areEqual('none', node.getStyle('display'));
    },

    assertNodeText: function(node, selector, text) {
        node = this.selectNode(node, selector);
        Y.Assert.areEqual(text, node.get('innerHTML'));
    },


    /**
     * A user first searches for duplicate bugs. If there are no duplicates
     * the file bug form should be visible for bug details to be entered.
     */
    test_no_dups_search_shows_filebug_form: function() {
        // filebug container should not initially be visible
        this.assertIsNotVisible(null, '#filebug-form-container');
        var search_text = Y.one(Y.DOM.byId('field.search'));
        search_text.set('value', 'foo');
        var search_button = Y.one(Y.DOM.byId('field.actions.search'));
        // The search button should initially say 'Next'
        Y.Assert.areEqual('Next', search_button.get('value'));
        this.config.yio.io.responseText = 'No similar bug reports.';
        this.config.yio.io.doAfter = function() {
            // Check the expected io calls have been made.
            Y.ArrayAssert.itemsAreEqual(
                ['https://bugs.launchpad.dev/' +
                 'foo/+filebug-show-similar?title=foo'],
                this.config.yio.calls);
            // filebug container should be visible after the dup search
            this.assertIsVisible(null, '#filebug-form-container');
            var dups_node = Y.one("#possible-duplicates");
            this.assertNodeText(
                    dups_node, undefined, 'No similar bug reports.');
        };
        simulate(search_button, undefined, 'click');
        this.wait();
    },

    /**
     * A user first searches for duplicate bugs. If there are duplicates
     * the dups should be listed and the file bug form should not be visible.
     */
    test_dups_search_shows_dup_info: function() {
        // filebug container should not initially be visible
        this.assertIsNotVisible(null, '#filebug-form-container');
        var search_text = Y.one(Y.DOM.byId('field.search'));
        search_text.set('value', 'foo');
        var search_button = Y.one(Y.DOM.byId('field.actions.search'));
        this.config.yio.io.responseText = ([
                '<table><tr><td id="bug-details-expander" ',
                'class="bug-already-reported-expander"></td></tr></table>',
                '<input type="button" value="No, I need to report a new bug"',
                ' name="field.bug_already_reported_as"',
                ' id="bug-not-already-reported" style="display: block">',
                ].join(''));
        this.config.yio.io.doAfter = function() {
            // filebug container should not be visible when there are dups
            this.assertIsNotVisible(null, '#filebug-form-container');
            // we should have a 'new bug' button
            this.assertIsVisible(null, '#bug-not-already-reported');
            // The search button should say 'Check again'
            Y.Assert.areEqual('Check again', search_button.get('value'));
        };
        simulate(search_button, undefined, 'click');
        this.wait();
    },

    /**
     * A user first searches for duplicate bugs. They can start typing in some
     * detail. They can search again for dups and their input should be
     * retained.
     */
    test_dups_search_retains_user_input_when_no_dups: function() {
        // filebug container should not initially be visible
        this.assertIsNotVisible(null, '#filebug-form-container');
        var search_text = Y.one(Y.DOM.byId('field.search'));
        search_text.set('value', 'foo');
        var search_button = Y.one(Y.DOM.byId('field.actions.search'));
        this.config.yio.io.responseText = 'No similar bug reports.';
        this.config.yio.io.doAfter = function() {
            var comment_text = Y.one(Y.DOM.byId('field.comment'));
            comment_text.set('value', 'an error occurred');
            this.config.yio.io.doAfter = function() {
                // The user input should be retained
                Y.Assert.areEqual(
                    'an error occurred', comment_text.get('value'));
            };
            simulate(search_button, undefined, 'click');
            this.wait();
        };
        simulate(search_button, undefined, 'click');
        this.wait();
    },

    /**
     * A user first searches for duplicate bugs and there are none.
     * They can start typing in some detail. They can search again for dups
     * and their input should be retained even when there are dups and they
     * have to click the "No, this is a new bug" button.
     */
    test_dups_search_retains_user_input_when_dups: function() {
        // filebug container should not initially be visible
        this.assertIsNotVisible(null, '#filebug-form-container');
        var search_text = Y.one(Y.DOM.byId('field.search'));
        search_text.set('value', 'foo');
        var search_button = Y.one(Y.DOM.byId('field.actions.search'));
        this.config.yio.io.responseText = 'No similar bug reports.';
        this.config.yio.io.doAfter = function() {
            var comment_text = Y.one(Y.DOM.byId('field.comment'));
            comment_text.set('value', 'an error occurred');
            this.config.yio.io.responseText = ([
                    '<img id="bug-details-expander" ',
                    'class="bug-already-reported-expander" ',
                    'src="/@@/treeCollapsed">',
                    '<input type="button" value="No, I need to report a bug"',
                    ' name="field.bug_already_reported_as"',
                    ' id="bug-not-already-reported" style="display: block">'
                    ].join(''));
            this.config.yio.io.doAfter = function() {
                var new_bug_button = Y.one('#bug-not-already-reported');
                simulate(new_bug_button, undefined, 'click');
                // filebug container should be visible
                this.assertIsVisible(null, '#filebug-form-container');
                // The user input should be retained
                Y.Assert.areEqual(
                    'an error occurred', comment_text.get('value'));
            };
            simulate(search_button, undefined, 'click');
            this.wait();
        };
        simulate(search_button, undefined, 'click');
        this.wait();
    },

    /**
     * The filebug form url is correctly set when the page loads.
     */
    test_project_initial_filebug_form_action: function() {
        Y.Assert.areEqual(
            'https://bugs.launchpad.dev/foo/+filebug',
            Y.one('#filebug-form').get('action'));
    },

    add_project_selector: function() {
        var project_selector = Y.Node.create([
        '<tr>',
        '    <td>',
        '        <label for="field.product">Project:</label>',
        '        <select size="1" name="field.product" id="field.product">',
        '            <option value="foo">Foo</option>',
        '            <option value="bar">Bar</option>',
        '        </select>',
        '    </td>',
        '</tr>'
        ].join(''));
        Y.one('#search-field').insert(project_selector, 'before');
        module.setup_product_urls();
    },

    /**
     * The filebug form url is correctly updated when the project changes.
     */
    test_project_change_filebug_form_action: function() {
        this.add_project_selector();
        var project = Y.one(Y.DOM.byId('field.product'));
        project.set('value', 'bar');
        simulate(project, undefined, 'change');
        Y.Assert.areEqual(
            'https://bugs.launchpad.dev/bar/+filebug',
            Y.one('#filebug-search-form').get('action'));
    },

    /**
     * A user first searches for duplicate bugs and there are none.
     * They can start typing in some detail. They change the project and
     * perform a new search. Their input should be retained.
     */
    test_project_change_retains_user_input_after_dups_serach: function() {
        Y.one(Y.DOM.byId('field.product')).set('value', 'foo');
        module.setup_product_urls();
        // filebug container should not initially be visible
        this.assertIsNotVisible(null, '#filebug-form-container');
        var search_text = Y.one(Y.DOM.byId('field.search'));
        search_text.set('value', 'foo');
        var search_button = Y.one(Y.DOM.byId('field.actions.search'));
        this.config.yio.io.responseText = 'No similar bug reports.';
        this.config.yio.io.doAfter = function() {
            var comment_text = Y.one(Y.DOM.byId('field.comment'));
            comment_text.set('value', 'an error occurred');

            this.config.yio.io.responseText = 'Bug filing details';
            var project = Y.one(Y.DOM.byId('field.product'));
            project.set('value', 'bar');
            simulate(project, undefined, 'change');
            // filebug container should be visible
            this.assertIsVisible(null, '#filebug-form-container');

            // Search button should day 'Check again' because we have already
            // done a search.
            var search_button = (Y.one(Y.DOM.byId('field.actions.search')));
            Y.Assert.areEqual('Check again', search_button.get('value'));

            this.config.yio.io.responseText = 'No similar bug reports.';
            this.config.yio.io.doAfter = function() {
                // filebug container should be visible
                this.assertIsVisible(null, '#filebug-form-container');
                // The user input should be retained
                Y.Assert.areEqual(
                    'an error occurred', comment_text.get('value'));
                Y.ArrayAssert.itemsAreEqual(
                        ['https://bugs.launchpad.dev/' +
                         'foo/+filebug-show-similar?title=foo',
                         'https://bugs.launchpad.dev/' +
                         'bar/+filebug-show-similar?title=foo'],
                        this.config.yio.calls);
            };
            simulate(search_button, undefined, 'click');
            this.wait();
        };
        simulate(search_button, undefined, 'click');
        this.wait();
    }

}));

var handle_complete = function(data) {
    window.status = '::::' + JSON.stringify(data);
    };
Y.Test.Runner.on('complete', handle_complete);
Y.Test.Runner.add(suite);

var console = new Y.Console({newestOnTop: false});
console.render('#log');

// Configure the javascript module under test. In production, the
// setup_dupe_finder() is called from the page template. We need to pass in
// a stub io handler here so that the XHR call made during set up is ignored.
var config = {};
config.yio = new IOStub();
module.setup_config(config);
module.setup_dupe_finder();

Y.on('domready', function(e) {
    Y.Test.Runner.run();
});
});