BigW Consortium Gitlab

build_spec.js 5.56 KB
Newer Older
1
/* eslint-disable no-new */
2 3
/* global Build */

4
require('~/lib/utils/datetime_utility');
5
require('~/lib/utils/url_utility');
6 7
require('~/build');
require('~/breakpoints');
8
require('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);
      });
47

48
      it('selects the current stage in the build dropdown menu', () => {
49 50
        expect($('.stage-selection').text()).toBe('test');
      });
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 committed
101
        expect($.ajax.calls.count()).toBe(3);
102

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 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
      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,
          truncated: true,
          size: '50',
        });

        expect(
          $('#build-trace .js-truncated-info').text().trim(),
        ).toContain('Showing last 50 KiB of log');
        expect($('#build-trace .js-truncated-info-size').text()).toMatch('50');
      });

165
      it('reloads the page when the build is done', () => {
Bryce Johnson committed
166
        spyOn(gl.utils, 'visitUrl');
167

168
        jasmine.clock().tick(4001);
169 170
        const [{ success }] = $.ajax.calls.argsFor(0);
        success.call($, {
171 172 173
          html: '<span>Final</span>',
          status: 'passed',
          append: true,
174
          complete: true,
175
        });
176

Bryce Johnson committed
177
        expect(gl.utils.visitUrl).toHaveBeenCalledWith(BUILD_URL);
178 179 180
      });
    });
  });
181
});