aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/test/js/metrics-spec.js
blob: 3556ed5cc09d293a0f0c75085749ac5a95da23a3 (plain)
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
/* globals casper: false */
var lib = require('../lib'),
    testName = lib.testName('Metrics');

lib.initMessages();
lib.changeWorkingDirectory('metrics-spec');
lib.configureCasper();

casper.test.begin(testName('List'), 9, function (test) {
  casper
      .start(lib.buildUrl('metrics'), function () {
        lib.setDefaultViewport();
        lib.mockRequestFromFile('/api/metrics/domains', 'domains.json');
        lib.mockRequestFromFile('/api/metrics/types', 'types.json');
        lib.mockRequestFromFile('/api/metrics/search', 'search.json');
      })

      .then(function () {
        casper.evaluate(function () {
          require(['apps/metrics/app'], function (App) {
            App.start({ el: '#metrics' });
          });
        });
      })

      .then(function () {
        casper.waitForSelector('#metrics-list li');
      })

      .then(function () {
        test.assertElementCount('#metrics-list li[data-id]', 3);
        test.assertSelectorContains('#metrics-list .js-metric-name', 'Business value');
        test.assertSelectorContains('#metrics-list .js-metric-key', 'business_value');
        test.assertSelectorContains('#metrics-list .js-metric-domain', 'Complexity');
        test.assertSelectorContains('#metrics-list .js-metric-type', 'Percent');
        test.assertSelectorContains('#metrics-list .js-metric-description', 'Description of Business value');
        test.assertElementCount('#metrics-list .js-metric-update', 3);
        test.assertElementCount('#metrics-list .js-metric-delete', 3);
        test.assertSelectorContains('#metrics-list-footer', '3/3');
      })

      .then(function () {
        lib.sendCoverage();
      })
      .run(function () {
        test.done();
      });
});


casper.test.begin(testName('Show More'), 4, function (test) {
  casper
      .start(lib.buildUrl('metrics'), function () {
        lib.setDefaultViewport();
        lib.mockRequestFromFile('/api/metrics/domains', 'domains.json');
        lib.mockRequestFromFile('/api/metrics/types', 'types.json');
        this.searchMock = lib.mockRequestFromFile('/api/metrics/search', 'search-big-1.json');
      })

      .then(function () {
        casper.evaluate(function () {
          require(['apps/metrics/app'], function (App) {
            App.start({ el: '#metrics' });
          });
        });
      })

      .then(function () {
        casper.waitForSelector('#metrics-list li');
      })

      .then(function () {
        test.assertElementCount('#metrics-list li[data-id]', 2);
        test.assertSelectorContains('#metrics-list-footer', '2/3');
        lib.clearRequestMock(this.searchMock);
        this.searchMock = lib.mockRequestFromFile('/api/metrics/search', 'search-big-2.json', { data: { p: '2' } });
        casper.click('#metrics-fetch-more');
        casper.waitForSelectorTextChange('#metrics-list-footer');
      })

      .then(function () {
        test.assertElementCount('#metrics-list li[data-id]', 3);
        test.assertSelectorContains('#metrics-list-footer', '3/3');
      })

      .then(function () {
        lib.sendCoverage();
      })
      .run(function () {
        test.done();
      });
});


casper.test.begin(testName('Create'), 4, function (test) {
  casper
      .start(lib.buildUrl('metrics'), function () {
        lib.setDefaultViewport();
        lib.mockRequestFromFile('/api/metrics/domains', 'domains.json');
        lib.mockRequestFromFile('/api/metrics/types', 'types.json');
        this.searchMock = lib.mockRequestFromFile('/api/metrics/search', 'search.json');
        this.createMock = lib.mockRequestFromFile('/api/metrics/create', 'error.json', { status: 400 });
      })

      .then(function () {
        casper.evaluate(function () {
          require(['apps/metrics/app'], function (App) {
            App.start({ el: '#metrics' });
          });
          jQuery.ajaxSetup({ dataType: 'json' });
        });
      })

      .then(function () {
        casper.waitForSelector('#metrics-list li');
      })

      .then(function () {
        test.assertElementCount('#metrics-list li[data-id]', 3);
        casper.click('#metrics-create');
        casper.waitForSelector('#create-metric-form');
      })

      .then(function () {
        casper.click('#create-metric-submit');
        casper.waitForSelector('.alert.alert-danger');
      })

      .then(function () {
        lib.clearRequestMock(this.searchMock);
        lib.mockRequestFromFile('/api/metrics/search', 'search-created.json');
        lib.clearRequestMock(this.createMock);
        lib.mockRequest('/api/metrics/create', '{}',
            { data: { key: 'new_metric', name: 'New Metric', domain: 'Domain for New Metric', type: 'RATING' } });
        casper.evaluate(function () {
          jQuery('#create-metric-key').val('new_metric');
          jQuery('#create-metric-name').val('New Metric');
          jQuery('#create-metric-domain').val('Domain for New Metric');
          jQuery('#create-metric-type').val('RATING');
        });
        casper.click('#create-metric-submit');
        casper.waitForSelectorTextChange('#metrics-list-footer');
      })

      .then(function () {
        test.assertElementCount('#metrics-list li[data-id]', 4);
        test.assertSelectorContains('#metrics-list .js-metric-key', 'new_metric');
        test.assertSelectorContains('#metrics-list .js-metric-name', 'New Metric');
      })

      .then(function () {
        lib.sendCoverage();
      })
      .run(function () {
        test.done();
      });
});


casper.test.begin(testName('Update'), 4, function (test) {
  casper
      .start(lib.buildUrl('metrics'), function () {
        lib.setDefaultViewport();
        lib.mockRequestFromFile('/api/metrics/domains', 'domains.json');
        lib.mockRequestFromFile('/api/metrics/types', 'types.json');
        this.searchMock = lib.mockRequestFromFile('/api/metrics/search', 'search.json');
        this.updateMock = lib.mockRequestFromFile('/api/metrics/update', 'error.json', { status: 400 });
      })

      .then(function () {
        casper.evaluate(function () {
          require(['apps/metrics/app'], function (App) {
            App.start({ el: '#metrics' });
          });
          jQuery.ajaxSetup({ dataType: 'json' });
        });
      })

      .then(function () {
        casper.waitForSelector('#metrics-list li');
      })

      .then(function () {
        casper.click('[data-id="3"] .js-metric-update');
        casper.waitForSelector('#create-metric-form');
      })

      .then(function () {
        casper.click('#create-metric-submit');
        casper.waitForSelector('.alert.alert-danger');
      })

      .then(function () {
        lib.clearRequestMock(this.searchMock);
        lib.mockRequestFromFile('/api/metrics/search', 'search-updated.json');
        lib.clearRequestMock(this.createMock);
        lib.mockRequest('/api/metrics/update', '{}',
            { data: { id: '3', key: 'updated_key', name: 'Updated Name', domain: 'Random Domain', type: 'WORK_DUR' } });
        casper.evaluate(function () {
          jQuery('#create-metric-key').val('updated_key');
          jQuery('#create-metric-name').val('Updated Name');
          jQuery('#create-metric-domain').val('Random Domain');
          jQuery('#create-metric-type').val('WORK_DUR');
        });
        casper.click('#create-metric-submit');
        casper.waitForSelectorTextChange('#metrics-list [data-id="3"] .js-metric-name');
      })

      .then(function () {
        test.assertSelectorContains('#metrics-list [data-id="3"] .js-metric-key', 'updated_key');
        test.assertSelectorContains('#metrics-list [data-id="3"] .js-metric-name', 'Updated Name');
        test.assertSelectorContains('#metrics-list [data-id="3"] .js-metric-domain', 'Random Domain');
        test.assertSelectorContains('#metrics-list [data-id="3"] .js-metric-type', 'Duration');
      })

      .then(function () {
        lib.sendCoverage();
      })
      .run(function () {
        test.done();
      });
});


casper.test.begin(testName('Delete'), 1, function (test) {
  casper
      .start(lib.buildUrl('metrics'), function () {
        lib.setDefaultViewport();
        lib.mockRequestFromFile('/api/metrics/domains', 'domains.json');
        lib.mockRequestFromFile('/api/metrics/types', 'types.json');
        this.searchMock = lib.mockRequestFromFile('/api/metrics/search', 'search.json');
        this.updateMock = lib.mockRequestFromFile('/api/metrics/delete', 'error.json', { status: 400 });
      })

      .then(function () {
        casper.evaluate(function () {
          require(['apps/metrics/app'], function (App) {
            App.start({ el: '#metrics' });
          });
          jQuery.ajaxSetup({ dataType: 'json' });
        });
      })

      .then(function () {
        casper.waitForSelector('#metrics-list li');
      })

      .then(function () {
        casper.click('[data-id="3"] .js-metric-delete');
        casper.waitForSelector('#delete-metric-form');
      })

      .then(function () {
        casper.click('#delete-metric-submit');
        casper.waitForSelector('.alert.alert-danger');
      })

      .then(function () {
        lib.clearRequestMock(this.updateMock);
        lib.mockRequest('/api/metrics/delete', '{}', { data: { ids: '3'} });
        casper.click('#delete-metric-submit');
        casper.waitWhileSelector('[data-id="3"]');
      })

      .then(function () {
        test.assert(true);
      })

      .then(function () {
        lib.sendCoverage();
      })
      .run(function () {
        test.done();
      });
});