summaryrefslogtreecommitdiff
path: root/spec/javascripts/commit/commit_pipeline_status_component_spec.js
blob: 421fe62a1e73c572082fe859a7a489be3591f632 (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
import Vue from 'vue';
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import commitPipelineStatus from '~/projects/tree/components/commit_pipeline_status_component.vue';
import mountComponent from 'spec/helpers/vue_mount_component_helper';

describe('Commit pipeline status component', () => {
  let vm;
  let Component;
  let mock;
  const mockCiStatus = {
    details_path: '/root/hello-world/pipelines/1',
    favicon: 'canceled.ico',
    group: 'canceled',
    has_details: true,
    icon: 'status_canceled',
    label: 'canceled',
    text: 'canceled',
  };

  beforeEach(() => {
    Component = Vue.extend(commitPipelineStatus);
  });

  describe('While polling pipeline data succesfully', () => {
    beforeEach(() => {
      mock = new MockAdapter(axios);
      mock.onGet('/dummy/endpoint').reply(() => {
        const res = Promise.resolve([200, {
          pipelines: [
            {
              details: {
                status: mockCiStatus,
              },
            },
          ],
        }]);
        return res;
      });
      vm = mountComponent(Component, {
        endpoint: '/dummy/endpoint',
      });
    });

    afterEach(() => {
      vm.poll.stop();
      vm.$destroy();
      mock.restore();
    });

    it('shows the loading icon when polling is starting', (done) => {
      expect(vm.$el.querySelector('.loading-container')).not.toBe(null);
      setTimeout(() => {
        expect(vm.$el.querySelector('.loading-container')).toBe(null);
        done();
      });
    });

    it('contains a ciStatus when the polling is succesful ', (done) => {
      setTimeout(() => {
        expect(vm.ciStatus).toEqual(mockCiStatus);
        done();
      });
    });

    it('contains a ci-status icon when polling is succesful', (done) => {
      setTimeout(() => {
        expect(vm.$el.querySelector('.ci-status-icon')).not.toBe(null);
        expect(vm.$el.querySelector('.ci-status-icon').classList).toContain(`ci-status-icon-${mockCiStatus.group}`);
        done();
      });
    });
  });

  describe('When polling data was not succesful', () => {
    beforeEach(() => {
      mock = new MockAdapter(axios);
      mock.onGet('/dummy/endpoint').reply(() => {
        const res = Promise.reject([502, { }]);
        return res;
      });
      vm = new Component({
        props: {
          endpoint: '/dummy/endpoint',
        },
      });
    });

    afterEach(() => {
      vm.poll.stop();
      vm.$destroy();
      mock.restore();
    });

    it('calls an errorCallback', (done) => {
      spyOn(vm, 'errorCallback').and.callThrough();
      vm.$mount();
      setTimeout(() => {
        expect(vm.errorCallback.calls.count()).toEqual(1);
        done();
      });
    });
  });
});