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
|
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { GlLoadingIcon } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { createAlert } from '~/alert';
import PipelinesTableWrapper from '~/commit/pipelines/pipelines_table_wrapper.vue';
import getMergeRequestsPipelines from '~/ci/merge_requests/graphql/queries/get_merge_request_pipelines.query.graphql';
import { mergeRequestPipelinesResponse } from '../mock_data';
Vue.use(VueApollo);
jest.mock('~/alert');
const pipelinesLength = mergeRequestPipelinesResponse.data.project.mergeRequest.pipelines.count;
let wrapper;
let mergeRequestPipelinesRequest;
let apolloMock;
const defaultProvide = {
graphqlPath: '/api/graphql/',
mergeRequestId: 1,
targetProjectFullPath: '/group/project',
};
const createComponent = () => {
const handlers = [[getMergeRequestsPipelines, mergeRequestPipelinesRequest]];
apolloMock = createMockApollo(handlers);
wrapper = shallowMount(PipelinesTableWrapper, {
apolloProvider: apolloMock,
provide: {
...defaultProvide,
},
});
return waitForPromises();
};
const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
const findPipelineList = () => wrapper.findAll('li');
beforeEach(() => {
mergeRequestPipelinesRequest = jest.fn();
mergeRequestPipelinesRequest.mockResolvedValue(mergeRequestPipelinesResponse);
});
afterEach(() => {
apolloMock = null;
createAlert.mockClear();
});
describe('PipelinesTableWrapper component', () => {
describe('When queries are loading', () => {
beforeEach(() => {
createComponent();
});
it('renders the loading icon', () => {
expect(findLoadingIcon().exists()).toBe(true);
});
it('does not render the pipeline list', () => {
expect(findPipelineList()).toHaveLength(0);
});
});
describe('When there is an error fetching pipelines', () => {
beforeEach(async () => {
mergeRequestPipelinesRequest.mockRejectedValueOnce({ error: 'API error message' });
await createComponent();
});
it('shows an error message', () => {
expect(createAlert).toHaveBeenCalledTimes(1);
expect(createAlert).toHaveBeenCalledWith({
message: "There was an error fetching this merge request's pipelines.",
});
});
});
describe('When queries have loaded', () => {
beforeEach(async () => {
await createComponent();
});
it('does not render the loading icon', () => {
expect(findLoadingIcon().exists()).toBe(false);
});
it('renders a pipeline list', () => {
expect(findPipelineList()).toHaveLength(pipelinesLength);
});
});
describe('polling', () => {
beforeEach(async () => {
await createComponent();
});
it('polls every 10 seconds', () => {
expect(mergeRequestPipelinesRequest).toHaveBeenCalledTimes(1);
jest.advanceTimersByTime(5000);
expect(mergeRequestPipelinesRequest).toHaveBeenCalledTimes(1);
jest.advanceTimersByTime(5000);
expect(mergeRequestPipelinesRequest).toHaveBeenCalledTimes(2);
});
});
});
|