~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
/* Copyright 2011 Canonical Ltd.  This software is licensed under the
 * GNU Affero General Public License version 3 (see the file LICENSE).
 *
 * Tests for lp.code.branchmergeproposal.updater.
 *
 */

YUI().use('lp.testing.runner', 'test', 'dump', 'console', 'node',
          'lp.testing.mockio', 'event',
          'lp.code.branchmergeproposal.updater', function(Y) {

var suite = new Y.Test.Suite("BranchMergeProposal Updater Tests");

var module = Y.lp.code.branchmergeproposal.updater;
var UpdaterWidget = module.UpdaterWidget;

/*
 * Tests for when the updater is built on top of a pending diff.
 *
 */

var pending_mp = Y.one('#pending-mp').getContent();

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

    name: 'branchmergeproposal-updater-pending-tests',

    setUp: function() {
        Y.one("#placeholder")
            .empty()
            .append(Y.Node.create(pending_mp));
        var diff_area = Y.one('#diff-area');
        this.updater = new UpdaterWidget({srcNode: diff_area});

        LP.cache.context = {
            web_link: "https://code.launchpad.dev/~foo/bar/foobr/+merge/123"};

    },

    tearDown: function() {
        this.updater.destroy();
    },

    test_default_values: function() {
        Y.Assert.isTrue(this.updater.get('pending'));
        Y.Assert.areEqual(
            '',
            this.updater.get('diff'));
    },

    test__setup_diff_container: function() {
        this.updater._setup_diff_container();
        Y.Assert.isFalse(this.updater.get('pending'));
        Y.Assert.areEqual(
            "Preview Diff ",
            this.updater.get(
                'srcNode').one('#review-diff h2').get('text'));
        Y.Assert.areEqual(
            "",
            this.updater.get(
                'srcNode').one('.diff-content').get('text'));
    },

    test_set_status_updating: function() {
        this.updater.set_status_updating();
        Y.Assert.areEqual(
            '/@@/spinner',
            Y.one('h2').one('img').getAttribute('src'));
    },

    test_set_status_longpolling: function() {
        this.updater.set_status_longpolling();
        Y.Assert.areEqual(
            '/@@/longpoll_loading',
            Y.one('h2').one('img').getAttribute('src'));
    },

    test_set_status_longpollerror: function() {
        this.updater.set_status_longpollerror();
        Y.Assert.areEqual(
            '/@@/longpoll_error',
            Y.one('h2').one('img').getAttribute('src'));
    },

    test_cleanup_status: function() {
        this.updater._setup_diff_container();
        this.updater.set_status_updating();
        this.updater.cleanup_status();
        Y.Assert.areEqual(
            'Preview Diff ',
            Y.one('h2').get('innerHTML'));
    },

    test_get_diff: function() {
        this.updater._setup_diff_container();
        Y.one('.diff-content').set(
            'innerHTML', 'this is a <span>diff</span>');
        Y.Assert.areEqual(
            'this is a <span>diff</span>',
            this.updater.get('diff'));
    },

    test_set_diff: function() {
        this.updater.set('diff', 'this is a <span>diff</span>');
        Y.Assert.areEqual(
            'this is a <span>diff</span>',
            Y.one('.diff-content').get('innerHTML'));
    },

    test_update_success: function() {
        var mockio = new Y.lp.testing.mockio.MockIo();
        this.updater.get('lp_client').io_provider = mockio;
        Y.Assert.areEqual(
            '',
            this.updater.get('diff'));
        this.updater.update();
        mockio.success({
            responseText: 'New <span>diff</span>',
            responseHeaders: {'Content-Type': 'text/html'}});

        Y.Assert.areEqual(
            'New <span>diff</span>',
            this.updater.get('diff'));
    },

    test_update_fires_event: function() {
        var fired = false;
        var mockio = new Y.lp.testing.mockio.MockIo();
        this.updater.get('lp_client').io_provider = mockio;
        this.updater.on(this.updater.NAME + '.updated', function() {
            fired = true;
        });
        this.updater.update();
        mockio.success({
            responseText: 'New <span>diff</span>',
            responseHeaders: {'Content-Type': 'text/html'}});

        Y.Assert.isTrue(fired);
    }

}));

/*
 * Tests for when the updater is built on top of an existing diff.
 *
 */
var current_mp = Y.one('#current-mp').getContent();

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

    name: 'branchmergeproposal-updater-refresh-tests',

    setUp: function() {
        Y.one("#placeholder")
            .empty()
            .append(Y.Node.create(current_mp));
        var diff_area = Y.one('#diff-area');
        this.updater = new UpdaterWidget({srcNode: diff_area});
    },

    tearDown: function() {
        this.updater.destroy();
    },

    test_default_values: function() {
        Y.Assert.isFalse(this.updater.get('pending'));
        Y.Assert.areEqual(
            'Example diff',
            this.updater.get('diff'));
    },

    test_get_diff: function() {
        Y.one('.diff-content').set(
            'innerHTML', 'this is a <span>diff</span>');
        Y.Assert.areEqual(
            'this is a <span>diff</span>',
            this.updater.get('diff'));
    }

}));

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

    name: 'branchmergeproposal-updater-utilities',

    test_is_mp_diff_updated_modified: function() {
        var data = {what: 'modified', edited_fields: ['preview_diff']};
        Y.Assert.isTrue(module.is_mp_diff_updated(data));
    },

    test_is_mp_diff_updater_deleted: function() {
        var data = {what: 'deleted'};
        Y.Assert.isFalse(module.is_mp_diff_updated(data));
    },

    test_is_mp_diff_updated_title_changed: function() {
        var data = {what: 'modified', edited_fields: ['title']};
        Y.Assert.isFalse(module.is_mp_diff_updated(data));
    }

}));

Y.lp.testing.Runner.run(suite);

});