summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_mr_widget/components/states/mr_widget_conflicts_spec.js
blob: e1bce7f0474445f8f345f3a8500bc6bc43cee954 (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { shallowMount } from '@vue/test-utils';
import { TEST_HOST } from 'helpers/test_constants';
import { removeBreakLine } from 'helpers/text_helper';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import ConflictsComponent from '~/vue_merge_request_widget/components/states/mr_widget_conflicts.vue';

describe('MRWidgetConflicts', () => {
  let wrapper;
  let mergeRequestWidgetGraphql = null;
  const path = '/conflicts';

  const findResolveButton = () => wrapper.findByTestId('resolve-conflicts-button');
  const findMergeLocalButton = () => wrapper.findByTestId('merge-locally-button');

  function createComponent(propsData = {}) {
    wrapper = extendedWrapper(
      shallowMount(ConflictsComponent, {
        propsData,
        provide: {
          glFeatures: {
            mergeRequestWidgetGraphql,
          },
        },
        mocks: {
          $apollo: {
            queries: {
              userPermissions: { loading: false },
              stateData: { loading: false },
            },
          },
        },
      }),
    );

    if (mergeRequestWidgetGraphql) {
      wrapper.setData({
        userPermissions: {
          canMerge: propsData.mr.canMerge,
          pushToSourceBranch: propsData.mr.canPushToSourceBranch,
        },
        stateData: {
          shouldBeRebased: propsData.mr.shouldBeRebased,
          sourceBranchProtected: propsData.mr.sourceBranchProtected,
        },
      });
    }

    return wrapper.vm.$nextTick();
  }

  afterEach(() => {
    mergeRequestWidgetGraphql = null;
    wrapper.destroy();
  });

  [false, true].forEach((featureEnabled) => {
    describe(`with GraphQL feature flag ${featureEnabled ? 'enabled' : 'disabled'}`, () => {
      beforeEach(() => {
        mergeRequestWidgetGraphql = featureEnabled;
      });

      // There are two permissions we need to consider:
      //
      // 1. Is the user allowed to merge to the target branch?
      // 2. Is the user allowed to push to the source branch?
      //
      // This yields 4 possible permutations that we need to test, and
      // we test them below. A user who can push to the source
      // branch should be allowed to resolve conflicts. This is
      // consistent with what the backend does.
      describe('when allowed to merge but not allowed to push to source branch', () => {
        beforeEach(async () => {
          await createComponent({
            mr: {
              canMerge: true,
              canPushToSourceBranch: false,
              conflictResolutionPath: path,
              conflictsDocsPath: '',
            },
          });
        });

        it('should tell you about conflicts without bothering other people', () => {
          expect(wrapper.text()).toContain('There are merge conflicts');
          expect(wrapper.text()).not.toContain('ask someone with write access');
        });

        it('should not allow you to resolve the conflicts', () => {
          expect(wrapper.text()).not.toContain('Resolve conflicts');
        });

        it('should have merge buttons', () => {
          expect(findMergeLocalButton().text()).toContain('Merge locally');
        });
      });

      describe('when not allowed to merge but allowed to push to source branch', () => {
        beforeEach(async () => {
          await createComponent({
            mr: {
              canMerge: false,
              canPushToSourceBranch: true,
              conflictResolutionPath: path,
              conflictsDocsPath: '',
            },
          });
        });

        it('should tell you about conflicts', () => {
          expect(wrapper.text()).toContain('There are merge conflicts');
          expect(wrapper.text()).toContain('ask someone with write access');
        });

        it('should allow you to resolve the conflicts', () => {
          expect(findResolveButton().text()).toContain('Resolve conflicts');
          expect(findResolveButton().attributes('href')).toEqual(path);
        });

        it('should not have merge buttons', () => {
          expect(wrapper.text()).not.toContain('Merge locally');
        });
      });

      describe('when allowed to merge and push to source branch', () => {
        beforeEach(async () => {
          await createComponent({
            mr: {
              canMerge: true,
              canPushToSourceBranch: true,
              conflictResolutionPath: path,
              conflictsDocsPath: '',
            },
          });
        });

        it('should tell you about conflicts without bothering other people', () => {
          expect(wrapper.text()).toContain('There are merge conflicts');
          expect(wrapper.text()).not.toContain('ask someone with write access');
        });

        it('should allow you to resolve the conflicts', () => {
          expect(findResolveButton().text()).toContain('Resolve conflicts');
          expect(findResolveButton().attributes('href')).toEqual(path);
        });

        it('should have merge buttons', () => {
          expect(findMergeLocalButton().text()).toContain('Merge locally');
        });
      });

      describe('when user does not have permission to push to source branch', () => {
        it('should show proper message', async () => {
          await createComponent({
            mr: {
              canMerge: false,
              canPushToSourceBranch: false,
              conflictsDocsPath: '',
            },
          });

          expect(wrapper.text().trim().replace(/\s\s+/g, ' ')).toContain(
            'ask someone with write access',
          );
        });

        it('should not have action buttons', async () => {
          await createComponent({
            mr: {
              canMerge: false,
              canPushToSourceBranch: false,
              conflictsDocsPath: '',
            },
          });

          expect(findResolveButton().exists()).toBe(false);
          expect(findMergeLocalButton().exists()).toBe(false);
        });

        it('should not have resolve button when no conflict resolution path', async () => {
          await createComponent({
            mr: {
              canMerge: true,
              conflictResolutionPath: null,
              conflictsDocsPath: '',
            },
          });

          expect(findResolveButton().exists()).toBe(false);
        });
      });

      describe('when fast-forward or semi-linear merge enabled', () => {
        it('should tell you to rebase locally', async () => {
          await createComponent({
            mr: {
              shouldBeRebased: true,
              conflictsDocsPath: '',
            },
          });

          expect(removeBreakLine(wrapper.text()).trim()).toContain(
            'Merge blocked: fast-forward merge is not possible. To merge this request, first rebase locally.',
          );
        });
      });

      describe('when source branch protected', () => {
        beforeEach(async () => {
          await createComponent({
            mr: {
              canMerge: true,
              canPushToSourceBranch: true,
              conflictResolutionPath: TEST_HOST,
              sourceBranchProtected: true,
              conflictsDocsPath: '',
            },
          });
        });

        it('should not allow you to resolve the conflicts', () => {
          expect(findResolveButton().exists()).toBe(false);
        });
      });

      describe('when source branch not protected', () => {
        beforeEach(async () => {
          await createComponent({
            mr: {
              canMerge: true,
              canPushToSourceBranch: true,
              conflictResolutionPath: TEST_HOST,
              sourceBranchProtected: false,
              conflictsDocsPath: '',
            },
          });
        });

        it('should allow you to resolve the conflicts', () => {
          expect(findResolveButton().text()).toContain('Resolve conflicts');
          expect(findResolveButton().attributes('href')).toEqual(TEST_HOST);
        });
      });
    });
  });
});