build_spec.js 8.13 KB
Newer Older
1
/* eslint-disable no-new */
2
/* global Build */
3 4 5 6 7 8
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';
9

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

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

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

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

25 26
    afterEach(() => {
      jasmine.clock().uninstall();
27 28
    });

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

34
      it('copies build options', function () {
35 36
        expect(this.build.pageUrl).toBe(BUILD_URL);
        expect(this.build.buildUrl).toBe(`${BUILD_URL}.json`);
37 38
        expect(this.build.buildStatus).toBe('success');
        expect(this.build.buildStage).toBe('test');
39
        expect(this.build.state).toBe('');
40 41
      });

42
      it('only shows the jobs matching the current stage', () => {
43 44 45 46
        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);
      });
winniehell's avatar
winniehell committed
47

48
      it('selects the current stage in the build dropdown menu', () => {
49 50
        expect($('.stage-selection').text()).toBe('test');
      });
winniehell's avatar
winniehell committed
51

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

55 56 57 58 59
        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);
      });
60

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

67
    describe('running build', () => {
68 69 70
      beforeEach(function () {
        this.build = new Build();
      });
71

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

75 76
        jasmine.clock().tick(4001);

77 78 79 80 81 82 83 84 85 86 87 88
        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($, {
89 90 91 92
          html: '<span>Update<span>',
          status: 'running',
          state: 'newstate',
          append: true,
93
          complete: false,
94 95
        });

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

99
        jasmine.clock().tick(4001);
100

Phil Hughes's avatar
Phil Hughes committed
101
        expect($.ajax.calls.count()).toBe(3);
102

Phil Hughes's avatar
Phil Hughes committed
103
        args = $.ajax.calls.argsFor(2)[0];
104 105 106 107
        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));
108

109
        args.success.call($, {
110 111 112 113
          html: '<span>More</span>',
          status: 'running',
          state: 'finalstate',
          append: true,
114
          complete: true,
115
        });
116 117 118

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

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

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

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

135
        jasmine.clock().tick(4001);
Phil Hughes's avatar
Phil Hughes committed
136
        args = $.ajax.calls.argsFor(2)[0];
137
        args.success.call($, {
138 139 140
          html: '<span>Different</span>',
          status: 'running',
          append: false,
141 142
        });

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

147
      it('reloads the page when the build is done', () => {
Bryce Johnson's avatar
Bryce Johnson committed
148
        spyOn(gl.utils, 'visitUrl');
149

150
        jasmine.clock().tick(4001);
151 152
        const [{ success }] = $.ajax.calls.argsFor(0);
        success.call($, {
153 154 155
          html: '<span>Final</span>',
          status: 'passed',
          append: true,
156
          complete: true,
157
        });
158

Bryce Johnson's avatar
Bryce Johnson committed
159
        expect(gl.utils.visitUrl).toHaveBeenCalledWith(BUILD_URL);
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

      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');
          });
        });
      });
262 263
    });
  });
264
});