BigW Consortium Gitlab

build_spec.js 8.61 KB
Newer Older
1
/* eslint-disable no-new */
2
/* global Build */
3 4 5 6 7
import { bytesToKiB } from '~/lib/utils/number_utils';
import '~/lib/utils/datetime_utility';
import '~/lib/utils/url_utility';
import '~/build';
import '~/breakpoints';
8

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

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

14
  beforeEach(() => {
15
    loadFixtures('builds/build-with-artifacts.html.raw');
16
  });
17

18
  describe('class constructor', () => {
19
    beforeEach(() => {
20 21
      jasmine.clock().install();
    });
22

23 24
    afterEach(() => {
      jasmine.clock().uninstall();
25 26
    });

27
    describe('setup', () => {
28
      beforeEach(function () {
29
        this.build = new Build();
30 31
      });

32
      it('copies build options', function () {
33
        expect(this.build.pageUrl).toBe(BUILD_URL);
34 35
        expect(this.build.buildStatus).toBe('success');
        expect(this.build.buildStage).toBe('test');
36
        expect(this.build.state).toBe('');
37 38
      });

39
      it('only shows the jobs matching the current stage', () => {
40 41 42 43
        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);
      });
44

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

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

52 53 54 55 56
        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);
      });
57

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

64
    describe('running build', () => {
65
      it('updates the build trace on an interval', function () {
Filipa Lacerda committed
66 67 68 69
        const deferred1 = $.Deferred();
        const deferred2 = $.Deferred();
        const deferred3 = $.Deferred();
        spyOn($, 'ajax').and.returnValues(deferred1.promise(), deferred2.promise(), deferred3.promise());
70 71
        spyOn(gl.utils, 'visitUrl');

Filipa Lacerda committed
72
        deferred1.resolve({
73 74 75 76
          html: '<span>Update<span>',
          status: 'running',
          state: 'newstate',
          append: true,
77
          complete: false,
78 79
        });

Filipa Lacerda committed
80
        deferred2.resolve();
81

Filipa Lacerda committed
82
        deferred3.resolve({
83 84 85 86
          html: '<span>More</span>',
          status: 'running',
          state: 'finalstate',
          append: true,
87
          complete: true,
88
        });
89

Filipa Lacerda committed
90 91 92 93 94 95 96
        this.build = new Build();

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

        jasmine.clock().tick(4001);

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

101
      it('replaces the entire build trace', () => {
Filipa Lacerda committed
102 103 104 105 106 107
        const deferred1 = $.Deferred();
        const deferred2 = $.Deferred();
        const deferred3 = $.Deferred();

        spyOn($, 'ajax').and.returnValues(deferred1.promise(), deferred2.promise(), deferred3.promise());

108 109
        spyOn(gl.utils, 'visitUrl');

Filipa Lacerda committed
110 111
        deferred1.resolve({
          html: '<span>Update<span>',
112
          status: 'running',
113 114
          append: false,
          complete: false,
115 116
        });

Filipa Lacerda committed
117
        deferred2.resolve();
118

Filipa Lacerda committed
119
        deferred3.resolve({
120 121 122
          html: '<span>Different</span>',
          status: 'running',
          append: false,
123 124
        });

Filipa Lacerda committed
125 126 127 128 129 130
        this.build = new Build();

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

        jasmine.clock().tick(4001);

131 132 133
        expect($('#build-trace .js-build-output').text()).not.toMatch(/Update/);
        expect($('#build-trace .js-build-output').text()).toMatch(/Different/);
      });
Filipa Lacerda committed
134
    });
135

Filipa Lacerda committed
136 137 138 139 140 141 142 143 144 145 146 147 148
    describe('truncated information', () => {
      describe('when size is less than total', () => {
        it('shows information about truncated log', () => {
          spyOn(gl.utils, 'visitUrl');
          const deferred = $.Deferred();
          spyOn($, 'ajax').and.returnValue(deferred.promise());

          deferred.resolve({
            html: '<span>Update</span>',
            status: 'success',
            append: false,
            size: 50,
            total: 100,
149 150
          });

Filipa Lacerda committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
          this.build = new Build();

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

        it('shows the size in KiB', () => {
          const size = 50;
          spyOn(gl.utils, 'visitUrl');
          const deferred = $.Deferred();

          spyOn($, 'ajax').and.returnValue(deferred.promise());
          deferred.resolve({
            html: '<span>Update</span>',
            status: 'success',
            append: false,
            size,
            total: 100,
168 169
          });

Filipa Lacerda committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
          this.build = new Build();

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

        it('shows incremented size', () => {
          const deferred1 = $.Deferred();
          const deferred2 = $.Deferred();
          const deferred3 = $.Deferred();

          spyOn($, 'ajax').and.returnValues(deferred1.promise(), deferred2.promise(), deferred3.promise());

          spyOn(gl.utils, 'visitUrl');

          deferred1.resolve({
            html: '<span>Update</span>',
            status: 'success',
            append: false,
            size: 50,
            total: 100,
192 193
          });

Filipa Lacerda committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
          deferred2.resolve();

          this.build = new Build();

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

          jasmine.clock().tick(4001);

          deferred3.resolve({
            html: '<span>Update</span>',
            status: 'success',
            append: true,
            size: 10,
            total: 100,
210
          });
Filipa Lacerda committed
211 212 213 214

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

Filipa Lacerda committed
217 218 219 220 221 222 223 224 225 226 227 228
        it('renders the raw link', () => {
          const deferred = $.Deferred();
          spyOn(gl.utils, 'visitUrl');

          spyOn($, 'ajax').and.returnValue(deferred.promise());
          deferred.resolve({
            html: '<span>Update</span>',
            status: 'success',
            append: false,
            size: 50,
            total: 100,
          });
229

Filipa Lacerda committed
230
          this.build = new Build();
231

Filipa Lacerda committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
          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', () => {
          const deferred = $.Deferred();
          spyOn(gl.utils, 'visitUrl');

          spyOn($, 'ajax').and.returnValue(deferred.promise());
          deferred.resolve({
            html: '<span>Update</span>',
            status: 'success',
            append: false,
            size: 100,
            total: 100,
250
          });
Filipa Lacerda committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

          this.build = new Build();

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

    describe('output trace', () => {
      beforeEach(() => {
        const deferred = $.Deferred();
        spyOn(gl.utils, 'visitUrl');

        spyOn($, 'ajax').and.returnValue(deferred.promise());
        deferred.resolve({
          html: '<span>Update</span>',
          status: 'success',
          append: false,
          size: 50,
          total: 100,
271
        });
Filipa Lacerda committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

        this.build = new Build();
      });

      it('should render trace controls', () => {
        const controllers = document.querySelector('.controllers');

        expect(controllers.querySelector('.js-raw-link-controller')).toBeDefined();
        expect(controllers.querySelector('.js-erase-link')).toBeDefined();
        expect(controllers.querySelector('.js-scroll-up')).toBeDefined();
        expect(controllers.querySelector('.js-scroll-down')).toBeDefined();
      });

      it('should render received output', () => {
        expect(
          document.querySelector('.js-build-output').innerHTML,
        ).toEqual('<span>Update</span>');
289
      });
290 291
    });
  });
292
});