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
|
import { shallowMount } from '@vue/test-utils';
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { createAlert } from '~/alert';
import commitReferences from '~/projects/commit_box/info/graphql/queries/commit_references.query.graphql';
import containingBranchesQuery from '~/projects/commit_box/info/graphql/queries/commit_containing_branches.query.graphql';
import RefsList from '~/projects/commit_box/info/components/refs_list.vue';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import {
FETCH_CONTAINING_REFS_EVENT,
FETCH_COMMIT_REFERENCES_ERROR,
} from '~/projects/commit_box/info/constants';
import CommitRefs from '~/projects/commit_box/info/components/commit_refs.vue';
import {
mockCommitReferencesResponse,
mockOnlyBranchesResponse,
mockContainingBranchesResponse,
refsListPropsMock,
} from '../mock_data';
Vue.use(VueApollo);
jest.mock('~/alert');
describe('Commit references component', () => {
let wrapper;
const successQueryHandler = (mockResponse) => jest.fn().mockResolvedValue(mockResponse);
const failedQueryHandler = jest.fn().mockRejectedValue(new Error('GraphQL error'));
const containingBranchesQueryHandler = successQueryHandler(mockContainingBranchesResponse);
const findRefsLists = () => wrapper.findAllComponents(RefsList);
const branchesList = () => findRefsLists().at(0);
const createComponent = async (
commitReferencesQueryHandler = successQueryHandler(mockCommitReferencesResponse),
) => {
wrapper = shallowMount(CommitRefs, {
apolloProvider: createMockApollo([
[commitReferences, commitReferencesQueryHandler],
[containingBranchesQuery, containingBranchesQueryHandler],
]),
provide: {
fullPath: '/some/project',
commitSha: 'xxx',
},
});
await waitForPromises();
};
it('renders component correcrly', async () => {
await createComponent();
expect(findRefsLists()).toHaveLength(2);
});
it('passes props to refs list', async () => {
await createComponent();
expect(branchesList().props()).toEqual(refsListPropsMock);
});
it('shows alert when response fails', async () => {
await createComponent(failedQueryHandler);
expect(createAlert).toHaveBeenCalledWith({
message: FETCH_COMMIT_REFERENCES_ERROR,
captureError: true,
});
});
it('fetches containing refs on the fetch event', async () => {
await createComponent();
branchesList().vm.$emit(FETCH_CONTAINING_REFS_EVENT);
await waitForPromises();
expect(containingBranchesQueryHandler).toHaveBeenCalledTimes(1);
});
it('does not render list when there is no branches or tags', async () => {
await createComponent(successQueryHandler(mockOnlyBranchesResponse));
expect(findRefsLists()).toHaveLength(1);
});
});
|