summaryrefslogtreecommitdiff
path: root/spec/javascripts/build_spec.js
blob: 8ec96bdb5833ff348ea9d7ebcd7f58f28da9a603 (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
/* eslint-disable no-new */
/* global Build */
import { bytesToKiB } from '~/lib/utils/number_utils';
import '~/lib/utils/datetime_utility';
import '~/lib/utils/url_utility';
import '~/build';
import '~/breakpoints';
import 'vendor/jquery.nicescroll';

describe('Build', () => {
  const BUILD_URL = `${gl.TEST_HOST}/frontend-fixtures/builds-project/builds/1`;

  preloadFixtures('builds/build-with-artifacts.html.raw');

  beforeEach(() => {
    loadFixtures('builds/build-with-artifacts.html.raw');
    spyOn($, 'ajax');
  });

  describe('class constructor', () => {
    beforeEach(() => {
      jasmine.clock().install();
    });

    afterEach(() => {
      jasmine.clock().uninstall();
    });

    describe('setup', () => {
      beforeEach(function () {
        this.build = new Build();
      });

      it('copies build options', function () {
        expect(this.build.pageUrl).toBe(BUILD_URL);
        expect(this.build.buildUrl).toBe(`${BUILD_URL}.json`);
        expect(this.build.buildStatus).toBe('success');
        expect(this.build.buildStage).toBe('test');
        expect(this.build.state).toBe('');
      });

      it('only shows the jobs matching the current stage', () => {
        expect($('.build-job[data-stage="build"]').is(':visible')).toBe(false);
        expect($('.build-job[data-stage="test"]').is(':visible')).toBe(true);
        expect($('.build-job[data-stage="deploy"]').is(':visible')).toBe(false);
      });

      it('selects the current stage in the build dropdown menu', () => {
        expect($('.stage-selection').text()).toBe('test');
      });

      it('updates the jobs when the build dropdown changes', () => {
        $('.stage-item:contains("build")').click();

        expect($('.stage-selection').text()).toBe('build');
        expect($('.build-job[data-stage="build"]').is(':visible')).toBe(true);
        expect($('.build-job[data-stage="test"]').is(':visible')).toBe(false);
        expect($('.build-job[data-stage="deploy"]').is(':visible')).toBe(false);
      });

      it('displays the remove date correctly', () => {
        const removeDateElement = document.querySelector('.js-artifacts-remove');
        expect(removeDateElement.innerText.trim()).toBe('1 year');
      });
    });

    describe('running build', () => {
      beforeEach(function () {
        this.build = new Build();
      });

      it('updates the build trace on an interval', function () {
        spyOn(gl.utils, 'visitUrl');

        jasmine.clock().tick(4001);

        expect($.ajax.calls.count()).toBe(1);

        // We have to do it this way to prevent Webpack to fail to compile
        // when destructuring assignments and reusing
        // the same variables names inside the same scope
        let args = $.ajax.calls.argsFor(0)[0];

        expect(args.url).toBe(`${BUILD_URL}/trace.json`);
        expect(args.dataType).toBe('json');
        expect(args.success).toEqual(jasmine.any(Function));

        args.success.call($, {
          html: '<span>Update<span>',
          status: 'running',
          state: 'newstate',
          append: true,
          complete: false,
        });

        expect($('#build-trace .js-build-output').text()).toMatch(/Update/);
        expect(this.build.state).toBe('newstate');

        jasmine.clock().tick(4001);

        expect($.ajax.calls.count()).toBe(3);

        args = $.ajax.calls.argsFor(2)[0];
        expect(args.url).toBe(`${BUILD_URL}/trace.json`);
        expect(args.dataType).toBe('json');
        expect(args.data.state).toBe('newstate');
        expect(args.success).toEqual(jasmine.any(Function));

        args.success.call($, {
          html: '<span>More</span>',
          status: 'running',
          state: 'finalstate',
          append: true,
          complete: true,
        });

        expect($('#build-trace .js-build-output').text()).toMatch(/UpdateMore/);
        expect(this.build.state).toBe('finalstate');
      });

      it('replaces the entire build trace', () => {
        spyOn(gl.utils, 'visitUrl');

        jasmine.clock().tick(4001);
        let args = $.ajax.calls.argsFor(0)[0];
        args.success.call($, {
          html: '<span>Update</span>',
          status: 'running',
          append: false,
          complete: false,
        });

        expect($('#build-trace .js-build-output').text()).toMatch(/Update/);

        jasmine.clock().tick(4001);
        args = $.ajax.calls.argsFor(2)[0];
        args.success.call($, {
          html: '<span>Different</span>',
          status: 'running',
          append: false,
        });

        expect($('#build-trace .js-build-output').text()).not.toMatch(/Update/);
        expect($('#build-trace .js-build-output').text()).toMatch(/Different/);
      });

      it('reloads the page when the build is done', () => {
        spyOn(gl.utils, 'visitUrl');

        jasmine.clock().tick(4001);
        const [{ success }] = $.ajax.calls.argsFor(0);
        success.call($, {
          html: '<span>Final</span>',
          status: 'passed',
          append: true,
          complete: true,
        });

        expect(gl.utils.visitUrl).toHaveBeenCalledWith(BUILD_URL);
      });

      describe('truncated information', () => {
        describe('when size is less than total', () => {
          it('shows information about truncated log', () => {
            jasmine.clock().tick(4001);
            const [{ success }] = $.ajax.calls.argsFor(0);

            success.call($, {
              html: '<span>Update</span>',
              status: 'success',
              append: false,
              size: 50,
              total: 100,
            });

            expect(document.querySelector('.js-truncated-info').classList).not.toContain('hidden');
          });

          it('shows the size in KiB', () => {
            jasmine.clock().tick(4001);
            const [{ success }] = $.ajax.calls.argsFor(0);
            const size = 50;

            success.call($, {
              html: '<span>Update</span>',
              status: 'success',
              append: false,
              size,
              total: 100,
            });

            expect(
              document.querySelector('.js-truncated-info-size').textContent.trim(),
            ).toEqual(`${bytesToKiB(size)}`);
          });

          it('shows incremented size', () => {
            jasmine.clock().tick(4001);
            let args = $.ajax.calls.argsFor(0)[0];
            args.success.call($, {
              html: '<span>Update</span>',
              status: 'success',
              append: false,
              size: 50,
              total: 100,
            });

            expect(
              document.querySelector('.js-truncated-info-size').textContent.trim(),
            ).toEqual(`${bytesToKiB(50)}`);

            jasmine.clock().tick(4001);
            args = $.ajax.calls.argsFor(2)[0];
            args.success.call($, {
              html: '<span>Update</span>',
              status: 'success',
              append: true,
              size: 10,
              total: 100,
            });

            expect(
              document.querySelector('.js-truncated-info-size').textContent.trim(),
            ).toEqual(`${bytesToKiB(60)}`);
          });

          it('renders the raw link', () => {
            jasmine.clock().tick(4001);
            const [{ success }] = $.ajax.calls.argsFor(0);

            success.call($, {
              html: '<span>Update</span>',
              status: 'success',
              append: false,
              size: 50,
              total: 100,
            });

            expect(
              document.querySelector('.js-raw-link').textContent.trim(),
            ).toContain('Complete Raw');
          });
        });

        describe('when size is equal than total', () => {
          it('does not show the trunctated information', () => {
            jasmine.clock().tick(4001);
            const [{ success }] = $.ajax.calls.argsFor(0);

            success.call($, {
              html: '<span>Update</span>',
              status: 'success',
              append: false,
              size: 100,
              total: 100,
            });

            expect(document.querySelector('.js-truncated-info').classList).toContain('hidden');
          });
        });
      });
    });
  });
});