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
151
152
153
154
155
|
import { GlAvatarLabeled, GlCollapsibleListbox, GlListboxItem } from '@gitlab/ui';
import { mount, shallowMount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import ProjectDropdown from '~/jira_connect/branches/components/project_dropdown.vue';
import { PROJECTS_PER_PAGE } from '~/jira_connect/branches/constants';
import getProjectsQuery from '~/jira_connect/branches/graphql/queries/get_projects.query.graphql';
import { mockProjects } from '../mock_data';
const mockProjectsQueryResponse = {
data: {
projects: {
nodes: mockProjects,
pageInfo: {
hasNextPage: false,
hasPreviousPage: false,
startCursor: '',
endCursor: '',
},
},
},
};
const mockGetProjectsQuerySuccess = jest.fn().mockResolvedValue(mockProjectsQueryResponse);
const mockGetProjectsQueryFailed = jest.fn().mockRejectedValue(new Error('GraphQL error'));
const mockQueryLoading = jest.fn().mockReturnValue(new Promise(() => {}));
describe('ProjectDropdown', () => {
let wrapper;
const findDropdown = () => wrapper.findComponent(GlCollapsibleListbox);
const findAllGlListboxItems = () => wrapper.findAllComponents(GlListboxItem);
function createMockApolloProvider({ mockGetProjectsQuery = mockGetProjectsQuerySuccess } = {}) {
Vue.use(VueApollo);
const mockApollo = createMockApollo([[getProjectsQuery, mockGetProjectsQuery]]);
return mockApollo;
}
function createComponent({ mockApollo, props, mountFn = shallowMount } = {}) {
wrapper = mountFn(ProjectDropdown, {
apolloProvider: mockApollo || createMockApolloProvider(),
propsData: props,
stubs: { GlCollapsibleListbox },
});
}
describe('when loading projects', () => {
beforeEach(() => {
createComponent({
mockApollo: createMockApolloProvider({ mockGetProjectsQuery: mockQueryLoading }),
});
});
it('sets dropdown `loading` prop to `true`', () => {
expect(findDropdown().props('loading')).toBe(true);
});
});
describe('when projects query succeeds', () => {
beforeEach(async () => {
createComponent();
await nextTick();
});
it('sets dropdown `loading` prop to `false`', () => {
expect(findDropdown().props('loading')).toBe(false);
});
it('renders dropdown items with correct props', () => {
const dropdownItems = findDropdown().props('items');
expect(dropdownItems).toHaveLength(mockProjects.length);
expect(dropdownItems).toMatchObject(mockProjects);
});
it('renders dropdown items with correct template', () => {
expect(findAllGlListboxItems()).toHaveLength(mockProjects.length);
const avatars = findAllGlListboxItems().wrappers.map((item) =>
item.findComponent(GlAvatarLabeled),
);
const avatarAttributes = avatars.map((avatar) => avatar.attributes());
const avatarProps = avatars.map((avatar) => avatar.props());
expect(avatarProps).toMatchObject(
mockProjects.map((project) => ({
label: project.name,
subLabel: project.nameWithNamespace,
})),
);
expect(avatarAttributes).toMatchObject(
mockProjects.map((project) => ({
src: project.avatarUrl,
'entity-name': project.name,
})),
);
});
describe('when selecting a dropdown item', () => {
it('emits `change` event with the selected project', async () => {
const mockProject = mockProjects[0];
await findDropdown().vm.$emit('select', mockProject.id);
expect(wrapper.emitted('change')[0]).toEqual([mockProject]);
});
});
describe('when `selectedProject` prop is specified', () => {
const mockProject = mockProjects[0];
beforeEach(() => {
createComponent({ props: { selectedProject: mockProject } });
});
it('selects the specified item', () => {
expect(findDropdown().props('selected')).toBe(mockProject.id);
});
});
});
describe('when projects query fails', () => {
beforeEach(async () => {
createComponent({
mockApollo: createMockApolloProvider({ mockGetProjectsQuery: mockGetProjectsQueryFailed }),
});
await waitForPromises();
});
it('emits `error` event', () => {
expect(wrapper.emitted('error')).toHaveLength(1);
});
});
describe('when searching branches', () => {
it('triggers a refetch', async () => {
createComponent({ mountFn: mount });
jest.clearAllMocks();
const mockSearchTerm = 'gitl';
await findDropdown().vm.$emit('search', mockSearchTerm);
expect(mockGetProjectsQuerySuccess).toHaveBeenCalledWith({
after: '',
first: PROJECTS_PER_PAGE,
membership: true,
search: mockSearchTerm,
searchNamespaces: true,
sort: 'similarity',
});
});
});
});
|