summaryrefslogtreecommitdiff
path: root/spec/frontend/commit/components/commit_box_pipeline_status_spec.js
blob: db7b7b4539712c337519ba58e42741746b0dbfcb (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
import { GlLoadingIcon, GlLink } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import createFlash from '~/flash';
import CiIcon from '~/vue_shared/components/ci_icon.vue';
import CommitBoxPipelineStatus from '~/projects/commit_box/info/components/commit_box_pipeline_status.vue';
import {
  COMMIT_BOX_POLL_INTERVAL,
  PIPELINE_STATUS_FETCH_ERROR,
} from '~/projects/commit_box/info/constants';
import getLatestPipelineStatusQuery from '~/projects/commit_box/info/graphql/queries/get_latest_pipeline_status.query.graphql';
import * as graphQlUtils from '~/pipelines/components/graph/utils';
import { mockPipelineStatusResponse } from '../mock_data';

const mockProvide = {
  fullPath: 'root/ci-project',
  iid: '46',
  graphqlResourceEtag: '/api/graphql:pipelines/id/320',
};

Vue.use(VueApollo);

jest.mock('~/flash');

describe('Commit box pipeline status', () => {
  let wrapper;

  const statusSuccessHandler = jest.fn().mockResolvedValue(mockPipelineStatusResponse);
  const failedHandler = jest.fn().mockRejectedValue(new Error('GraphQL error'));

  const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
  const findStatusIcon = () => wrapper.findComponent(CiIcon);
  const findPipelineLink = () => wrapper.findComponent(GlLink);

  const advanceToNextFetch = () => {
    jest.advanceTimersByTime(COMMIT_BOX_POLL_INTERVAL);
  };

  const createMockApolloProvider = (handler) => {
    const requestHandlers = [[getLatestPipelineStatusQuery, handler]];

    return createMockApollo(requestHandlers);
  };

  const createComponent = (handler = statusSuccessHandler) => {
    wrapper = shallowMount(CommitBoxPipelineStatus, {
      provide: {
        ...mockProvide,
      },
      apolloProvider: createMockApolloProvider(handler),
    });
  };

  afterEach(() => {
    wrapper.destroy();
  });

  describe('loading state', () => {
    it('should display loading state when loading', () => {
      createComponent();

      expect(findLoadingIcon().exists()).toBe(true);
      expect(findStatusIcon().exists()).toBe(false);
    });
  });

  describe('loaded state', () => {
    beforeEach(async () => {
      createComponent();

      await waitForPromises();
    });

    it('should display pipeline status after the query is resolved successfully', async () => {
      expect(findStatusIcon().exists()).toBe(true);

      expect(findLoadingIcon().exists()).toBe(false);
      expect(createFlash).toHaveBeenCalledTimes(0);
    });

    it('should link to the latest pipeline', () => {
      const {
        data: {
          project: {
            pipeline: {
              detailedStatus: { detailsPath },
            },
          },
        },
      } = mockPipelineStatusResponse;

      expect(findPipelineLink().attributes('href')).toBe(detailsPath);
    });
  });

  describe('error state', () => {
    it('createFlash should show if there is an error fetching the pipeline status', async () => {
      createComponent(failedHandler);

      await waitForPromises();

      expect(createFlash).toHaveBeenCalledWith({
        message: PIPELINE_STATUS_FETCH_ERROR,
      });
    });
  });

  describe('polling', () => {
    it('polling interval is set for pipeline stages', () => {
      createComponent();

      const expectedInterval = wrapper.vm.$apollo.queries.pipelineStatus.options.pollInterval;

      expect(expectedInterval).toBe(COMMIT_BOX_POLL_INTERVAL);
    });

    it('polls for pipeline status', async () => {
      createComponent();

      await waitForPromises();

      expect(statusSuccessHandler).toHaveBeenCalledTimes(1);

      advanceToNextFetch();
      await waitForPromises();

      expect(statusSuccessHandler).toHaveBeenCalledTimes(2);

      advanceToNextFetch();
      await waitForPromises();

      expect(statusSuccessHandler).toHaveBeenCalledTimes(3);
    });

    it('toggles pipelineStatus polling with visibility check', async () => {
      jest.spyOn(graphQlUtils, 'toggleQueryPollingByVisibility');

      createComponent();

      await waitForPromises();

      expect(graphQlUtils.toggleQueryPollingByVisibility).toHaveBeenCalledWith(
        wrapper.vm.$apollo.queries.pipelineStatus,
      );
    });
  });
});