summaryrefslogtreecommitdiff
path: root/spec/javascripts/build_spec.js.es6
blob: 370944b6a8c9f55267f5aa4a30b9ff9c10b614e9 (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
/* global Build */
/* eslint-disable no-new */
//= require build
//= require breakpoints
//= require jquery.nicescroll
//= require turbolinks

(() => {
  describe('Build', () => {
    fixture.preload('build.html');

    beforeEach(function () {
      fixture.load('build.html');
      spyOn($, 'ajax');
    });

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

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

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

        it('copies build options', function () {
          expect(this.build.pageUrl).toBe('http://example.com/root/test-build/builds/2');
          expect(this.build.buildUrl).toBe('http://example.com/root/test-build/builds/2.json');
          expect(this.build.buildStatus).toBe('passed');
          expect(this.build.buildStage).toBe('test');
          expect(this.build.state).toBe('buildstate');
        });

        it('only shows the jobs matching the current stage', function () {
          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', function () {
          expect($('.stage-selection').text()).toBe('test');
        });

        it('updates the jobs when the build dropdown changes', function () {
          $('.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);
        });
      });

      describe('initial build trace', function () {
        beforeEach(function () {
          new Build();
        });

        it('displays the initial build trace', function () {
          expect($.ajax.calls.count()).toBe(1);
          const [{ url, dataType, success, context }] = $.ajax.calls.argsFor(0);
          expect(url).toBe('http://example.com/root/test-build/builds/2.json');
          expect(dataType).toBe('json');
          expect(success).toEqual(jasmine.any(Function));

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

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

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

          expect($('.js-build-refresh').length).toBe(0);
        });
      });

      describe('running build', function () {
        beforeEach(function () {
          $('.js-build-options').data('buildStatus', 'running');
          this.build = new Build();
          spyOn(this.build, 'location')
            .and.returnValue('http://example.com/root/test-build/builds/2');
        });

        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(
            'http://example.com/root/test-build/builds/2/trace.json?state=buildstate'
          );
          expect(dataType).toBe('json');
          expect(success).toEqual(jasmine.any(Function));

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

          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);
          [{ url, dataType, success, context }] = $.ajax.calls.argsFor(2);
          expect(url).toBe(
            'http://example.com/root/test-build/builds/2/trace.json?state=newstate'
          );
          expect(dataType).toBe('json');
          expect(success).toEqual(jasmine.any(Function));

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

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

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

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

          jasmine.clock().tick(4001);
          [{ success, context }] = $.ajax.calls.argsFor(2);
          success.call(context, {
            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', function () {
          spyOn(Turbolinks, 'visit');

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

          expect(Turbolinks.visit).toHaveBeenCalledWith(
            'http://example.com/root/test-build/builds/2'
          );
        });
      });
    });
  });
})();