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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
import { GlIcon, GlLink, GlSprintf, GlFormGroup, GlFormRadioGroup, GlFormRadio } from '@gitlab/ui';
import { nextTick } from 'vue';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import CompareApp from '~/projects/compare/components/app.vue';
import {
COMPARE_REVISIONS_DOCS_URL,
I18N,
COMPARE_OPTIONS,
COMPARE_OPTIONS_INPUT_NAME,
} from '~/projects/compare/constants';
import RevisionCard from '~/projects/compare/components/revision_card.vue';
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';
import { appDefaultProps as defaultProps } from './mock_data';
jest.mock('~/lib/utils/csrf', () => ({ token: 'mock-csrf-token' }));
describe('CompareApp component', () => {
let wrapper;
const findSourceRevisionCard = () => wrapper.findByTestId('sourceRevisionCard');
const findTargetRevisionCard = () => wrapper.findByTestId('targetRevisionCard');
const createComponent = (props = {}) => {
wrapper = shallowMountExtended(CompareApp, {
propsData: {
...defaultProps,
...props,
},
directives: {
GlTooltip: createMockDirective('gl-tooltip'),
},
stubs: {
GlSprintf,
GlFormRadioGroup,
},
});
};
beforeEach(() => {
createComponent();
});
it('renders component with prop', () => {
expect(wrapper.props()).toEqual(
expect.objectContaining({
projectCompareIndexPath: defaultProps.projectCompareIndexPath,
sourceProjectRefsPath: defaultProps.sourceProjectRefsPath,
targetProjectRefsPath: defaultProps.targetProjectRefsPath,
paramsFrom: defaultProps.paramsFrom,
paramsTo: defaultProps.paramsTo,
}),
);
});
it('renders title', () => {
const title = wrapper.find('h1');
expect(title.text()).toBe(I18N.title);
});
it('renders subtitle', () => {
const subtitle = wrapper.find('p');
expect(subtitle.text()).toMatchInterpolatedText(I18N.subtitle);
});
it('renders link to docs', () => {
const docsLink = wrapper.findComponent(GlLink);
expect(docsLink.attributes('href')).toBe(COMPARE_REVISIONS_DOCS_URL);
});
it('contains the correct form attributes', () => {
expect(wrapper.attributes('action')).toBe(defaultProps.projectCompareIndexPath);
expect(wrapper.attributes('method')).toBe('POST');
});
it('has input with csrf token', () => {
expect(wrapper.find('input[name="authenticity_token"]').attributes('value')).toBe(
'mock-csrf-token',
);
});
it('render Source and Target BranchDropdown components', () => {
const revisionCards = wrapper.findAllComponents(RevisionCard);
expect(revisionCards.length).toBe(2);
expect(revisionCards.at(0).props('revisionText')).toBe(I18N.source);
expect(revisionCards.at(1).props('revisionText')).toBe(I18N.target);
});
describe('compare button', () => {
const findCompareButton = () => wrapper.findByTestId('compare-button');
it('renders button', () => {
expect(findCompareButton().exists()).toBe(true);
});
it('submits form', () => {
findCompareButton().vm.$emit('click');
expect(wrapper.find('form').element.submit).toHaveBeenCalled();
});
it('has compare text', () => {
expect(findCompareButton().text()).toBe('Compare');
});
});
it('sets the selected project when the "selectProject" event is emitted', async () => {
const project = {
name: 'some-to-name',
id: '1',
};
findTargetRevisionCard().vm.$emit('selectProject', {
direction: 'to',
project,
});
await nextTick();
expect(findTargetRevisionCard().props('selectedProject')).toEqual(
expect.objectContaining(project),
);
});
it('sets the selected revision when the "selectRevision" event is emitted', async () => {
const revision = 'some-revision';
findTargetRevisionCard().vm.$emit('selectRevision', {
direction: 'to',
revision,
});
await nextTick();
expect(findSourceRevisionCard().props('paramsBranch')).toBe(revision);
});
describe('swap revisions button', () => {
const findSwapRevisionsButton = () => wrapper.findByTestId('swapRevisionsButton');
it('renders the swap revisions button', () => {
expect(findSwapRevisionsButton().exists()).toBe(true);
});
it('renders icon', () => {
expect(findSwapRevisionsButton().findComponent(GlIcon).props('name')).toBe('substitute');
});
it('has tooltip', () => {
const tooltip = getBinding(findSwapRevisionsButton().element, 'gl-tooltip');
expect(tooltip.value).toBe(I18N.swapRevisions);
});
it('swaps revisions when clicked', async () => {
findSwapRevisionsButton().vm.$emit('click');
await nextTick();
expect(findTargetRevisionCard().props('paramsBranch')).toBe(defaultProps.paramsTo);
expect(findSourceRevisionCard().props('paramsBranch')).toBe(defaultProps.paramsFrom);
});
});
describe('compare options', () => {
const findGroup = () => wrapper.findComponent(GlFormGroup);
const findOptionsGroup = () => wrapper.findComponent(GlFormRadioGroup);
const findOptions = () => wrapper.findAllComponents(GlFormRadio);
it('renders label for the compare options', () => {
expect(findGroup().attributes('label')).toBe(I18N.optionsLabel);
});
it('correct input name', () => {
expect(findOptionsGroup().attributes('name')).toBe(COMPARE_OPTIONS_INPUT_NAME);
});
it('renders "only incoming changes" option', () => {
expect(findOptions().at(0).text()).toBe(COMPARE_OPTIONS[0].text);
});
it('renders "since source was created" option', () => {
expect(findOptions().at(1).text()).toBe(COMPARE_OPTIONS[1].text);
});
it('straight mode button when clicked', async () => {
expect(wrapper.props('straight')).toBe(false);
expect(wrapper.vm.isStraight).toBe(false);
findOptionsGroup().vm.$emit('input', COMPARE_OPTIONS[1].value);
await nextTick();
expect(wrapper.vm.isStraight).toBe(true);
});
});
describe('merge request buttons', () => {
const findProjectMrButton = () => wrapper.findByTestId('projectMrButton');
const findCreateMrButton = () => wrapper.findByTestId('createMrButton');
it('does not have merge request buttons', () => {
createComponent();
expect(findProjectMrButton().exists()).toBe(false);
expect(findCreateMrButton().exists()).toBe(false);
});
it('has "View open merge request" button', () => {
createComponent({
projectMergeRequestPath: 'some/project/merge/request/path',
});
expect(findProjectMrButton().exists()).toBe(true);
expect(findCreateMrButton().exists()).toBe(false);
});
it('has "Create merge request" button', () => {
createComponent({
createMrPath: 'some/create/create/mr/path',
});
expect(findProjectMrButton().exists()).toBe(false);
expect(findCreateMrButton().exists()).toBe(true);
});
});
});
|