~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
/* Copyright (c) 2008, Canonical Ltd. All rights reserved. */

YUI({
    base: '../../../../canonical/launchpad/icing/yui/',
    filter: 'raw',
    combine: false,
    fetchCSS: false
      }).use('event', 'lp.bugs.bugtask_index', 'lp.client', 'node',
             'test', 'widget-stack', 'console', function(Y) {

// Local aliases
var Assert = Y.Assert,
    ArrayAssert = Y.ArrayAssert;

/*
 * 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 rawnode = Y.Node.getDOMNode(widget.one(selector));
    Y.Event.simulate(rawnode, evtype, options);
}

var suite = new Y.Test.Suite("Bugtask Me-Too Choice Edit Tests");

suite.add(new Y.Test.Case({

    name: 'me_too_choice_edit_basics',

    setUp: function() {
        // Monkeypatch LP to avoid network traffic and to make
        // some things work as expected.
        Y.lp.client.Launchpad.prototype.named_post =
          function(url, func, config) {
            config.on.success();
          };
        LP = {
          'cache': {
            'bug': {
              self_link: "http://bugs.example.com/bugs/1234"
          }}};
        // add the in-page HTML
        var inpage = Y.Node.create([
            '<span id="affectsmetoo">',
            '  <span class="static">',
            '   <img src="https://bugs.launchpad.net/@@/flame-icon" alt=""/>',
            '    This bug affects me too',
            '    <a href="+affectsmetoo">',
            '      <img class="editicon" alt="Edit"',
            '           src="https://bugs.launchpad.net/@@/edit" />',
            '    </a>',
            '  </span>',
            '  <span class="dynamic unseen">',
            '    <img class="editicon" alt="Edit"',
            '         src="https://bugs.launchpad.net/@@/edit" />',
            '    <a href="+affectsmetoo" class="js-action"',
            '      ><span class="value">Does this bug affect you?</span></a>',
            '   <img src="https://bugs.launchpad.net/@@/flame-icon" alt=""/>',
            '  </span>',
            '</span>'].join(''));
        Y.one("body").appendChild(inpage);
        var me_too_content = Y.one('#affectsmetoo');
        this.config = {
            contentBox: me_too_content, value: null,
            elementToFlash: me_too_content, others_affected_count: 5
        };
        this.choice_edit = new Y.lp.bugs.bugtask_index._MeTooChoiceSource(
            this.config);
        this.choice_edit.render();
    },

    tearDown: function() {
        var status = Y.one("document").one("#affectsmetoo");
        if (status) {
            status.get("parentNode").removeChild(status);
        }
    },

    /**
     * The choice edit should be displayed inline.
     */
    test_is_inline: function() {
        var display = this.choice_edit.get('boundingBox').getStyle('display');
        Assert.areEqual(
            display, 'inline',
            "Not displayed inline, display is: " + display);
    },

    /**
     * The .static area should be hidden by adding the "unseen" class.
     */
    test_hide_static: function() {
        var static_area = this.choice_edit.get('contentBox').one('.static');
        Assert.isTrue(
            static_area.hasClass('unseen'), "Static area is not hidden.");
    },

    /**
     * The .dynamic area should be shown by removing the "unseen" class.
     */
    test_hide_dynamic: function() {
        var dynamic_area = this.choice_edit.get('contentBox').one('.dynamic');
        Assert.isFalse(
            dynamic_area.hasClass('unseen'), "Dynamic area is hidden.");
    },

    /**
     * The UI should be in a waiting state while the save process is
     * executing and return to a non-waiting state once it has
     * finished.
     */
    test_ui_waiting_for_success: function() {
        this.do_test_ui_waiting('success');
    },

    /**
     * The UI should be in a waiting state while the save process is
     * executing and return to a non-waiting state even if the process
     * fails.
     */
    test_ui_waiting_for_failure: function() {
        this.do_test_ui_waiting('failure');
    },

    /**
     * Helper function that does the leg work for the
     * test_ui_waiting_* methods.
     */
    do_test_ui_waiting: function(callback) {
        var edit_icon = this.choice_edit.get('editicon');
        // The spinner should not be displayed at first.
        Assert.isNull(
            edit_icon.get('src').match(/\/spinner$/),
            "The edit icon is displaying a spinner at rest.");
        // The spinner should not be displayed after opening the
        // choice list.
        simulate(this.choice_edit.get('boundingBox'), '.value', 'click');
        Assert.isNull(
            edit_icon.get('src').match(/\/spinner$/),
            "The edit icon is displaying a spinner after opening the " +
            "choice list.");
        // The spinner should be visible during the interval between a
        // choice being made and a response coming back from Launchpad
        // that the choice has been saved.
        var edit_icon_src_during_save;
        // Patch the named_post method to simulate success or failure,
        // as determined by the callback argument. We cannot make
        // assertions in this method because exceptions are swallowed
        // somewhere. Instead, we save something testable to a local
        // var.
        Y.lp.client.Launchpad.prototype.named_post =
            function(url, func, config) {
                edit_icon_src_during_save = edit_icon.get('src');
                config.on[callback]();
            };
        simulate(this.choice_edit._choice_list.get('boundingBox'),
                 'li a[href$=true]', 'click');
        Assert.isNotNull(
            edit_icon_src_during_save.match(/\/spinner$/),
            "The edit icon is not displaying a spinner during save.");
        // The spinner should not be displayed once a choice has been
        // saved.
        Assert.isNull(
            edit_icon.get('src').match(/\/spinner$/),
            "The edit icon is displaying a spinner once the choice has " +
            "been made.");
    },

    test__getSourceNames: function() {
        var names;
        // No other users affected.
        names = this.choice_edit._getSourceNames(0);
        Assert.areEqual(
            'This bug affects you', names[true]);
        Assert.areEqual(
            "This bug doesn't affect you", names[false]);
        // 1 other user affected.
        names = this.choice_edit._getSourceNames(1);
        Assert.areEqual(
            'This bug affects you and 1 other person', names[true]);
        Assert.areEqual(
            'This bug affects 1 person, but not you', names[false]);
        // 2 other users affected.
        names = this.choice_edit._getSourceNames(2);
        Assert.areEqual(
            'This bug affects you and 2 other people', names[true]);
        Assert.areEqual(
            'This bug affects 2 people, but not you', names[false]);
    },

    test_new_names_are_applied: function() {
        var names = {};
        Y.each(this.choice_edit.get('items'), function(item) {
            names[item.value] = item.source_name;
        });
        Assert.areEqual(
            'This bug affects you and 5 other people', names[true]);
        Assert.areEqual(
            'This bug affects 5 people, but not you', names[false]);
    }

}));

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

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

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

});