BigW Consortium Gitlab

build_spec.js.es6 6.03 KB
Newer Older
1
/* eslint-disable no-new */
2 3 4
/* global Build */
/* global Turbolinks */

5
//= require lib/utils/datetime_utility
6 7 8 9 10
//= require build
//= require breakpoints
//= require jquery.nicescroll
//= require turbolinks

11
describe('Build', () => {
12
  const BUILD_URL = `${gl.TEST_HOST}/frontend-fixtures/builds-project/builds/1`;
13 14 15 16 17 18 19 20 21
  // see spec/factories/ci/builds.rb
  const BUILD_TRACE = 'BUILD TRACE';
  // see lib/ci/ansi2html.rb
  const INITIAL_BUILD_TRACE_STATE = window.btoa(JSON.stringify({
    offset: BUILD_TRACE.length, n_open_tags: 0, fg_color: null, bg_color: null, style_mask: 0,
  }));

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

22
  beforeEach(() => {
23 24 25
    fixture.load('builds/build-with-artifacts.html.raw');
    spyOn($, 'ajax');
  });
26

27
  describe('constructor', () => {
28
    beforeEach(() => {
29 30
      jasmine.clock().install();
    });
31

32 33
    afterEach(() => {
      jasmine.clock().uninstall();
34 35
    });

36
    describe('setup', () => {
37
      beforeEach(function () {
38
        this.build = new Build();
39 40
      });

41
      it('copies build options', function () {
42 43
        expect(this.build.pageUrl).toBe(BUILD_URL);
        expect(this.build.buildUrl).toBe(`${BUILD_URL}.json`);
44 45 46
        expect(this.build.buildStatus).toBe('success');
        expect(this.build.buildStage).toBe('test');
        expect(this.build.state).toBe(INITIAL_BUILD_TRACE_STATE);
47 48
      });

49
      it('only shows the jobs matching the current stage', () => {
50 51 52 53
        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);
      });
54

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

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

62 63 64 65 66
        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);
      });
67

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

74 75
    describe('initial build trace', () => {
      beforeEach(() => {
76 77
        new Build();
      });
78

79
      it('displays the initial build trace', () => {
80 81
        expect($.ajax.calls.count()).toBe(1);
        const [{ url, dataType, success, context }] = $.ajax.calls.argsFor(0);
82
        expect(url).toBe(`${BUILD_URL}.json`);
83 84
        expect(dataType).toBe('json');
        expect(success).toEqual(jasmine.any(Function));
85

86
        success.call(context, { trace_html: '<span>Example</span>', status: 'running' });
87

88
        expect($('#build-trace .js-build-output').text()).toMatch(/Example/);
89 90
      });

91
      it('removes the spinner', () => {
92 93
        const [{ success, context }] = $.ajax.calls.argsFor(0);
        success.call(context, { trace_html: '<span>Example</span>', status: 'success' });
94

95 96 97
        expect($('.js-build-refresh').length).toBe(0);
      });
    });
98

99
    describe('running build', () => {
100 101 102
      beforeEach(function () {
        $('.js-build-options').data('buildStatus', 'running');
        this.build = new Build();
103
        spyOn(this.build, 'location').and.returnValue(BUILD_URL);
104
      });
105

106 107 108 109 110 111
      it('updates the build trace on an interval', function () {
        jasmine.clock().tick(4001);

        expect($.ajax.calls.count()).toBe(2);
        let [{ url, dataType, success, context }] = $.ajax.calls.argsFor(1);
        expect(url).toBe(
112
          `${BUILD_URL}/trace.json?state=${encodeURIComponent(INITIAL_BUILD_TRACE_STATE)}`
113 114 115 116 117 118 119 120 121
        );
        expect(dataType).toBe('json');
        expect(success).toEqual(jasmine.any(Function));

        success.call(context, {
          html: '<span>Update<span>',
          status: 'running',
          state: 'newstate',
          append: true,
122 123
        });

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

127
        jasmine.clock().tick(4001);
128

129 130
        expect($.ajax.calls.count()).toBe(3);
        [{ url, dataType, success, context }] = $.ajax.calls.argsFor(2);
131
        expect(url).toBe(`${BUILD_URL}/trace.json?state=newstate`);
132 133 134 135 136 137 138 139
        expect(dataType).toBe('json');
        expect(success).toEqual(jasmine.any(Function));

        success.call(context, {
          html: '<span>More</span>',
          status: 'running',
          state: 'finalstate',
          append: true,
140
        });
141 142 143

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

146
      it('replaces the entire build trace', () => {
147 148 149 150 151 152
        jasmine.clock().tick(4001);
        let [{ success, context }] = $.ajax.calls.argsFor(1);
        success.call(context, {
          html: '<span>Update</span>',
          status: 'running',
          append: true,
153 154
        });

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

157 158 159 160 161 162
        jasmine.clock().tick(4001);
        [{ success, context }] = $.ajax.calls.argsFor(2);
        success.call(context, {
          html: '<span>Different</span>',
          status: 'running',
          append: false,
163 164
        });

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

169
      it('reloads the page when the build is done', () => {
170
        spyOn(Turbolinks, 'visit');
171

172 173 174 175 176 177
        jasmine.clock().tick(4001);
        const [{ success, context }] = $.ajax.calls.argsFor(1);
        success.call(context, {
          html: '<span>Final</span>',
          status: 'passed',
          append: true,
178
        });
179

180
        expect(Turbolinks.visit).toHaveBeenCalledWith(BUILD_URL);
181 182 183
      });
    });
  });
184
});