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
|
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { GlLoadingIcon } from '@gitlab/ui';
import { createAlert } from '~/alert';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import createMockApollo from 'helpers/mock_apollo_helper';
import getLinkedPipelinesQuery from '~/pipelines/graphql/queries/get_linked_pipelines.query.graphql';
import getPipelineStagesQuery from '~/pipelines/graphql/queries/get_pipeline_stages.query.graphql';
import LegacyPipelineMiniGraph from '~/pipelines/components/pipeline_mini_graph/legacy_pipeline_mini_graph.vue';
import PipelineMiniGraph from '~/pipelines/components/pipeline_mini_graph/pipeline_mini_graph.vue';
import * as sharedGraphQlUtils from '~/graphql_shared/utils';
import {
linkedPipelinesFetchError,
stagesFetchError,
mockPipelineStagesQueryResponse,
mockUpstreamDownstreamQueryResponse,
} from './mock_data';
Vue.use(VueApollo);
jest.mock('~/alert');
describe('PipelineMiniGraph', () => {
let wrapper;
let linkedPipelinesResponse;
let pipelineStagesResponse;
const fullPath = 'gitlab-org/gitlab';
const iid = '315';
const pipelineEtag = '/api/graphql:pipelines/id/315';
const createComponent = ({
pipelineStagesHandler = pipelineStagesResponse,
linkedPipelinesHandler = linkedPipelinesResponse,
} = {}) => {
const handlers = [
[getLinkedPipelinesQuery, linkedPipelinesHandler],
[getPipelineStagesQuery, pipelineStagesHandler],
];
const mockApollo = createMockApollo(handlers);
wrapper = shallowMountExtended(PipelineMiniGraph, {
propsData: {
fullPath,
iid,
pipelineEtag,
},
apolloProvider: mockApollo,
});
return waitForPromises();
};
const findLegacyPipelineMiniGraph = () => wrapper.findComponent(LegacyPipelineMiniGraph);
const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
beforeEach(() => {
linkedPipelinesResponse = jest.fn().mockResolvedValue(mockUpstreamDownstreamQueryResponse);
pipelineStagesResponse = jest.fn().mockResolvedValue(mockPipelineStagesQueryResponse);
});
describe('when initial queries are loading', () => {
beforeEach(() => {
createComponent();
});
it('shows a loading icon and no mini graph', () => {
expect(findLoadingIcon().exists()).toBe(true);
expect(findLegacyPipelineMiniGraph().exists()).toBe(false);
});
});
describe('when queries have loaded', () => {
it('does not show a loading icon', async () => {
await createComponent();
expect(findLoadingIcon().exists()).toBe(false);
});
it('renders the Pipeline Mini Graph', async () => {
await createComponent();
expect(findLegacyPipelineMiniGraph().exists()).toBe(true);
});
it('fires the queries', async () => {
await createComponent();
expect(linkedPipelinesResponse).toHaveBeenCalledWith({ iid, fullPath });
expect(pipelineStagesResponse).toHaveBeenCalledWith({ iid, fullPath });
});
});
describe('polling', () => {
it('toggles query polling with visibility check', async () => {
jest.spyOn(sharedGraphQlUtils, 'toggleQueryPollingByVisibility');
createComponent();
await waitForPromises();
expect(sharedGraphQlUtils.toggleQueryPollingByVisibility).toHaveBeenCalledTimes(2);
});
});
describe('when pipeline queries are unsuccessful', () => {
const failedHandler = jest.fn().mockRejectedValue(new Error('GraphQL error'));
it.each`
query | handlerName | errorMessage
${'pipeline stages'} | ${'pipelineStagesHandler'} | ${stagesFetchError}
${'linked pipelines'} | ${'linkedPipelinesHandler'} | ${linkedPipelinesFetchError}
`('throws an error for the $query query', async ({ errorMessage, handlerName }) => {
await createComponent({ [handlerName]: failedHandler });
await waitForPromises();
expect(createAlert).toHaveBeenCalledWith({ message: errorMessage });
});
});
});
|